Given an array arr[] of N elements. The task is to count the total number of indices (i, j) such that arr[i] != arr[j] and i < j.
Examples:
Input: arr[] = {1, 1, 2}
Output: 2
(1, 2) and (1, 2) are the only valid pairs.Input: arr[] = {1, 2, 3}
Output: 3Input: arr[] = {1, 1, 1}
Output: 0
Approach: Initialise a count variable cnt = 0 and run two nested loops to check every possible pair whether the the current pair is valid or not. If it is valid, then increment the count variable. Finally, print the count of valid pairs.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the // count of valid pairs int countPairs(int arr[], int n) { // To store the required count int cnt = 0; // For each index pair (i, j) for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // If current pair is valid // then increment the count if (arr[i] != arr[j]) cnt++; } } return cnt; } // Driven code int main() { int arr[] = { 1, 1, 2 }; int n = sizeof(arr) / sizeof(int); cout << countPairs(arr, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the // count of valid pairs static int countPairs(int arr[], int n) { // To store the required count int cnt = 0; // For each index pair (i, j) for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // If current pair is valid // then increment the count if (arr[i] != arr[j]) cnt++; } } return cnt; } // Driven code public static void main (String[] args) { int arr[] = { 1, 1, 2 }; int n = arr.length; System.out.println(countPairs(arr, n)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to return the # count of valid pairs def countPairs(arr, n): # To store the required count cnt = 0; # For each index pair (i, j) for i in range(n): for j in range(i + 1, n): # If current pair is valid # then increment the count if (arr[i] != arr[j]): cnt += 1; return cnt; # Driver code if __name__ == '__main__': arr = [ 1, 1, 2 ]; n = len(arr); print(countPairs(arr, n)); # This code is contributed by 29AjayKumar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the // count of valid pairs static int countPairs(int []arr, int n) { // To store the required count int cnt = 0; // For each index pair (i, j) for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // If current pair is valid // then increment the count if (arr[i] != arr[j]) cnt++; } } return cnt; } // Driven code public static void Main() { int []arr = { 1, 1, 2 }; int n = arr.Length; Console.WriteLine(countPairs(arr, n)); } } // This code is contributed by AnkitRai01 |
2
Time Complexity: O(N2)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count pairs in array such that one element is reverse of another
- Count equal element pairs in the given array
- Count pairs in an array such that at least one element is prime
- Count of pairs having each element equal to index of the other from an Array
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Count of pairs with difference at most K with no element repeating
- Count pairs formed by distinct element sub-arrays
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Count numbers in range such that digits in it and it's product with q are unequal
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Count of pairs (x, y) in an array such that x < y
- Count of odd and even sum pairs in an array
- Count pairs in an array containing at least one even value
- Count pairs in array whose sum is divisible by 4
- Count pairs in an array that hold i*arr[i] > j*arr[j]
- Count pairs in array whose sum is divisible by K
- Count of pairs (i, j) in the array such that arr[i] is a factor of arr[j]
- Count divisible pairs in an array
- Count of pairs in an Array whose sum is Prime
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.

