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 pairsint 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 Codeint main(){int arr[] = {16, 17, 18};int n= sizeof(arr)/sizeof(int); // Function call to print required answercout<<(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 pairsdef 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 Codearr = [16, 17, 18]# Function call to print required answerprint(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 pairsfunction 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 answerecho (ValidPairs($arr,$n));//This code is contributed by mits?> |
Javascript
<script>// javascript implementation of the approach // Function to return total valid pairs function ValidPairs(arr , n) { // Initialize count of all the elements var count = Array(121).fill(0); // frequency count of all the elements for (i = 0; i < n; i++) count[arr[i]] += 1; var ans = 0; for (i = 0; i < n; i++) for (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 var arr = [ 16, 17, 18 ]; var n = arr.length; // Function call to print required answer document.write(ValidPairs(arr, n));// This code is contributed by umadevi9616</script> |
1
Complexity Analysis:
- Time Complexity: O(n^2)
- Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


