Count unequal element pairs from the given Array

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: 3

Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : AnkitRai01, 29AjayKumar