Count the number of pair in Array with given XOR property A[i]^A[j] = i^j
Last Updated :
10 Apr, 2023
Given an array A[] with N elements, the task is to find the total number of pairs possible with A[i] ^ A[j] = i ^ j, considering base-indexing 1 and (i, j) are distinct.
Examples:
Input: arr[] = {4, 2, 3, 1}
Output: 2
Explanation: The array has 2 pairs: (4, 1) and (2, 3)
- For (4, 1), 4^1 = 5 and their index 1^4 = 5
- Similarly, for (2, 3), 2^3 = 1 and their index 2^3 = 1
Approach: This can be solved with the following idea:
Applying basic XOR principles, we can find that
Given: A[i]^A[j] = i^j
- A[i] ^ A[j] ^ A[j] = i ^ j ^ A[j]
- A[i] ^ i = i ^ i ^ j ^ A[j]
- A[i] ^ i = A[j] ^ j
Thus, we basically need to find the total pair of elements possible with the same value of A[i]^i, where i is the index of the element in the array.
Steps involved in the implementation of code:
- Calculate the XOR of arr[i] ^ (i). Store it in the map.
- Increase the count of pairs by checking the frequency of each key and applying (n * (n-1)) /2.
Below is the Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getPairCount( int arr[], int n)
{
int count = 0;
map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[(arr[i] ^ (i + 1))]++;
for ( auto itr : mp)
count += ((itr.second) * (itr.second - 1)) / 2;
return count;
}
int main()
{
int arr[] = { 4, 2, 3, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << getPairCount(arr, n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int getPairCount( int [] arr, int n) {
int count = 0 ;
Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++)
mp.put(arr[i] ^ (i + 1 ), mp.getOrDefault(arr[i] ^ (i + 1 ), 0 ) + 1 );
for (Map.Entry<Integer, Integer> entry : mp.entrySet())
count += ((entry.getValue()) * (entry.getValue() - 1 )) / 2 ;
return count;
}
public static void main(String[] args) {
int [] arr = { 4 , 2 , 3 , 1 };
int n = arr.length;
System.out.println(getPairCount(arr, n));
}
}
|
Python3
from collections import defaultdict
def getPairCount(arr, n):
count = 0
mp = defaultdict( int )
for i in range (n):
mp[(arr[i] ^ (i + 1 ))] + = 1
for itr in mp:
count + = ((mp[itr]) * (mp[itr] - 1 )) / / 2
return count
arr = [ 4 , 2 , 3 , 1 ]
n = len (arr)
print (getPairCount(arr, n))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int getPairCount( int [] arr, int n)
{
int count = 0;
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
int key = arr[i] ^ (i + 1);
if (mp.ContainsKey(key))
{
mp[key]++;
}
else
{
mp[key] = 1;
}
}
foreach (KeyValuePair< int , int > kvp in mp)
{
count += (kvp.Value * (kvp.Value - 1)) / 2;
}
return count;
}
static void Main( string [] args)
{
int [] arr = { 4, 2, 3, 1 };
int n = arr.Length;
Console.WriteLine(getPairCount(arr, n));
}
}
|
Javascript
function getPairCount(arr) {
let count = 0;
const mp = new Map();
for (let i = 0; i < arr.length; i++) {
mp.set((arr[i] ^ (i + 1)), (mp.get((arr[i] ^ (i + 1))) || 0) + 1);
}
for (let [key, value] of mp) {
count += ((value) * (value - 1)) / 2;
}
return count;
}
const arr = [4, 2, 3, 1];
const n = arr.length;
console.log(getPairCount(arr, n));
|
Time Complexity: O(N), Since we have to run the loop only once.
Auxiliary Space: O(N), Temporary mapping of A[i]^i values.
Share your thoughts in the comments
Please Login to comment...