Open In App

# Count pairs from an array having product of their sum and difference equal to 1

Given an array arr[] of size N, the task is to count possible pairs of array elements (arr[i], arr[j]) such that (arr[i] + arr[j]) * (arr[i] – arr[j]) is 1.

Examples:

Input: arr[] = {3, 1, 1, 0}
Output:
Explanation:
The two possible pairs are:

1. (arr + arr) * (arr – arr) = 1
2. (arr + arr) * (arr – arr) = 1

Input: arr[] = {12, 0, 1, 1, 14, 0, 9, 0}
Output:
Explanation:
The four possible pairs are as follows:

1. (3, 6): (arr + arr) * (arr – arr) = 1
2. (4, 6): (arr + arr) * (arr – arr) = 1
3. (3, 8): (arr + arr) * (arr – arr) = 1
4. (3, 6): (arr + arr) * (arr – arr) = 1

Naive Approach: The simplest approach is to traverse the array and generate all possible pairs from the given array and count those pairs whose product of sum and difference is 1. Finally, print the final count obtained after completing the above steps.

Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: The given condition can be expressed for any pair of array elements (arr[i], arr[j]) as:

(arr[i] + arr[j]) * (arr[i] – arr[j]) = 1
=> (arr[i]2 – arr[j]2) = 1

Therefore, it can be concluded that the required conditions can be satisfied only by the pairs arr[i] = 1 and arr[j] = 0 and i < j. Follow the steps below to solve the problem:

• Initialize two variables, oneCount and desiredPairs to store the count of 1s and required pairs respectively.
• Traverse the given array and check the following:
• If arr[i] = 1: Increment oneCount by 1.
• If arr[i] = 0: Add the count of 1s obtained so far to desiredPairs.
• After completing the above steps, print the value of desiredPairs as the required answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to count the desired``// number of pairs``int` `countPairs(``int` `arr[], ``int` `n)``{``    ``// Initialize oneCount``    ``int` `oneCount = 0;` `    ``// Initialize the desiredPair``    ``int` `desiredPair = 0;` `    ``// Traverse the given array``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// If 1 is encountered``        ``if` `(arr[i] == 1) {``            ``oneCount++;``        ``}` `        ``// If 0 is encountered``        ``if` `(arr[i] == 0) {` `            ``// Update count of pairs``            ``desiredPair += oneCount;``        ``}``    ``}` `    ``// Return the final count``    ``return` `desiredPair;``}` `// Driver Code``int` `main()``{``    ``// Given array arr[]``    ``int` `arr[] = { 3, 1, 1, 0 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``// Function Call``    ``cout << countPairs(arr, N);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;` `class` `GFG{` `// Function to count the desired``// number of pairs``static` `int` `countPairs(``int` `arr[], ``int` `n)``{``    ` `    ``// Initialize oneCount``    ``int` `oneCount = ``0``;` `    ``// Initialize the desiredPair``    ``int` `desiredPair = ``0``;` `    ``// Traverse the given array``    ``for``(``int` `i = ``0``; i < n; i++)``    ``{``        ` `        ``// If 1 is encountered``        ``if` `(arr[i] == ``1``)``        ``{``            ``oneCount++;``        ``}` `        ``// If 0 is encountered``        ``if` `(arr[i] == ``0``)``        ``{``            ` `            ``// Update count of pairs``            ``desiredPair += oneCount;``        ``}``    ``}` `    ``// Return the final count``    ``return` `desiredPair;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given array arr[]``    ``int` `arr[] = { ``3``, ``1``, ``1``, ``0` `};``    ``int` `N = arr.length;` `    ``// Function call``    ``System.out.println(countPairs(arr, N));``}``}` `// This code is contributed by sanjoy_62`

## Python3

 `# Python3 program for the above approach` `# Function to count the desired``# number of pairs``def` `countPairs(arr, n):``    ` `    ``# Initialize oneCount``    ``oneCount ``=` `0` `    ``# Initialize the desiredPair``    ``desiredPair ``=` `0` `    ``# Traverse the given array``    ``for` `i ``in` `range``(n):` `        ``# If 1 is encountered``        ``if` `(arr[i] ``=``=` `1``):``            ``oneCount ``+``=` `1` `        ``# If 0 is encountered``        ``if` `(arr[i] ``=``=` `0``):` `            ``# Update count of pairs``            ``desiredPair ``+``=` `oneCount` `    ``# Return the final count``    ``return` `desiredPair` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given array arr[]``    ``arr ``=` `[ ``3``, ``1``, ``1``, ``0` `]``    ``N ``=` `len``(arr)``    ` `    ``# Function call``    ``print``(countPairs(arr, N))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to count the desired``// number of pairs``static` `int` `countPairs(``int` `[]arr, ``int` `n)``{``    ` `    ``// Initialize oneCount``    ``int` `oneCount = 0;` `    ``// Initialize the desiredPair``    ``int` `desiredPair = 0;` `    ``// Traverse the given array``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ` `        ``// If 1 is encountered``        ``if` `(arr[i] == 1)``        ``{``            ``oneCount++;``        ``}` `        ``// If 0 is encountered``        ``if` `(arr[i] == 0)``        ``{``            ` `            ``// Update count of pairs``            ``desiredPair += oneCount;``        ``}``    ``}` `    ``// Return the readonly count``    ``return` `desiredPair;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ` `    ``// Given array []arr``    ``int` `[]arr = { 3, 1, 1, 0 };``    ``int` `N = arr.Length;` `    ``// Function call``    ``Console.WriteLine(countPairs(arr, N));``}``}` `// This code is contributed by gauravrajput1`

## Javascript

 ``

Output:

`2`

Time Complexity: O(N)
Auxiliary Space: O(N)