Count of distinct pairs having one element as K times the other
Given an array arr[] and an integer K, find the maximum number of pairs that can be made such that one element is K times the other i.e, arr[i]=K*arr[j].
Examples:
Input: arr[] = {1, 2, 1, 2, 4} K = 2
Output: 2
Explanation: There are two possible ways to construct pairs: ({1, 2}, {1, 2}) and ({1, 2}, {2, 4}).
Input: a = {5, 4, 3, 2, 1} K = 2
Output: 1
Explanation: We can construct either set {1, 2} or set {2, 4}.
Approach: Sort the given array arr[] and check all the possible pairs of the array arr[] and check if a given (i, j) arr[i]=2*arr[j]. Follow the steps below to solve the problem:
- Sort the array arr[] using the sort function in C++ STL.
- Initialize a vector used to keep the count of already used elements.
- Initialize the variable ans as 0 to store the count of all possible pairs.
- Iterate over the range [0, N-1] using the variable i and perform the following steps:
- Iterate over the range [l, N-1] using the variable j and do the following:
- If the value of used[j] and used[i] are false and arr[j]=K*arr[i], then, set the value of used[i] and used[j] to true and increase the value of ans by 1 and break the loop.
- Finally, after completing the above steps, print the value of ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxPairs(vector< int > a, int k)
{
sort(a.begin(), a.end());
int n = a.size(), ans = 0;
vector< bool > used(n);
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (!used[j]
&& a[j] == k * a[i]
&& !used[i]) {
used[j] = used[i] = true ;
ans++;
break ;
}
}
}
return ans;
}
int32_t main()
{
vector< int > a{ 1, 2, 1, 2, 4 };
int k = 2;
cout << maxPairs(a, k);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
public static int maxPairs( int [] a, int k)
{
Arrays.sort(a);
int n = a.length, ans = 0 ;
boolean [] used = new boolean [n];
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
if (!used[j]
&& a[j] == k * a[i]
&& !used[i]) {
used[i] = true ;
used[j] = used[i];
ans++;
break ;
}
}
}
return ans;
}
public static void main(String args[])
{
int [] a = { 1 , 2 , 1 , 2 , 4 };
int k = 2 ;
System.out.println(maxPairs(a, k));
}
}
|
Python3
def maxPairs(a, k):
a.sort()
n = len (a)
ans = 0
used = [ False ] * n
for i in range ( 0 , n):
for j in range (i + 1 , n):
if (used[j] = = False and a[j] = = k * a[i] and used[i] = = False ):
used[j] = used[j] = True
ans + = 1
break
return ans
a = [ 1 , 2 , 1 , 2 , 4 ]
k = 2
print (maxPairs(a, k))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maxPairs(List< int > a, int k)
{
a.Sort();
int n = a.Count, ans = 0;
int [] Ar = new int [n];
List< int > used = new List< int >(Ar);
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (used[j]==0
&& a[j] == k * a[i]
&& used[i]==0) {
used[j] = used[i] = 1;
ans++;
break ;
}
}
}
return ans;
}
public static void Main(){
List< int > a = new List< int >(){ 1, 2, 1, 2, 4 };
int k = 2;
Console.Write(maxPairs(a, k));
}
}
|
Javascript
<script>
function maxPairs(a, k)
{
a.sort( function (x, y) { return x - y });
let n = a.length, ans = 0;
let used = new Array(n).fill( false );
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (used[j] == false && a[j] == k * a[i] && used[i] == false ) {
used[j] = used[j] = true ;
ans++;
break ;
}
}
}
return ans;
}
let a = [1, 2, 1, 2, 4];
let k = 2;
document.write(maxPairs(a, k));
</script>
|
Time Complexity: O(N^2)
Space Complexity: O(N)
Last Updated :
31 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...