Count Strong Pairs
Last Updated :
16 Jan, 2024
Given array A[] of size N, each element represents 2A[i] for all i from 1 to N. The Task for this problem is to find the number of Strong pairs. A Strong pair is defined as a pair (i, j) such that 2A[i] ^ 2A[j] = 2A[j] ^ 2A[i] for i < j.
Note: operator ^ denotes the exponent.
Examples:
Input: A[] = {3, 1, 3, 2}
Output: 2
Explanation:
We have two pairs that satisfy above condition
- For i = 1 and j = 3, A[i] = 3 and A[j] = 3. we have 23 ^ 23 = 23 ^ 23 i.e 88=88is true
- For i = 2 and j = 4, A[i] = 1 and A[j] = 2. we have 21 ^ 22 = 22 ^ 21 i.e 24(=16) = 42(=16) is also true.
Input: A[] = {1, 1, 1}
Output: 3
Explanation:
We have three pairs that satisfy above condition
- For i = 1 and j = 2, A[i] = 1 and A[j] = 1. we have 21 ^ 21 = 21 ^ 21
- For i = 2 and j = 3, A[i] = 1 and A[j] = 1. we have 21 ^ 21 = 21 ^ 21
- For i = 1 and j = 3, A[i] = 1 and A[j] = 1. we have 21 ^ 21 = 21 ^ 21
Approach: To solve the problem follow the below idea:
- Case 1: a ^ a = a ^ a when A[i] = A[j]
to count such pairs we will have A[i] and A[j] equal in that case we can take n distinct elements in 2 ways which is nC2.
- Case 2: 2 ^ 4 = 4 ^ 2 when A[i] = 1 and A[j] = 2
number of pairs will be calculated by multiplication rule multiplying frequency of 2 and 4.
- We can solve this using HashMap to count the frequency of each element and handle both case.
Below are the steps for the above approach:
- Declaring HashMap[]
- Declaring variable numberOfpairs = 0 for counting number of pairs
- Iterate over all N elements and populate the HashMap[]
- Iterate over HashMap[] and add answer for first case in numberOfPairs.
- First case can be calculated by possible ways to chose two same elements in the array which can be easily done using HashMap.
- then for second case count frequency of element 2 and 4 and multiply them.
- add them in numberOfPairs variable.
- and return numberOfPairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaxSubarraySum( int A[], int N)
{
unordered_map< int , int > HashMap;
int numberOfPairs = 0;
for ( int i = 0; i < N; i++) {
HashMap[A[i]]++;
}
for ( auto & e : HashMap) {
numberOfPairs += (e.second - 1) * e.second / 2;
}
numberOfPairs += HashMap[1] * HashMap[2];
return numberOfPairs;
}
int32_t main()
{
int N = 4;
int A[] = { 3, 1, 3, 2 };
cout << findMaxSubarraySum(A, N) << endl;
int N1 = 3;
int A1[] = { 1, 1, 1 };
cout << findMaxSubarraySum(A1, N1) << endl;
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class Main {
static int findMaxSubarraySum( int [] A, int N) {
Map<Integer, Integer> HashMap = new HashMap<>();
int numberOfPairs = 0 ;
for ( int i = 0 ; i < N; i++) {
HashMap.put(A[i], HashMap.getOrDefault(A[i], 0 ) + 1 );
}
for (Map.Entry<Integer, Integer> entry : HashMap.entrySet()) {
int frequency = entry.getValue();
numberOfPairs += (frequency - 1 ) * frequency / 2 ;
}
numberOfPairs += HashMap.getOrDefault( 1 , 0 ) * HashMap.getOrDefault( 2 , 0 );
return numberOfPairs;
}
public static void main(String[] args) {
int N = 4 ;
int [] A = { 3 , 1 , 3 , 2 };
System.out.println(findMaxSubarraySum(A, N));
int N1 = 3 ;
int [] A1 = { 1 , 1 , 1 };
System.out.println(findMaxSubarraySum(A1, N1));
}
}
|
Python3
def find_max_subarray_sum(arr, n):
hashmap = {}
number_of_pairs = 0
for i in range (n):
if arr[i] in hashmap:
hashmap[arr[i]] + = 1
else :
hashmap[arr[i]] = 1
for key, value in hashmap.items():
number_of_pairs + = (value - 1 ) * value / / 2
number_of_pairs + = hashmap.get( 1 , 0 ) * hashmap.get( 2 , 0 )
return number_of_pairs
if __name__ = = "__main__" :
N = 4
A = [ 3 , 1 , 3 , 2 ]
print (find_max_subarray_sum(A, N))
N1 = 3
A1 = [ 1 , 1 , 1 ]
print (find_max_subarray_sum(A1, N1))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int FindMaxSubarraySum( int [] arr, int n)
{
Dictionary< int , int > hashmap = new Dictionary< int , int >();
int numberOfPairs = 0;
for ( int i = 0; i < n; i++)
{
if (hashmap.ContainsKey(arr[i]))
{
hashmap[arr[i]] += 1;
}
else
{
hashmap[arr[i]] = 1;
}
}
foreach ( var entry in hashmap)
{
int value = entry.Value;
numberOfPairs += (value - 1) * value / 2;
}
numberOfPairs += hashmap.GetValueOrDefault(1, 0) * hashmap.GetValueOrDefault(2, 0);
return numberOfPairs;
}
static void Main()
{
int N = 4;
int [] A = { 3, 1, 3, 2 };
Console.WriteLine(FindMaxSubarraySum(A, N));
int N1 = 3;
int [] A1 = { 1, 1, 1 };
Console.WriteLine(FindMaxSubarraySum(A1, N1));
}
}
|
Javascript
function findMaxSubarraySum(A) {
let HashMap = {};
let numberOfPairs = 0;
for (let i = 0; i < A.length; i++) {
HashMap[A[i]] = (HashMap[A[i]] || 0) + 1;
}
for (let key in HashMap) {
numberOfPairs += (HashMap[key] - 1) * HashMap[key] / 2;
}
numberOfPairs += (HashMap[1] || 0) * (HashMap[2] || 0);
return numberOfPairs;
}
let A = [3, 1, 3, 2];
console.log(findMaxSubarraySum(A));
let A1 = [1, 1, 1];
console.log(findMaxSubarraySum(A1));
|
Time Complexity: O(N)
Auxiliary Space: O(N),where N is the number of elements in array.
Share your thoughts in the comments
Please Login to comment...