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 0.
Examples:
Input: arr[] = {2, -2, 1, 1}
Output : 2
Explanation:
(arr[0] + arr[1]) * (arr[0] – arr[1]) = 0
(arr[3] + arr[4]) * (arr[3] – arr[4]) = 0
Input: arr[] = {5, 9, -9, -9}
Output : 3
Naive approach:
Generate all the pairs and for each pair check if the given condition holds true, for all such valid pair increment the count.
- Initialise a variable count for storing the answer.
- Generate all the pairs
- Check if the given condition holds true (i.e, arr[i] + arr[j]) * (arr[i] – arr[j]) is 0)
- Increment the count by 1.
- Print the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countPairs( int arr[], int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
if ((arr[i] + arr[j]) * (arr[i] - arr[j])
== 0) {
count++;
}
}
}
cout << count << endl;
}
int main()
{
int arr[] = { 2, -2, 1, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
countPairs(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void countPairs( int [] arr, int N)
{
int count = 0 ;
for ( int i = 0 ; i < N; i++) {
for ( int j = i + 1 ; j < N; j++) {
if ((arr[i] + arr[j]) * (arr[i] - arr[j])
== 0 ) {
count++;
}
}
}
System.out.println(count);
}
public static void main(String[] args)
{
int [] arr = { 2 , - 2 , 1 , 1 };
int N = arr.length;
countPairs(arr, N);
}
}
|
Python3
def countPairs(arr, N):
count = 0
for i in range (N):
for j in range (i + 1 , N):
if (arr[i] + arr[j]) * (arr[i] - arr[j]) = = 0 :
count + = 1
print (count)
arr = [ 2 , - 2 , 1 , 1 ]
N = len (arr)
countPairs(arr, N)
|
C#
using System;
public class Gfg {
static void Main( string [] args)
{
int [] arr = { 2, -2, 1, 1 };
int N = arr.Length;
countPairs(arr, N);
}
static void countPairs( int [] arr, int N)
{
int count = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
if ((arr[i] + arr[j]) * (arr[i] - arr[j])
== 0) {
count++;
}
}
}
Console.WriteLine(count);
}
}
|
Javascript
function countPairs(arr, N)
{
let count = 0;
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N; j++) {
if ((arr[i] + arr[j]) * (arr[i] - arr[j])
== 0) {
count++;
}
}
}
document.write(count);
}
let arr = [2, -2, 1, 1 ];
let N = arr.length;
countPairs(arr, N);
|
Time Complexity: O(n*n)
Auxiliary Space: O(1)
Approach: It can be observed that the equation (arr[i] + arr[j]) * (arr[i] – arr[j]) = 0 can be reduced to arr[i]2 = arr[j]2. Therefore, the task reduces to counting pairs having absolute value equal. Follow the steps below to solve the problem:
- Initialize an array hash[] to store the frequency of the absolute value of each array element.
- Calculate the count of pairs by adding (hash[x] * (hash[x] – 1))/ 2 for every array distinct absolute values.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
int countPairs( int arr[], int N)
{
int desiredPairs = 0;
int hash[MAXN] = { 0 };
for ( int i = 0; i < N; i++) {
hash[ abs (arr[i])]++;
}
for ( int i = 0; i < MAXN; i++) {
desiredPairs
+= ((hash[i]) * (hash[i] - 1)) / 2;
}
cout << desiredPairs;
}
int main()
{
int arr[] = { 2, -2, 1, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
countPairs(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG{
static int MAXN = 100005 ;
static void countPairs( int arr[], int N)
{
int desiredPairs = 0 ;
int hash[] = new int [MAXN];
Arrays.fill(hash, 0 );
for ( int i = 0 ; i < N; i++)
{
hash[(Math.abs(arr[i]))]++;
}
for ( int i = 0 ; i < MAXN; i++)
{
desiredPairs += ((hash[i]) *
(hash[i] - 1 )) / 2 ;
}
System.out.print(desiredPairs);
}
public static void main (String[] args)
{
int arr[] = { 2 , - 2 , 1 , 1 };
int N = arr.length;
countPairs(arr, N);
}
}
|
Python3
MAXN = 100005
def countPairs(arr, N):
desiredPairs = 0
hash = [ 0 ] * MAXN
for i in range (N):
hash [ abs (arr[i])] + = 1
for i in range (MAXN):
desiredPairs + = (( hash [i]) *
( hash [i] - 1 )) / / 2
print (desiredPairs)
if __name__ = = "__main__" :
arr = [ 2 , - 2 , 1 , 1 ]
N = len (arr)
countPairs(arr, N)
|
C#
using System;
class GFG{
static int MAXN = 100005;
static void countPairs( int []arr, int N)
{
int desiredPairs = 0;
int []hash = new int [MAXN];
for ( int i = 0; i < N; i++)
{
hash[(Math.Abs(arr[i]))]++;
}
for ( int i = 0; i < MAXN; i++)
{
desiredPairs += ((hash[i]) *
(hash[i] - 1)) / 2;
}
Console.Write(desiredPairs);
}
public static void Main(String[] args)
{
int []arr = { 2, -2, 1, 1 };
int N = arr.Length;
countPairs(arr, N);
}
}
|
Javascript
<script>
function countPairs(arr, N)
{
let desiredPairs = 0;
let hash = new Uint8Array(9100005);
for (let i = 0; i < N; i++) {
hash[(Math.abs(arr[i]))]++;
}
for (let i = 0; i < 9100005; i++) {
desiredPairs
+= ((hash[i]) * (hash[i] - 1)) / 2;
}
document.write(desiredPairs);
}
let arr = [2, -2, 1, 1];
let N = arr.length;
countPairs(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)