Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices
Last Updated :
12 Dec, 2022
Given an array arr[] of N integers, the task is to find the number of unordered pairs (i, j) in the array such that the ratio of elements at these indices is the same as the ratio of indices (arr[j]/arr[i] = j/i).
Examples:
Input: arr[] = {4, 5, 12, 10, 6}
Output: 2
Explanation: The pairs that follow the given condition are:
- (1, 3) as arr[3] / arr[1] = 12/4 = 3 = 3/1
- (2, 4) as arr[4] / arr[2] = 10/5 = 2 = 4/2
Input: arr[] = {5, -2, 4, 20, 25, -6}
Output: 3
Naive Approach: The given problem can be solved by iterating over all the unordered pairs (i, j) in the given array while keeping track of the number of pairs that follow the condition arr[j] / arr[i] = j / i.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
if ((arr[j] % arr[i] == 0)
&& (j + 1) % (i + 1) == 0
&& (arr[j] / arr[i]
== (j + 1) / (i + 1))) {
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { 5, -2, 4, 20, 25, -6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int countPairs( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
for ( int j = i + 1 ; j < n; j++) {
if ((arr[j] % arr[i] == 0 )
&& (j + 1 ) % (i + 1 ) == 0
&& (arr[j] / arr[i]
== (j + 1 ) / (i + 1 ))) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 5 , - 2 , 4 , 20 , 25 , - 6 };
int n = arr.length;
System.out.print(countPairs(arr, n));
}
}
|
Python3
def countPairs(arr, n):
count = 0
for i in range (n - 1 ):
for j in range (i + 1 , n):
if ((arr[j] % arr[i] = = 0 )
and (j + 1 ) % (i + 1 ) = = 0
and (arr[j] / / arr[i]
= = (j + 1 ) / / (i + 1 ))):
count + = 1
return count
if __name__ = = "__main__" :
arr = [ 5 , - 2 , 4 , 20 , 25 , - 6 ]
n = len (arr)
print (countPairs(arr, n))
|
C#
using System;
public class GFG
{
static int countPairs( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
if ((arr[j] % arr[i] == 0)
&& (j + 1) % (i + 1) == 0
&& (arr[j] / arr[i]
== (j + 1) / (i + 1))) {
count++;
}
}
}
return count;
}
public static void Main ( string [] args)
{
int [] arr = { 5, -2, 4, 20, 25, -6 };
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
|
Javascript
<script>
function countPairs(arr, n)
{
let count = 0;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
if ((arr[j] % arr[i] == 0)
&& (j + 1) % (i + 1) == 0
&& (arr[j] / arr[i]
== (j + 1) / (i + 1))) {
count++;
}
}
}
return count;
}
let arr = [5, -2, 4, 20, 25, -6];
let 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 using the observation that the maximum value of y / x for any pair (x, y) that can be reached is N. Also, y must be divisible by x. Therefore, for x in the range [1, N], iterate over all y in the range [1, N] such that y is divisible by x and keep a track of the number of pairs (x, y) such that arr[y] / arr[x] = y / x. This can be done using the Sieve of Eratosthenes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr[], int n)
{
int count = 0;
for ( int x = 1; x <= n; x++) {
for ( int y = 2 * x; y <= n; y += x) {
if ((arr[y - 1] % arr[x - 1] == 0)
&& (arr[y - 1] / arr[x - 1]
== y / x)) {
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { 5, -2, 4, 20, 25, -6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, n);
return 0;
}
|
Java
import java.io.*;
public class GFG{
static int countPairs( int arr[], int n)
{
int count = 0 ;
for ( int x = 1 ; x <= n; x++) {
for ( int y = 2 * x; y <= n; y += x) {
if ((arr[y - 1 ] % arr[x - 1 ] == 0 )
&& (arr[y - 1 ] / arr[x - 1 ]
== y / x)) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 5 , - 2 , 4 , 20 , 25 , - 6 };
int n = arr.length;
System.out.print(countPairs(arr, n));
}
}
|
Python3
def countPairs(arr, n):
count = 0
for x in range ( 1 , n + 1 , 1 ):
for y in range ( 2 * x, n + 1 , x):
if ((arr[y - 1 ] % arr[x - 1 ] = = 0 ) and (arr[y - 1 ] / / arr[x - 1 ] = = y / / x)):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 5 , - 2 , 4 , 20 , 25 , - 6 ]
n = len (arr)
print (countPairs(arr, n))
|
C#
using System;
class GFG {
static int countPairs( int [] arr, int n)
{
int count = 0;
for ( int x = 1; x <= n; x++) {
for ( int y = 2 * x; y <= n; y += x) {
if ((arr[y - 1] % arr[x - 1] == 0)
&& (arr[y - 1] / arr[x - 1] == y / x)) {
count++;
}
}
}
return count;
}
public static void Main()
{
int [] arr = { 5, -2, 4, 20, 25, -6 };
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
|
Javascript
<script>
function countPairs(arr , n)
{
var count = 0;
for ( var x = 1; x <= n; x++) {
for ( var y = 2 * x; y <= n; y += x) {
if ((arr[y - 1] % arr[x - 1] == 0)
&& (arr[y - 1] / arr[x - 1]
== y / x)) {
count++;
}
}
}
return count;
}
var arr = [ 5, -2, 4, 20, 25, -6 ];
var n = arr.length;
document.write(countPairs(arr, n));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...