Related Articles

# Count unequal element pairs from the given Array

• Last Updated : 07 Apr, 2021

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:
(1, 2) and (1, 2) are the only valid pairs.
Input: arr[] = {1, 2, 3}
Output: 3
Input: arr[] = {1, 1, 1}
Output:

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

## Javascript

 ``
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up