GeeksforGeeks App
Open App
Browser
Continue

# 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:
Only valid pair is (18, 16)

Input: arr[] = {16, 16}
Output:
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``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

## 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

## 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

## PHP

 ``

## Javascript

 ``

Output

`1`

Complexity Analysis:

• Time Complexity: O(n^2)
• Auxiliary Space: O(1)

My Personal Notes arrow_drop_up