Count valid pairs in the array satisfying given conditions
Given an array of integers arr[], the task is to count the number of valid pairs of elements from arr. A pair (arr[x], arr[y]) is said to be invalid if
- arr[x] < arr[y]
- abs(arr[x] – arr[y]) is odd
Note: Pairs (arr[x], arr[y]) and (arr[y], arr[x]) are two different pairs when x != y and the value of arr[i] for all possible values of i is ≤ 120.
Examples:
Input: arr[] = {16, 17, 18}
Output: 2
Only valid pair is (18, 16)Input: arr[] = {16, 16}
Output: 2
Valid pairs are (16, 16) and (16, 16)
Approach: Instead of processing all the elements, we can process pairs of (arr[i], count) representing the count of element arr[i] in the array. Since there are only 120 possible values, we make a frequency count of each element group which reduces the overall complexity.
For each pair (arr[x], countX) and (arr[y], countY), if the conditions are satisfied, then total valid pairs will be countX * countY.
If arr[x] = arr[y], then we over-counted some pairs. In that case, valid pairs will be countA * (countA – 1) as no element can make a pair with itself.
Below is the implementation of the above approach:
C++
//C++ implementation of the approach #include<bits/stdc++.h> using namespace std; // Function to return total valid pairs int ValidPairs(int arr[],int n) { // Initialize count of all the elements int count[121]={0}; // frequency count of all the elements for(int i=0;i<n;i++) count[arr[i]] += 1; int ans = 0; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if( arr[i] < arr[j]) continue; if (abs(arr[i] - arr[j]) % 2 == 1) continue; // Add total valid pairs ans += count[arr[i]]* count[arr[j]]; if (arr[i] == arr[j]) // Exclude pairs made with a single element // i.e. (x, x) ans -= count[arr[i]]; } return ans; } // Driver Code int main() { int arr[] = {16, 17, 18}; int n= sizeof(arr)/sizeof(int); // Function call to print required answer cout<<(ValidPairs(arr,n)); return 0; } //contributed by Arnab Kundu |
Java
//Java implementation of the approach class GFG{ // Function to return total valid pairs static int ValidPairs(int arr[],int n) { // Initialize count of all the elements int[] count=new int[121]; // frequency count of all the elements for(int i=0;i<n;i++) count[arr[i]] += 1; int ans = 0; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if( arr[i] < arr[j]) continue; if (Math.abs(arr[i] - arr[j]) % 2 == 1) continue; // Add total valid pairs ans += count[arr[i]]* count[arr[j]]; if (arr[i] == arr[j]) // Exclude pairs made with a single element // i.e. (x, x) ans -= count[arr[i]]; } return ans; } // Driver Code public static void main(String[] args) { int arr[] = {16, 17, 18}; int n= arr.length; // Function call to print required answer System.out.println(ValidPairs(arr,n)); } } // This code contributed by mits |
Python3
# Python3 implementation of the approach # Function to return total valid pairs def ValidPairs(arr): # Initialize count of all the elements count = [0] * 121 # frequency count of all the elements for ele in arr: count[ele] += 1 ans = 0 for eleX, countX in enumerate(count): for eleY, countY in enumerate(count): if eleX < eleY: continue if (abs(eleX - eleY) % 2 == 1): continue # Add total valid pairs ans += countX * countY if eleX == eleY: # Exclude pairs made with a single element # i.e. (x, x) ans -= countX return ans # Driver Code arr = [16, 17, 18] # Function call to print required answer print(ValidPairs(arr)) |
C#
//C# implementation of the approach using System; class GFG{ // Function to return total valid pairs static int ValidPairs(int[] arr,int n) { // Initialize count of all the elements int[] count=new int[121]; // frequency count of all the elements for(int i=0;i<n;i++) count[arr[i]] += 1; int ans = 0; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if( arr[i] < arr[j]) continue; if (Math.Abs(arr[i] - arr[j]) % 2 == 1) continue; // Add total valid pairs ans += count[arr[i]]* count[arr[j]]; if (arr[i] == arr[j]) // Exclude pairs made with a single element // i.e. (x, x) ans -= count[arr[i]]; } return ans; } // Driver Code public static void Main() { int[] arr =new int[]{16, 17, 18}; int n= arr.Length; // Function call to print required answer Console.WriteLine(ValidPairs(arr,n)); } } // This code contributed by mits |
PHP
<?php //PHP implementation of the approach // Function to return total valid pairs function ValidPairs($arr,$n) { // Initialize count of all the elements $count=array_fill(0,121,0); // frequency count of all the elements for($i=0;$i<$n;$i++) $count[$arr[$i]] += 1; $ans = 0; for($i=0;$i<$n;$i++) for($j=0;$j<$n;$j++) { if( $arr[$i] < $arr[$j]) continue; if (abs($arr[$i] - $arr[$j]) % 2 == 1) continue; // Add total valid pairs $ans += $count[$arr[$i]]* $count[$arr[$j]]; if ($arr[$i] == $arr[$j]) // Exclude pairs made with a single element // i.e. (x, x) $ans -= $count[$arr[$i]]; } return $ans; } // Driver Code $arr = array(16, 17, 18); $n= count($arr); // Function call to print required answer echo (ValidPairs($arr,$n)); //This code is contributed by mits ?> |
1
Recommended Posts:
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Count of pairs (x, y) in an array such that x < y
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Find sum of a[i]%a[j] for all valid pairs
- Count pairs in an array that hold i+j= arr[i]+arr[j]
- Count divisible pairs in an array
- Count pairs in array whose sum is divisible by 4
- Count pairs in an array such that frequency of one is at least value of other
- Count pairs in array whose sum is divisible by K
- Count pairs in an array that hold i*arr[i] > j*arr[j]
- Count pairs in a sorted array whose sum is less than x
- Count of pairs in an array whose sum is a perfect square
- Count pairs in an array such that at least one element is prime
- Count pairs in a sorted array whose product is less than k
- Count pairs in an array such that the absolute difference between them is ≥ K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



