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 :2Explanation:

(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

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

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `#define MAXN 100005` `// Function to count required` `// number of pairs` `int` `countPairs(` `int` `arr[], ` `int` `N)` `{` ` ` `// Stores count of pairs` ` ` `int` `desiredPairs = 0;` ` ` `// Initialize hash with 0` ` ` `int` `hash[MAXN] = { 0 };` ` ` `// Count frequency of each element` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `hash[` `abs` `(arr[i])]++;` ` ` `}` ` ` `// Calculate desired number of pairs` ` ` `for` `(` `int` `i = 0; i < MAXN; i++) {` ` ` `desiredPairs` ` ` `+= ((hash[i]) * (hash[i] - 1)) / 2;` ` ` `}` ` ` `// Print desired pairs` ` ` `cout << desiredPairs;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given arr[]` ` ` `int` `arr[] = { 2, -2, 1, 1 };` ` ` `// Size of the array` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `// Function Call` ` ` `countPairs(arr, N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.util.Arrays; ` `class` `GFG{` ` ` `static` `int` `MAXN = ` `100005` `;` `// Function to count required` `// number of pairs` `static` `void` `countPairs(` `int` `arr[], ` `int` `N)` `{` ` ` ` ` `// Stores count of pairs` ` ` `int` `desiredPairs = ` `0` `;` ` ` `// Initialize hash with 0` ` ` `int` `hash[] = ` `new` `int` `[MAXN];` ` ` `Arrays.fill(hash, ` `0` `);` ` ` `// Count frequency of each element` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{` ` ` `hash[Math.abs(arr[i])]++;` ` ` `}` ` ` `// Calculate desired number of pairs` ` ` `for` `(` `int` `i = ` `0` `; i < MAXN; i++)` ` ` `{` ` ` `desiredPairs += ((hash[i]) * ` ` ` `(hash[i] - ` `1` `)) / ` `2` `;` ` ` `}` ` ` `// Print desired pairs` ` ` `System.out.print(desiredPairs);` `} ` ` ` `// Driver Code` `public` `static` `void` `main (String[] args) ` `{` ` ` ` ` `// Given arr[]` ` ` `int` `arr[] = { ` `2` `, -` `2` `, ` `1` `, ` `1` `};` ` ` `// Size of the array` ` ` `int` `N = arr.length;` ` ` `// Function call` ` ` `countPairs(arr, N);` `}` `}` `// This code is contributed by code_hunt` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for ` `# the above approach` `MAXN ` `=` `100005` `# Function to count required` `# number of pairs` `def` `countPairs(arr, N):` ` ` `# Stores count of pairs` ` ` `desiredPairs ` `=` `0` ` ` `# Initialize hash with 0` ` ` `hash` `=` `[` `0` `] ` `*` `MAXN` ` ` `# Count frequency of ` ` ` `# each element` ` ` `for` `i ` `in` `range` `(N):` ` ` `hash` `[` `abs` `(arr[i])] ` `+` `=` `1` ` ` ` ` `# Calculate desired number ` ` ` `# of pairs` ` ` `for` `i ` `in` `range` `(MAXN):` ` ` `desiredPairs ` `+` `=` `((` `hash` `[i]) ` `*` ` ` `(` `hash` `[i] ` `-` `1` `)) ` `/` `/` `2` ` ` ` ` `# Print desired pairs` ` ` `print` `(desiredPairs)` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `# Given arr[]` ` ` `arr ` `=` `[` `2` `, ` `-` `2` `, ` `1` `, ` `1` `]` ` ` `# Size of the array` ` ` `N ` `=` `len` `(arr)` ` ` `# Function Call` ` ` `countPairs(arr, N)` `# This code is contributed by Chitranayal` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` ` ` `static` `int` `MAXN = 100005;` `// Function to count required` `// number of pairs` `static` `void` `countPairs(` `int` `[]arr, ` `int` `N)` `{` ` ` ` ` `// Stores count of pairs` ` ` `int` `desiredPairs = 0;` ` ` `// Initialize hash with 0` ` ` `int` `[]hash = ` `new` `int` `[MAXN];` ` ` `// Count frequency of each element` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{` ` ` `hash[Math.Abs(arr[i])]++;` ` ` `}` ` ` `// Calculate desired number of pairs` ` ` `for` `(` `int` `i = 0; i < MAXN; i++)` ` ` `{` ` ` `desiredPairs += ((hash[i]) * ` ` ` `(hash[i] - 1)) / 2;` ` ` `}` ` ` `// Print desired pairs` ` ` `Console.Write(desiredPairs);` `} ` ` ` `// Driver Code` `public` `static` `void` `Main(String[] args) ` `{` ` ` ` ` `// Given []arr` ` ` `int` `[]arr = { 2, -2, 1, 1 };` ` ` `// Size of the array` ` ` `int` `N = arr.Length;` ` ` `// Function call` ` ` `countPairs(arr, N);` `}` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count pairs from an array having product of their sum and difference equal to 1
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count the pairs in an array such that the difference between them and their indices is equal
- Count of pairs from Array with sum equal to twice their bitwise AND
- Number of pairs in an array having sum equal to product
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count of Substrings having Sum equal to their Length
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
- Split array into two subsequences having minimum count of pairs with sum equal to X
- Count of all possible pairs having sum of LCM and GCD equal to N
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count pairs from two arrays having sum equal to K

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.