Count valid pairs in the array satisfying given conditions
Last Updated :
12 Sep, 2022
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++
#include<bits/stdc++.h>
using namespace std;
int ValidPairs( int arr[], int n)
{
int count[121]={0};
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 ;
ans += count[arr[i]]* count[arr[j]];
if (arr[i] == arr[j])
ans -= count[arr[i]];
}
return ans;
}
int main()
{
int arr[] = {16, 17, 18};
int n= sizeof (arr)/ sizeof ( int );
cout<<(ValidPairs(arr,n));
return 0;
}
|
Java
class GFG{
static int ValidPairs( int arr[], int n)
{
int [] count= new int [ 121 ];
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 ;
ans += count[arr[i]]* count[arr[j]];
if (arr[i] == arr[j])
ans -= count[arr[i]];
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 16 , 17 , 18 };
int n= arr.length;
System.out.println(ValidPairs(arr,n));
}
}
|
Python3
def ValidPairs(arr):
count = [ 0 ] * 121
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
ans + = countX * countY
if eleX = = eleY:
ans - = countX
return ans
arr = [ 16 , 17 , 18 ]
print (ValidPairs(arr))
|
C#
using System;
class GFG{
static int ValidPairs( int [] arr, int n)
{
int [] count= new int [121];
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 ;
ans += count[arr[i]]* count[arr[j]];
if (arr[i] == arr[j])
ans -= count[arr[i]];
}
return ans;
}
public static void Main()
{
int [] arr = new int []{16, 17, 18};
int n= arr.Length;
Console.WriteLine(ValidPairs(arr,n));
}
}
|
PHP
<?php
function ValidPairs( $arr , $n )
{
$count = array_fill (0,121,0);
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 ;
$ans += $count [ $arr [ $i ]]* $count [ $arr [ $j ]];
if ( $arr [ $i ] == $arr [ $j ])
$ans -= $count [ $arr [ $i ]];
}
return $ans ;
}
$arr = array (16, 17, 18);
$n = count ( $arr );
echo (ValidPairs( $arr , $n ));
?>
|
Javascript
<script>
function ValidPairs(arr , n)
{
var count = Array(121).fill(0);
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 ;
ans += count[arr[i]] * count[arr[j]];
if (arr[i] == arr[j])
ans -= count[arr[i]];
}
return ans;
}
var arr = [ 16, 17, 18 ];
var n = arr.length;
document.write(ValidPairs(arr, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(n^2)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...