Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
Last Updated :
26 May, 2021
Given an array arr[], the task is to count unordered pairs of positive integers (p, q) such that p occurs in the array at least q times and q occurs at least p times.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 1
(1, 1) is the only valid pair.
Input: arr[] = {3, 3, 2, 2, 2}
Output: 2
(2, 3) and (2, 2) are the only possible pairs.
Approach:
- The idea is to hash every element of an array with its count and create a vector of unique elements from the array elements. Initialize the number of pairs to 0.
- Loop through the vector of unique elements to count the number of pairs.
- Inside the loop, if the count of the element is less than the element itself, then continue, else if the count of the element is equal to the element, then increment the number of pairs by 1 (Pair of the form (x, x)), else go to Step 4.
- Increment the number of pairs by 1 and loop from j = (element + 1) to the count of the element updating j by 1 if the count of j is greater than or equal to an element, then increment the number of pairs by 1 as the pairs are unordered.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int get_unordered_pairs( int a[], int n)
{
vector< int > vs;
unordered_map< int , int > m;
for ( int i = 0; i < n; i++) {
m[a[i]]++;
if (m[a[i]] == 1)
vs.push_back(a[i]);
}
int number_of_pairs = 0;
for ( int i = 0; i < vs.size(); i++) {
if (m[vs[i]] < vs[i])
continue ;
else if (m[vs[i]] == vs[i])
number_of_pairs += 1;
else {
number_of_pairs += 1;
for ( int j = vs[i] + 1; j <= m[vs[i]]; j++) {
if (m[j] >= vs[i])
number_of_pairs += 1;
}
}
}
return number_of_pairs;
}
int main()
{
int arr[] = { 3, 3, 2, 2, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << get_unordered_pairs(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int get_unordered_pairs( int []a, int n)
{
ArrayList<Integer> vs = new ArrayList<Integer>();
int [] m = new int [maximum(a)+ 1 ];
for ( int i = 0 ; i < n; i++)
{
m[( int )a[i]]++;
if (m[a[i]] == 1 )
vs.add(a[i]);
}
int number_of_pairs = 0 ;
for ( int i = 0 ; i < vs.size(); i++)
{
if (m[( int )vs.get(i)] < ( int )vs.get(i))
continue ;
else if (m[( int )vs.get(i)] == ( int )vs.get(i))
number_of_pairs += 1 ;
else
{
number_of_pairs += 1 ;
for ( int j = ( int )vs.get(i) + 1 ; j <= m[( int )vs.get(i)]; j++)
{
if (m[j] >= ( int )vs.get(i))
number_of_pairs += 1 ;
}
}
}
return number_of_pairs;
}
static int maximum( int []arr)
{
int max = Integer.MIN_VALUE;
for ( int i = 0 ; i < arr.length; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
public static void main (String[] args)
{
int []arr = { 3 , 3 , 2 , 2 , 2 };
int n = arr.length;
System.out.println(get_unordered_pairs(arr, n));
}
}
|
Python3
from collections import defaultdict
def get_unordered_pairs(a, n):
vs = []
m = defaultdict( lambda : 0 )
for i in range ( 0 , n):
m[a[i]] + = 1
if m[a[i]] = = 1 :
vs.append(a[i])
number_of_pairs = 0
for i in range ( 0 , len (vs)):
if m[vs[i]] < vs[i]:
continue
elif m[vs[i]] = = vs[i]:
number_of_pairs + = 1
else :
number_of_pairs + = 1
for j in range (vs[i] + 1 , m[vs[i]] + 1 ):
if m[j] > = vs[i]:
number_of_pairs + = 1
return number_of_pairs
if __name__ = = "__main__" :
arr = [ 3 , 3 , 2 , 2 , 2 ]
n = len (arr)
print (get_unordered_pairs(arr, n))
|
C#
using System;
using System.Collections;
using System.Linq;
class GFG
{
static int get_unordered_pairs( int []a, int n)
{
ArrayList vs = new ArrayList();
int [] m = new int [a.Max()+1];
for ( int i = 0; i < n; i++)
{
m[( int )a[i]]++;
if (m[a[i]] == 1)
vs.Add(a[i]);
}
int number_of_pairs = 0;
for ( int i = 0; i < vs.Count; i++)
{
if (m[( int )vs[i]] < ( int )vs[i])
continue ;
else if (m[( int )vs[i]] == ( int )vs[i])
number_of_pairs += 1;
else
{
number_of_pairs += 1;
for ( int j = ( int )vs[i] + 1; j <= m[( int )vs[i]]; j++)
{
if (m[j] >= ( int )vs[i])
number_of_pairs += 1;
}
}
}
return number_of_pairs;
}
static void Main()
{
int []arr = { 3, 3, 2, 2, 2 };
int n = arr.Length;
Console.WriteLine(get_unordered_pairs(arr, n));
}
}
|
PHP
<?php
function get_unordered_pairs( $a , $n )
{
$vs = array ();
$m = array ();
for ( $i = 0; $i < $n ; $i ++)
$m [ $a [ $i ]] = 0 ;
for ( $i = 0; $i < $n ; $i ++)
{
$m [ $a [ $i ]]++;
if ( $m [ $a [ $i ]] == 1)
array_push ( $vs , $a [ $i ]);
}
$number_of_pairs = 0;
for ( $i = 0; $i < sizeof( $vs ); $i ++)
{
if ( $m [ $vs [ $i ]] < $vs [ $i ])
continue ;
else if ( $m [ $vs [ $i ]] == $vs [ $i ])
$number_of_pairs += 1;
else
{
$number_of_pairs += 1;
for ( $j = $vs [ $i ] + 1;
$j <= $m [ $vs [ $i ]]; $j ++)
{
if ( $m [ $j ] >= $vs [ $i ])
$number_of_pairs += 1;
}
}
}
return $number_of_pairs ;
}
$arr = array (3, 3, 2, 2, 2);
$n = sizeof( $arr );
echo get_unordered_pairs( $arr , $n );
?>
|
Javascript
<script>
function get_unordered_pairs(a, n)
{
let vs = [];
let m = Array.from({length: Math.max(...a)+1}, (_, i) => 0);
for (let i = 0; i < n; i++)
{
m[a[i]]++;
if (m[a[i]] == 1)
vs.push(a[i]);
}
let number_of_pairs = 0;
for (let i = 0; i < vs.length; i++)
{
if (m[vs[i]] < vs[i])
continue ;
else if (m[vs[i]] == vs[i])
number_of_pairs += 1;
else
{
number_of_pairs += 1;
for (let j = vs[i] + 1; j <= m[vs[i]]; j++)
{
if (m[j] >= vs[i])
number_of_pairs += 1;
}
}
}
return number_of_pairs;
}
let arr = [ 3, 3, 2, 2, 2 ];
let n = arr.length;
document.write(get_unordered_pairs(arr, n));
</script>
|
Time Complexity: O(N* log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...