Find the number of Dominant Pairs
Last Updated :
20 Sep, 2023
Given an array arr[] of integers of size n where n is even, the task is to calculate the number of dominant pairs (i, j), where a pair is called dominant if (0 ≤ i < n/2, n / 2 ≤ j<n, arr[i] ≥ 5*arr[j]) this relation is fulfilled. (0-based indexing is used and n is even)
Examples:
Input: N = 4, arr[] = {10, 2, 2, 1}
Output: 2
Explanation: As we can see index i = 0 and j = 2 where arr[0] ≥ 5*arr[2] (10 ≥ 5*2)is fulfilled so this forms a dominant pair and in the same manner index i = 0 and j = 3 form a dominant pair. So a total of 2 dominant pairs.
Input: N = 6, arr[] = {10, 8, 2, 1, 1, 2}
Output: 5
Explanation: As we can see index i = 0 and j = 3 where arr[0] ≥ 5*arr[3] (10 ≥ 5*1) is fulfilled so this forms a dominant pair and in the same manner (0, 4), (0, 5), (1, 3), (1, 4) also form dominant pair. So a total of 5 dominant pairs.
Approach: This can be solved with the following idea:
The idea is to first sort the first half of the array in ascending order and the second half of the array in descending order. This is because if we want to find pairs (i, j) where i < j, then the values of i should be from the first half and the values of j should be from the second half and then keep fulfilling the condition arr[i] ≥ 5*arr[j].
Below are the steps involved in the implementation of the code:
- Initializes a variable ans to 0 and a variable right to n/2.
- Sort the array arr[] in two parts, first from 0 to n/2 and then from n/2 to n.
- Iterate through the first half of the array using a variable left. For each value of left, iterate through the second half of the array using variable right until it finds a value of j such that
arr[left] ≥ 5 * arr[right] (because if this condition is not fulfilled, then increasing the value of right will not result in any new dominant pairs).
- For each value of left, add the number of elements from the second half that satisfy the condition arr[left] ≥ 5 * arr[right] to the variable ans.
- Finally, return the value of ans, which represents the total number of dominant pairs in the given array.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
using namespace std;
int dominantPairs( int n, vector< int >& arr)
{
sort(arr.begin(), arr.begin() + (n / 2));
sort(arr.begin() + (n / 2), arr.end());
int ans = 0;
int right = n / 2;
for ( int left = 0; left < n / 2; left++) {
while (right < n && arr[left] >= 5 * arr[right]) {
right++;
}
ans += right - n / 2;
}
return ans;
}
int main()
{
int n = 6;
vector< int > arr = { 10, 8, 2, 1, 1, 2 };
int ans = dominantPairs(n, arr);
cout << ans;
return 0;
}
|
Java
import java.util.Arrays;
import java.util.List;
class GFG {
public static int dominantPairs( int n,
List<Integer> arr)
{
int [] sortedArr = new int [n];
for ( int i = 0 ; i < n; i++) {
sortedArr[i] = arr.get(i);
}
Arrays.sort(sortedArr, 0 , n / 2 );
Arrays.sort(sortedArr, n / 2 , n);
int ans = 0 ;
int right = n / 2 ;
for ( int left = 0 ; left < n / 2 ; left++) {
while (right < n
&& sortedArr[left]
>= 5 * sortedArr[right]) {
right++;
}
ans += right - n / 2 ;
}
return ans;
}
public static void main(String[] args)
{
int n = 6 ;
List<Integer> arr
= Arrays.asList( 10 , 8 , 2 , 1 , 1 , 2 );
int ans = dominantPairs(n, arr);
System.out.println(ans);
}
}
|
Python3
def dominantPairs(n, arr):
arr[:n / / 2 ] = sorted (arr[:n / / 2 ])
arr[n / / 2 :] = sorted (arr[n / / 2 :])
ans = 0
right = n / / 2
for left in range (n / / 2 ):
while right < n and arr[left] > = 5 * arr[right]:
right + = 1
ans + = right - n / / 2
return ans
n = 6
arr = [ 10 , 8 , 2 , 1 , 1 , 2 ]
ans = dominantPairs(n, arr)
print (ans)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static int DominantPairs( int n, List< int > arr)
{
arr.Sort(0, n / 2, null );
arr.Sort(n / 2, n - (n / 2), null );
int ans = 0;
int right = n / 2;
for ( int left = 0; left < n / 2; left++)
{
while (right < n && arr[left] >= 5 * arr[right])
{
right++;
}
ans += right - n / 2;
}
return ans;
}
static void Main( string [] args)
{
int n = 6;
List< int > arr = new List< int > { 10, 8, 2, 1, 1, 2 };
int ans = DominantPairs(n, arr);
Console.WriteLine(ans);
}
}
|
Javascript
function dominantPairs(n, arr) {
arr.splice(0, Math.floor(n/2), ...arr.slice(0, Math.floor(n / 2)).sort((a, b)=>a-b));
arr.splice(Math.floor(n/2), Math.floor(n/2),...arr.slice(Math.floor(n / 2)).sort((a,b)=>a-b));
let ans = 0;
let right = Math.floor(n / 2);
for (let left = 0; left < Math.floor(n / 2); left++) {
while (right < n && arr[left] >= 5 * arr[right]) {
right += 1;
}
ans += right - Math.floor(n / 2);
}
return ans;
}
let n = 6;
let arr = [10, 8, 2, 1, 1, 2];
let ans = dominantPairs(n, arr);
console.log(ans);
|
Time Complexity: O(NlogN)
Auxiliary Space :O(1)
Share your thoughts in the comments
Please Login to comment...