Count number of pairs (i, j) from an array such that arr[i] * j = arr[j] * i
Last Updated :
27 Jan, 2023
Given an array arr[] of size N, the task is to count the number of pairs (i, j) possible from the array such that arr[j] * i = arr[i] * j, where 1 ≤ i < j ≤ N.
Examples:
Input: arr[] = {1, 3, 5, 6, 5}
Output: 2
Explanation:
Pair (1, 5) satisfies the condition, since arr[1] * 5 = arr[5] * 1.
Pair (2, 4) satisfies the condition, since arr[2] * 4 = arr[4] * 2.
Therefore, total number of pairs satisfying the given condition is 2.
Input: arr[] = {2, 1, 3}
Output: 0
Naive Approach: The simplest approach to solve the problem is to generate all possible pairs from the array and check for each pair, whether the given condition satisfies or not. Increase count for the pairs for which the condition is satisfied. Finally, print the count of all such pairs.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
void countPairs( int arr[], int N)
{
int count = 0;
for ( int i = 1; i <= N; i++) {
for ( int j = i + 1; j <= N; j++) {
if ((arr[j - 1] * i) == (arr[i - 1] * j)) {
count++;
}
}
}
cout << count;
}
int main()
{
int arr[] = { 1, 3, 5, 6, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
countPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void countPairs( int [] arr, int N)
{
int count = 0 ;
for ( int i = 1 ; i <= N; i++) {
for ( int j = i + 1 ; j <= N; j++) {
if ((arr[j - 1 ] * i) == (arr[i - 1 ] * j)) {
count++;
}
}
}
System.out.print(count);
}
public static void main(String args[])
{
int [] arr = { 1 , 3 , 5 , 6 , 5 };
int N = arr.length;
countPairs(arr, N);
}
}
|
Python3
def countPairs(arr, N):
count = 0
for i in range ( 1 , N + 1 ):
for j in range (i + 1 , N + 1 ):
if (arr[j - 1 ] * i) = = (arr[i - 1 ] * j):
count + = 1
print (count)
arr = [ 1 , 3 , 5 , 6 , 5 ]
N = len (arr)
countPairs(arr, N)
|
C#
using System;
using System.Collections.Generic;
public class Gfg {
static void countPairs( int []arr, int N)
{
int count = 0;
for ( int i = 1; i <= N; i++) {
for ( int j = i + 1; j <= N; j++) {
if ((arr[j - 1] * i) == (arr[i - 1] * j)) {
count++;
}
}
}
Console.WriteLine(count);
}
public static void Main( string [] args)
{
int [] arr = { 1, 3, 5, 6, 5 };
int N = arr.Length;
countPairs(arr, N);
}
}
|
Javascript
function countPairs(arr, N)
{
let count = 0;
for (let i = 1; i <= N; i++) {
for (let j = i + 1; j <= N; j++) {
if ((arr[j - 1] * i) == (arr[i - 1] * j)) {
count++;
}
}
}
document.write(count);
}
let arr = [1, 3, 5, 6, 5 ];
let N = arr.length;
countPairs(arr, N);
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the rearrangement of the given equation arr[i] * j = arr[j] * i to arr[i] / i = arr[j] / j.
Follow the steps below to solve the problem:
- Initialize a variable, say count, to store the total count of pairs satisfying the given conditions.
- Initialize an Unordered Map, say mp, to count the frequency of values arr[i] / i.
- Traverse the array arr[] and update the frequencies of arr[i]/i in the Map.
- Print the count as the answer.
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;
unordered_map< double , int > mp;
for ( int i = 0; i < N; i++) {
double val = 1.0 * arr[i];
double idx = 1.0 * (i + 1);
count += mp[val / idx];
mp[val / idx]++;
}
cout << count;
}
int main()
{
int arr[] = { 1, 3, 5, 6, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
countPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void countPairs( int []arr, int N)
{
int count = 0 ;
Map<Double, Integer> mp
= new HashMap<Double, Integer>();
for ( int i = 0 ; i < N; i++)
{
Double val = 1.0 * arr[i];
Double idx = 1.0 * (i + 1 );
if (mp.containsKey(val / idx))
count += mp.get(val/idx);
if (mp.containsKey(val / idx))
mp.put(val / idx, mp.getOrDefault(val / idx, 0 ) + 1 );
else
mp.put(val/idx, 1 );
}
System.out.print(count);
}
public static void main(String args[])
{
int []arr = { 1 , 3 , 5 , 6 , 5 };
int N = arr.length;
countPairs(arr, N);
}
}
|
Python3
from collections import defaultdict
def countPairs(arr, N):
count = 0
mp = defaultdict( int )
for i in range (N):
val = 1.0 * arr[i]
idx = 1.0 * (i + 1 )
count + = mp[val / idx]
mp[val / idx] + = 1
print (count)
if __name__ = = "__main__" :
arr = [ 1 , 3 , 5 , 6 , 5 ]
N = len (arr)
countPairs(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countPairs( int []arr, int N)
{
int count = 0;
Dictionary< double ,
int > mp = new Dictionary< double ,
int >();
for ( int i = 0; i < N; i++)
{
double val = 1.0 * arr[i];
double idx = 1.0 * (i + 1);
if (mp.ContainsKey(val / idx))
count += mp[val/idx];
if (mp.ContainsKey(val / idx))
mp[val / idx]++;
else
mp[val/idx] = 1;
}
Console.WriteLine(count);
}
public static void Main()
{
int []arr = { 1, 3, 5, 6, 5 };
int N = arr.Length;
countPairs(arr, N);
}
}
|
Javascript
<script>
function countPairs(arr, N)
{
var count = 0;
var mp = new Map();
for ( var i = 0; i < N; i++) {
var val = 1.0 * arr[i];
var idx = 1.0 * (i + 1);
count += mp.has(val/idx)?mp.get(val/idx):0
if (mp.has(val/idx))
mp.set(val/idx, mp.get(val/idx)+1)
else
mp.set(val/idx, 1)
}
document.write( count);
}
var arr = [1, 3, 5, 6, 5];
var N = arr.length;
countPairs(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...