Count unequal element pairs from the given Array
Last Updated :
19 Sep, 2023
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 current pair is valid or not. If it is valid, then increment the count variable. Finally, print the count of valid pairs.
Algorithm:
- Define a static method named “countPairs” that takes two parameters, an integer array “arr” and an integer “n”, and returns an integer.
- Declare an integer variable “cnt” and initialize it to 0. This variable will store the count of valid pairs.
- Use a nested for-loop to iterate through each index pair (i, j) of the input array “arr”.
- If the current pair of values at indices i and j are different, then increment the “cnt” variable by 1. This indicates that the current pair is a valid pair.
- Return the final value of “cnt”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr[], int n)
{
int cnt = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (arr[i] != arr[j])
cnt++;
}
}
return cnt;
}
int main()
{
int arr[] = { 1, 1, 2 };
int n = sizeof (arr) / sizeof ( int );
cout << countPairs(arr, n);
return 0;
}
|
Java
class GFG
{
static int countPairs( int arr[], int n)
{
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
if (arr[i] != arr[j])
cnt++;
}
}
return cnt;
}
public static void main (String[] args)
{
int arr[] = { 1 , 1 , 2 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
def countPairs(arr, n):
cnt = 0 ;
for i in range (n):
for j in range (i + 1 , n):
if (arr[i] ! = arr[j]):
cnt + = 1 ;
return cnt;
if __name__ = = '__main__' :
arr = [ 1 , 1 , 2 ];
n = len (arr);
print (countPairs(arr, n));
|
C#
using System;
class GFG
{
static int countPairs( int []arr, int n)
{
int cnt = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if (arr[i] != arr[j])
cnt++;
}
}
return cnt;
}
public static void Main()
{
int []arr = { 1, 1, 2 };
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
|
Javascript
<script>
function countPairs(arr, n)
{
var cnt = 0;
for ( var i = 0; i < n; i++) {
for ( var j = i + 1; j < n; j++) {
if (arr[i] != arr[j])
cnt++;
}
}
return cnt;
}
var arr = [ 1, 1, 2 ];
var n = arr.length;
document.write(countPairs(arr, n));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Approach 2: Count unequal element pairs from the given Array Using Recursion.
Algorithm:
1. Create function “countPairsRec” which takes array “arr[ ]” and indices “i” and “j” .
2. If the value of “i” is larger than or equal to the value of “j”, the function returns 0 because there are no more pairs to compare.
3. Otherwise, determine whether the pair (arr[i], arr[j]) is a valid pair. If it is valid, increase the count by one and call the function again with the next pair.
4. If the pair (arr[i], arr[j]) is invalid, go to the next pair without increasing the count.
5. At the end of the function, return the count.
6. Create a new function called “countPairs” that accepts an integer array “arr” and an integer “n” as input parameters.
7. In the function, call the “countPairsRec” function with the array’s starting and ending indices.
Here’s the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairsRec( int arr[], int i, int j) {
if (i >= j) {
return 0;
}
if (arr[i] != arr[j]) {
return 1 + countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
else {
return countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
}
int countPairs( int arr[], int n) {
return countPairsRec(arr, 0, n-1);
}
int main() {
int arr[] = {1, 2, 3};
int n = sizeof (arr) / sizeof ( int );
cout << countPairs(arr, n) << endl;
return 0;
}
|
C
#include <stdio.h>
int countPairsRec( int arr[], int i, int j) {
if (i >= j) {
return 0;
}
if (arr[i] != arr[j]) {
return 1 + countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
else {
return countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
}
int countPairs( int arr[], int n) {
return countPairsRec(arr, 0, n-1);
}
int main() {
int arr[] = {1, 2, 3};
int n = sizeof (arr) / sizeof ( int );
printf ( "%d\n" , countPairs(arr, n));
return 0;
}
|
Java
import java.util.*;
class Main {
static int countPairsRec( int arr[], int i, int j) {
if (i >= j) {
return 0 ;
}
if (arr[i] != arr[j]) {
return 1 + countPairsRec(arr, i+ 1 , j) + countPairsRec(arr, i, j- 1 );
}
else {
return countPairsRec(arr, i+ 1 , j) + countPairsRec(arr, i, j- 1 );
}
}
static int countPairs( int arr[], int n) {
return countPairsRec(arr, 0 , n- 1 );
}
public static void main(String[] args) {
int arr[] = { 1 , 2 , 3 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
def countPairsRec(arr, i, j):
if i > = j:
return 0
if arr[i] ! = arr[j]:
return 1 + countPairsRec(arr, i + 1 , j) + countPairsRec(arr, i, j - 1 )
else :
return countPairsRec(arr, i + 1 , j) + countPairsRec(arr, i, j - 1 )
def countPairs(arr):
return countPairsRec(arr, 0 , len (arr) - 1 )
arr = [ 1 , 2 , 3 ]
print (countPairs(arr))
|
C#
using System;
class Program
{
static int countPairsRec( int [] arr, int i, int j) {
if (i >= j) {
return 0;
}
if (arr[i] != arr[j]) {
return 1 + countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
else {
return countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
}
static int countPairs( int [] arr, int n) {
return countPairsRec(arr, 0, n-1);
}
static void Main( string [] args) {
int [] arr = {1, 2, 3};
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
|
Javascript
function countPairsRec(arr, i, j) {
if (i >= j) {
return 0;
}
if (arr[i] !== arr[j]) {
return 1 + countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
else {
return countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
}
function countPairs(arr) {
return countPairsRec(arr, 0, arr.length-1);
}
const arr = [1, 2, 3];
console.log(countPairs(arr));
|
The Recursive approach is contributed by Vaibhav Saroj .
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...