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++
// 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
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the
// count of valid pairs
function countPairs(arr, n)
{
// To store the required count
var cnt = 0;
// For each index pair (i, j)
for (var i = 0; i < n; i++) {
for (var 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
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;
// Function to recursively count the number of valid pairs
int countPairsRec(int arr[], int i, int j) {
// Base case: if we've compared all pairs, return 0
if (i >= j) {
return 0;
}
// Recursive case:
// If the current pair is valid, add 1 and move on to the next pair
if (arr[i] != arr[j]) {
return 1 + countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
// If the current pair is invalid, move on to the next pair without counting it
else {
return countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
}
// Wrapper function to call the recursive function
int countPairs(int arr[], int n) {
return countPairsRec(arr, 0, n-1);
}
// Driver code
int main() {
int arr[] = {1, 2, 3};
int n = sizeof(arr) / sizeof(int);
cout << countPairs(arr, n) << endl;
return 0;
}
// This code is contributed by Vaibhav Saroj
C
#include <stdio.h>
// Function to recursively count the number of valid pairs
int countPairsRec(int arr[], int i, int j) {
// Base case: if we've compared all pairs, return 0
if (i >= j) {
return 0;
}
// Recursive case:
// If the current pair is valid, add 1 and move on to the next pair
if (arr[i] != arr[j]) {
return 1 + countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
// If the current pair is invalid, move on to the next pair without counting it
else {
return countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
}
// Wrapper function to call the recursive function
int countPairs(int arr[], int n) {
return countPairsRec(arr, 0, n-1);
}
// Driver code
int main() {
int arr[] = {1, 2, 3};
int n = sizeof(arr) / sizeof(int);
printf("%d\n", countPairs(arr, n));
return 0;
}
// This code is contributed by Vaibhav Saroj
Java
/*package whatever //do not write package name here */
import java.util.*;
class Main {
// Function to recursively count the number of valid pairs
static int countPairsRec(int arr[], int i, int j) {
// Base case: if we've compared all pairs, return 0
if (i >= j) {
return 0;
}
// Recursive case:
// If the current pair is valid, add 1 and move on to the next pair
if (arr[i] != arr[j]) {
return 1 + countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
// If the current pair is invalid, move on to the next pair without counting it
else {
return countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
}
// Wrapper function to call the recursive function
static int countPairs(int arr[], int n) {
return countPairsRec(arr, 0, n-1);
}
// Driver code
public static void main(String[] args) {
int arr[] = {1, 2, 3};
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
// This code is contributed by Vaibhav Saroj
Python3
def countPairsRec(arr, i, j):
# Base case: if we've compared all pairs, return 0
if i >= j:
return 0
# Recursive case:
# If the current pair is valid, add 1 and move on to the next pair
if arr[i] != arr[j]:
return 1 + countPairsRec(arr, i + 1, j) + countPairsRec(arr, i, j - 1)
# If the current pair is invalid, move on to the next pair without counting it
else:
return countPairsRec(arr, i + 1, j) + countPairsRec(arr, i, j - 1)
# Wrapper function to call the recursive function
def countPairs(arr):
return countPairsRec(arr, 0, len(arr) - 1)
# Driver code
arr = [1, 2, 3]
print(countPairs(arr))
C#
using System;
class Program
{
// Function to recursively count the number of valid pairs
static int countPairsRec(int[] arr, int i, int j) {
// Base case: if we've compared all pairs, return 0
if (i >= j) {
return 0;
}
// Recursive case:
// If the current pair is valid, add 1 and move on to the next pair
if (arr[i] != arr[j]) {
return 1 + countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
// If the current pair is invalid, move on to the next pair without counting it
else {
return countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
}
// Wrapper function to call the recursive function
static int countPairs(int[] arr, int n) {
return countPairsRec(arr, 0, n-1);
}
// Driver code
static void Main(string[] args) {
int[] arr = {1, 2, 3};
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
// This code is contributed by Vaibhav Saroj
JavaScript
function countPairsRec(arr, i, j) {
// Base case: if we've compared all pairs, return 0
if (i >= j) {
return 0;
}
// Recursive case:
// If the current pair is valid, add 1 and move on to the next pair
if (arr[i] !== arr[j]) {
return 1 + countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
// If the current pair is invalid, move on to the next pair without counting it
else {
return countPairsRec(arr, i+1, j) + countPairsRec(arr, i, j-1);
}
}
// Wrapper function to call the recursive function
function countPairs(arr) {
return countPairsRec(arr, 0, arr.length-1);
}
// Driver code
const arr = [1, 2, 3];
console.log(countPairs(arr));
// This code is contributed by Vaibhav Saroj
The Recursive approach is contributed by Vaibhav Saroj .
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Similar Reads
Count equal element pairs in the given array
Given an array arr[] of N integers representing the lengths of the gloves, the task is to count the maximum possible pairs of gloves from the given array. Note that a glove can only pair with a same-sized glove and it can only be part of a single pair. Examples: Input: arr[] = {6, 5, 2, 3, 5, 2, 2,
8 min read
Count equal pairs from given string arrays
Given two string arrays s1[] and s2[]. The task is to find the count of pairs (s1[i], s2[j]) such that s1[i] = s2[j]. Note that an element s1[i] can only participate in a single pair. Examples: Input: s1[] = {"abc", "def"}, s2[] = {"abc", "abc"} Output: 1 Explanation: Only valid pair is (s1[0], s2[0
11 min read
Find the count of even odd pairs in a given Array
Given an array arr[], the task is to find the count even-odd pairs in the array.Examples: Input: arr[] = { 1, 2, 1, 3 } Output: 2 Explanation: The 2 pairs of the form (even, odd) are {2, 1} and {2, 3}. Input: arr[] = { 5, 4, 1, 2, 3} Output: 3 Naive Approach: Run two nested loops to get all the poss
7 min read
Count pairs formed by distinct element sub-arrays
Given an array, count number of pairs that can be formed from all possible contiguous sub-arrays containing distinct numbers. The array contains positive numbers between 0 to n-1 where n is the size of the array. Examples: Input: [1, 4, 2, 4, 3, 2] Output: 8 The subarrays with distinct elements are
7 min read
Pairs from an array that satisfy the given condition
Given an array arr[], the task is to count all the valid pairs from the array. A pair (arr[i], arr[j]) is said to be valid if func( arr[i] ) + func( arr[j] ) = func( XOR(arr[i], arr[j]) ) where func(x) returns the number of set bits in x. Examples: Input: arr[] = {2, 3, 4, 5, 6} Output: 3 (2, 4), (2
9 min read
Count pairs from given array with Bitwise OR equal to K
Given an array arr[] consisting of N positive integers and an integer K, the task is to count all pairs possible from the given array with Bitwise OR equal to K. Examples: Input: arr[] = {2, 38, 44, 29, 62}, K = 46Output: 2Explanation: Only the following two pairs are present in the array whose Bitw
5 min read
Queries to count array elements from a given range having a single set bit
Given an array arr[] consisting of N integers and a 2D array Q[][] consisting of queries of the following two types: 1 L R: Print the count of numbers from the range [L, R] having only a single set bit.2 X V: Update the array element at Xth index with V.Examples: Input: arr[] = { 12, 11, 16, 2, 32 }
15+ min read
Count Pairs from two arrays with even sum
Given two arrays A[] and B[] of N and M integers respectively. The task is to count the number of unordered pairs formed by choosing an element from array A[] and other from array B[] in such a way that their sum is an even number. Note that an element will only be a part of a single pair.Examples:
7 min read
Count pairs from two arrays having sum equal to K
Given an integer K and two arrays A1 and A2, the task is to return the total number of pairs (one element from A1 and one element from A2) with a sum equal to K. Note: Arrays can have duplicate elements. We consider every pair as different, the only constraint is, an element (of any array) can parti
6 min read
Count pairs from an array having GCD equal to the minimum element in the pair
Given an array arr[] consisting of N integers, the task is to find the number of pairs such that the GCD of any pair of array elements is the minimum element of that pair. Examples: Input: arr[ ] = {2, 3, 1, 2}Output: 4Explanation:Below are the all possible pairs from the given array: (0, 1): The GC
13 min read