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

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

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(N^{2})**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**1**s 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;` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

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