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: 2
Explanation:
The two possible pairs are:
- (arr[1] + arr[3]) * (arr[1] – arr[3]) = 1
- (arr[2] + arr[3]) * (arr[2] – arr[3]) = 1
Input: arr[] = {12, 0, 1, 1, 14, 0, 9, 0}
Output: 4
Explanation:
The four possible pairs are as follows:
- (3, 6): (arr[3] + arr[6]) * (arr[3] – arr[6]) = 1
- (4, 6): (arr[4] + arr[6]) * (arr[4] – arr[6]) = 1
- (3, 8): (arr[3] + arr[8]) * (arr[3] – arr[8]) = 1
- (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++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr[], int n)
{
int oneCount = 0;
int desiredPair = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == 1) {
oneCount++;
}
if (arr[i] == 0) {
desiredPair += oneCount;
}
}
return desiredPair;
}
int main()
{
int arr[] = { 3, 1, 1, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int countPairs( int arr[], int n)
{
int oneCount = 0 ;
int desiredPair = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] == 1 )
{
oneCount++;
}
if (arr[i] == 0 )
{
desiredPair += oneCount;
}
}
return desiredPair;
}
public static void main(String[] args)
{
int arr[] = { 3 , 1 , 1 , 0 };
int N = arr.length;
System.out.println(countPairs(arr, N));
}
}
|
Python3
def countPairs(arr, n):
oneCount = 0
desiredPair = 0
for i in range (n):
if (arr[i] = = 1 ):
oneCount + = 1
if (arr[i] = = 0 ):
desiredPair + = oneCount
return desiredPair
if __name__ = = '__main__' :
arr = [ 3 , 1 , 1 , 0 ]
N = len (arr)
print (countPairs(arr, N))
|
C#
using System;
class GFG{
static int countPairs( int []arr, int n)
{
int oneCount = 0;
int desiredPair = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == 1)
{
oneCount++;
}
if (arr[i] == 0)
{
desiredPair += oneCount;
}
}
return desiredPair;
}
public static void Main(String[] args)
{
int []arr = { 3, 1, 1, 0 };
int N = arr.Length;
Console.WriteLine(countPairs(arr, N));
}
}
|
Javascript
<script>
function countPairs(arr, n)
{
let oneCount = 0;
let desiredPair = 0;
for (let i = 0; i < n; i++)
{
if (arr[i] == 1)
{
oneCount++;
}
if (arr[i] == 0)
{
desiredPair += oneCount;
}
}
return desiredPair;
}
let arr = [ 3, 1, 1, 0 ];
let N = arr.length;
document.write(countPairs(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)