Open In App

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

Last Updated : 28 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




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


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




<script>
 
// Javascript program for the above approach
 
// Function to count the desired
// number of pairs
function countPairs(arr, n)
{
      
    // Initialize oneCount
    let oneCount = 0;
  
    // Initialize the desiredPair
    let desiredPair = 0;
  
    // Traverse the given array
    for(let 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
 
// Given array arr[]
let arr = [ 3, 1, 1, 0 ];
let N = arr.length;
 
// Function call
document.write(countPairs(arr, N));
 
// This code is contributed by target_2
 
</script>


Output: 

2

 

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads