Count maximum number of disjoint pairs having one element not less than K times the other
Given an array arr[] and a positive integer K, the task is to find the maximum count of disjoint pairs (arr[i], arr[j]) such that arr[j] ? K * arr[i].
Examples:
Input: arr[] = { 1, 9, 4, 7, 3 }, K = 2
Output: 2
Explanation:
There can be 2 possible pairs that can formed from the given array i.e., (4, 1) and (7, 3) that satisfy the given conditions.
Input: arr[] = {2, 3, 4, 5, 6, 7, 8, 9}, K = 3
Output: 2
Approach: The given problem can be solved by using the Two Pointer Approach. Follow the steps below to solve the given problem:
- Sort the given array in increasing order.
- Initialize two variables i and j as 0 and (N / 2) respectively and variable count that stores the resultant maximum count of pairs.
- Traverse the given array over the range [0, N/2] and perform the following steps:
- Increment the value of j until j < N and arr[j] < K * arr[i].
- If the value of j is less than N, then increment the count of pairs by 1.
- Increment the value of j by 1.
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximizePairs( int arr[], int n, int k)
{
sort(arr, arr + n);
int i = 0, j = n / 2;
int count = 0;
for (i = 0; i < n / 2; i++) {
while (j < n
&& (k * arr[i]) > arr[j])
j++;
if (j < n)
count++;
j++;
}
return count;
}
int main()
{
int arr[] = { 1, 9, 4, 7, 3 };
int N = sizeof (arr) / sizeof ( int );
int K = 2;
cout << maximizePairs(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maximizePairs( int arr[], int n, int k)
{
Arrays.sort(arr);
int i = 0 , j = n / 2 ;
int count = 0 ;
for (i = 0 ; i < n / 2 ; i++) {
while (j < n
&& (k * arr[i]) > arr[j])
j++;
if (j < n)
count++;
j++;
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 1 , 9 , 4 , 7 , 3 };
int N = arr.length;
int K = 2 ;
System.out.print(maximizePairs(arr, N, K));
}
}
|
Python3
def maximizePairs(arr, n, k):
arr.sort()
i = 0
j = n / / 2
count = 0
for i in range (n / / 2 ):
while (j < n and (k * arr[i]) > arr[j]):
j + = 1
if (j < n):
count + = 1
j + = 1
return count
if __name__ = = '__main__' :
arr = [ 1 , 9 , 4 , 7 , 3 ]
N = len (arr)
K = 2
print (maximizePairs(arr, N, K))
|
C#
using System;
class GFG{
static int maximizePairs( int []arr, int n, int k)
{
Array.Sort(arr);
int i = 0, j = n / 2;
int count = 0;
for (i = 0; i < n / 2; i++) {
while (j < n
&& (k * arr[i]) > arr[j])
j++;
if (j < n)
count++;
j++;
}
return count;
}
public static void Main(String[] args)
{
int []arr = { 1, 9, 4, 7, 3 };
int N = arr.Length;
int K = 2;
Console.Write(maximizePairs(arr, N, K));
}
}
|
Javascript
<script>
function maximizePairs(arr, n, k) {
arr.sort((a, b) => a - b);
let i = 0,
j = Math.floor(n / 2);
let count = 0;
for (i = 0; i < Math.floor(n / 2); i++) {
while (j < n && k * arr[i] > arr[j]) j++;
if (j < n) count++;
j++;
}
return count;
}
let arr = [1, 9, 4, 7, 3];
let N = arr.length;
let K = 2;
document.write(maximizePairs(arr, N, K));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Last Updated :
11 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...