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[1] + arr[3]) * (arr[1] – arr[3]) = 1
  2. (arr[2] + arr[3]) * (arr[2] – arr[3]) = 1

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

  1. (3, 6): (arr[3] + arr[6]) * (arr[3] – arr[6]) = 1
  2. (4, 6): (arr[4] + arr[6]) * (arr[4] – arr[6]) = 1
  3. (3, 8): (arr[3] + arr[8]) * (arr[3] – arr[8]) = 1
  4. (3, 6): (arr[4] + arr[8]) * (arr[4] – arr[8]) = 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
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[0]);
    // Function Call
    cout << countPairs(arr, N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output: 

2













 

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

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.