Given an array arr[] consisting of N integers, the task is to count the number of valid pairs (i, j) such that arr[i] + arr[j] = arr[i] / arr[j].
Examples:
Input: arr[] = {-4, -3, 0, 2, 1}
Output: 1
Explanation: The only possible pair is (0, 3) which satisfies the condition ( -4 + 2 = -4 / 2 (= -2) ).
Input: arr[] = {1, 2, 3, 4, 5}
Output: 0
Naive Approach: The simple approach is to generate all possible pairs of the given array and count the number of pairs whose sum is equal to their division. After checking, all the pairs print the final count of possible pairs.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int countPairs( int a[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (a[j] != 0
&& a[i] % a[j] == 0) {
if ((a[i] + a[j])
== (a[i] / a[j]))
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { -4, -3, 0, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countPairs( int a[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
if (a[j] != 0
&& a[i] % a[j] == 0 ) {
if ((a[i] + a[j])
== (a[i] / a[j]))
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { - 4 , - 3 , 0 , 2 , 1 };
int N = arr.length;
System.out.print(countPairs(arr, N));
}
}
|
Python3
def countPairs(a, n):
count = 0
for i in range (n):
for j in range (i + 1 , n):
if (a[j] ! = 0 and a[i] % a[j] = = 0 ):
if ((a[i] + a[j]) = = (a[i] / / a[j])):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ - 4 , - 3 , 0 , 2 , 1 ]
N = len (arr)
print (countPairs(arr, N))
|
C#
using System;
class GFG {
static int countPairs( int [] a, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if (a[j] != 0
&& a[i] % a[j] == 0)
{
if ((a[i] + a[j])
== (a[i] / a[j]))
count++;
}
}
}
return count;
}
static void Main() {
int [] arr = { -4, -3, 0, 2, 1 };
int N = arr.Length;
Console.WriteLine(countPairs(arr, N));
}
}
|
Javascript
<script>
function countPairs(a, n)
{
var count = 0;
for ( var i = 0; i < n; i++) {
for ( var j = i + 1; j < n; j++) {
if (a[j] != 0
&& a[i] % a[j] == 0) {
if ((a[i] + a[j])
== (a[i] / a[j]))
count++;
}
}
}
return count;
}
var arr = [-4, -3, 0, 2, 1 ];
var N = arr.length;
document.write( countPairs(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by simplifying the given expression and using a Map to count the number of pairs that satisfy the below-simplified condition:
Suppose X and Y are numbers present in indices i and j, then the condition that needs to be satisfied is:
=> X + Y = X/Y
=> X = Y 2/(1 – Y)
Follow the steps below to solve the above problem:
- Initialize a variable, say, count, to store the count of all possible pairs satisfying the required condition.
- Initialize a Map to store the frequencies of values of the above expression obtained for each array element.
- Traverse the given array using the variable i and perform the following steps:
- If arr[i] is not equal to 1 and 0, then calculate arr[i] 2/(1 – arr[i]), say X.
- Add the frequency of X in the Map to count.
- Increase the frequency of arr[i] by 1 in the Map.
- After completing the above steps, print the value of the count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int a[], int n)
{
int count = 0;
map< double , int > mp;
for ( int i = 0; i < n; i++) {
int y = a[i];
if (y != 0 && y != 1) {
double x = ((y * 1.0)
/ (1 - y))
* y;
count += mp[x];
}
mp[y]++;
}
return count;
}
int main()
{
int arr[] = { -4, -3, 0, 2, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countPairs( int a[], int n)
{
int count = 0 ;
HashMap<Double, Integer> mp = new HashMap<Double, Integer>();
for ( int i = 0 ; i < n; i++)
{
double y = a[i];
if (y != 0 && y != 1 )
{
double x = ((y * 1.0 )
/ ( 1 - y))
* y;
if (mp.containsKey(x))
count += mp.get(x);
}
if (mp.containsKey(y)){
mp.put(y, mp.get(y)+ 1 );
}
else {
mp.put(y, 1 );
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { - 4 , - 3 , 0 , 2 , 1 };
int N = arr.length;
System.out.print(countPairs(arr, N));
}
}
|
Python3
def countPairs(a, n) :
count = 0
mp = {}
for i in range (n):
y = a[i]
if (y ! = 0 and y ! = 1 ) :
x = (((y * 1.0 )
/ / ( 1 - y))
* y)
count + = mp.get(x, 0 )
mp[y] = mp.get(y, 0 ) + 1
return count
arr = [ - 4 , - 3 , 0 , 2 , 1 ]
N = len (arr)
print (countPairs(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countPairs( int [] a, int n)
{
int count = 0;
Dictionary< double , int > mp
= new Dictionary< double , int >();
for ( int i = 0; i < n; i++) {
int y = a[i];
if (y != 0 && y != 1) {
double x = ((y * 1.0) / (1 - y)) * y;
if (!mp.ContainsKey(x))
mp[x] = 0;
count += mp[x];
}
if (!mp.ContainsKey(y))
mp[y] = 0;
mp[y]++;
}
return count;
}
public static void Main()
{
int [] arr = { -4, -3, 0, 2, 1 };
int N = arr.Length;
Console.Write(countPairs(arr, N));
}
}
|
Javascript
<script>
function countPairs(a, n)
{
let count = 0;
let mp = new Map();
for (let i = 0; i < n; i++)
{
let y = a[i];
if (y != 0 && y != 1)
{
let x = ((y * 1.0)
/ (1 - y))
* y;
if (mp.has(x))
count += mp.get(x);
}
if (mp.has(y)){
mp.set(y, mp.get(y)+1);
}
else {
mp.set(y, 1);
}
}
return count;
}
let arr = [ -4, -3, 0, 2, 1 ];
let N = arr.length;
document.write(countPairs(arr, N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)