Open In App

Count Strong Pairs

Last Updated : 16 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to Count of pairs that
// satisfy given condition X ^ Y = Y ^ X
int findMaxSubarraySum(int A[], int N)
{
 
    // Declaring Frequency HashMap
    unordered_map<int, int> HashMap;
 
    // Declaring track number of pairs
    int numberOfPairs = 0;
 
    // populate frquency HashMap
    for (int i = 0; i < N; i++) {
        HashMap[A[i]]++;
    }
 
    // iterating on HashMap for case 1
    for (auto& e : HashMap) {
        numberOfPairs += (e.second - 1) * e.second / 2;
    }
 
    // case 2
    numberOfPairs += HashMap[1] * HashMap[2];
 
    // returning number of pairs that satisfy conditions
    return numberOfPairs;
}
 
// Driver Code
int32_t main()
{
 
    // Input 1
    int N = 4;
    int A[] = { 3, 1, 3, 2 };
 
    // Function Call
    cout << findMaxSubarraySum(A, N) << endl;
 
    // Input 2
    int N1 = 3;
    int A1[] = { 1, 1, 1 };
 
    // Function Call
    cout << findMaxSubarraySum(A1, N1) << endl;
 
    return 0;
}


Java




import java.util.HashMap;
import java.util.Map;
 
public class Main {
 
    // Function to count pairs that satisfy the given condition X ^ Y = Y ^ X
    static int findMaxSubarraySum(int[] A, int N) {
        // Declaring Frequency HashMap
        Map<Integer, Integer> HashMap = new HashMap<>();
 
        // Declaring track number of pairs
        int numberOfPairs = 0;
 
        // Populate frequency HashMap
        for (int i = 0; i < N; i++) {
            HashMap.put(A[i], HashMap.getOrDefault(A[i], 0) + 1);
        }
 
        // Iterating on HashMap for case 1
        for (Map.Entry<Integer, Integer> entry : HashMap.entrySet()) {
            int frequency = entry.getValue();
            numberOfPairs += (frequency - 1) * frequency / 2;
        }
 
        // Case 2
        numberOfPairs += HashMap.getOrDefault(1, 0) * HashMap.getOrDefault(2, 0);
 
        // Returning the number of pairs that satisfy conditions
        return numberOfPairs;
    }
 
    // Driver Code
    public static void main(String[] args) {
        // Input 1
        int N = 4;
        int[] A = {3, 1, 3, 2};
 
        // Function Call
        System.out.println(findMaxSubarraySum(A, N));
 
        // Input 2
        int N1 = 3;
        int[] A1 = {1, 1, 1};
 
        // Function Call
        System.out.println(findMaxSubarraySum(A1, N1));
    }
}
 
// This code is contributed by shivamgupta0987654321


Python3




# Function to count pairs that satisfy given condition X ^ Y = Y ^ X
def find_max_subarray_sum(arr, n):
    # Declaring Frequency HashMap
    hashmap = {}
 
    # Declaring track number of pairs
    number_of_pairs = 0
 
    # populate frequency HashMap
    for i in range(n):
        if arr[i] in hashmap:
            hashmap[arr[i]] += 1
        else:
            hashmap[arr[i]] = 1
 
    # iterating on HashMap for case 1
    for key, value in hashmap.items():
        number_of_pairs += (value - 1) * value // 2
 
    # case 2
    number_of_pairs += hashmap.get(1, 0) * hashmap.get(2, 0)
 
    # returning number of pairs that satisfy conditions
    return number_of_pairs
 
 
# Driver Code
if __name__ == "__main__":
    # Input 1
    N = 4
    A = [3, 1, 3, 2]
 
    # Function Call
    print(find_max_subarray_sum(A, N))
 
    # Input 2
    N1 = 3
    A1 = [1, 1, 1]
 
    # Function Call
    print(find_max_subarray_sum(A1, N1))


C#




using System;
using System.Collections.Generic;
 
class Program
{
    // Function to count pairs that satisfy the given condition X ^ Y = Y ^ X
    static int FindMaxSubarraySum(int[] arr, int n)
    {
        // Declaring Frequency Dictionary
        Dictionary<int, int> hashmap = new Dictionary<int, int>();
 
        // Declaring track number of pairs
        int numberOfPairs = 0;
 
        // Populate frequency Dictionary
        for (int i = 0; i < n; i++)
        {
            if (hashmap.ContainsKey(arr[i]))
            {
                hashmap[arr[i]] += 1;
            }
            else
            {
                hashmap[arr[i]] = 1;
            }
        }
 
        // Iterating on Dictionary for case 1
        foreach (var entry in hashmap)
        {
            int value = entry.Value;
            numberOfPairs += (value - 1) * value / 2;
        }
 
        // Case 2
        numberOfPairs += hashmap.GetValueOrDefault(1, 0) * hashmap.GetValueOrDefault(2, 0);
 
        // Returning number of pairs that satisfy conditions
        return numberOfPairs;
    }
 
    // Driver Code
    static void Main()
    {
        // Input 1
        int N = 4;
        int[] A = { 3, 1, 3, 2 };
 
        // Function Call
        Console.WriteLine(FindMaxSubarraySum(A, N));
 
        // Input 2
        int N1 = 3;
        int[] A1 = { 1, 1, 1 };
 
        // Function Call
        Console.WriteLine(FindMaxSubarraySum(A1, N1));
    }
}


Javascript




// Javascript code to implement the approach
 
// Function to count pairs that satisfy the given condition X ^ Y = Y ^ X
function findMaxSubarraySum(A) {
    // Declaring Frequency HashMap
    let HashMap = {};
 
    // Declaring track number of pairs
    let numberOfPairs = 0;
 
    // Populate frequency HashMap
    for (let i = 0; i < A.length; i++) {
        HashMap[A[i]] = (HashMap[A[i]] || 0) + 1;
    }
 
    // Iterating on HashMap for case 1
    for (let key in HashMap) {
        numberOfPairs += (HashMap[key] - 1) * HashMap[key] / 2;
    }
 
    // Case 2
    numberOfPairs += (HashMap[1] || 0) * (HashMap[2] || 0);
 
    // Returning the number of pairs that satisfy conditions
    return numberOfPairs;
}
 
// Driver Code
// Input 1
let A = [3, 1, 3, 2];
 
// Function Call and Output
console.log(findMaxSubarraySum(A));
 
// Input 2
let A1 = [1, 1, 1];
 
// Function Call and Output
console.log(findMaxSubarraySum(A1));


Output

2
3







Time Complexity: O(N)
Auxiliary Space: O(N),where N is the number of elements in array.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads