Count of pairs in given Array with product of their values equal to sum of their indices (arr[i]*arr[j] = i+j)
Given an array arr[] of length N with distinct integers from 1 to 2*N, the task is to count the number of pairs of indices (i, j) such that (i < j) and arr[i] * arr[j] = i + j, i.e. calculate the number of pairs such that their product is equal to their sum of indices.
Examples:
Input: N = 5, arr[] = {3, 1, 5, 9, 2}
Output: 3
Explanation: There are three pairs (i, j) such that (i < j) and arr[i] * arr[j] = i + j (1, 2), (1, 5), (2, 3)
Input: N = 3, arr[] = {6, 1, 5}
Output: 1
Naive Approach: Iterate over all pairs of indices (i, j) with (i < j) and check for each pair if the above condition is satisfied, then increase the answer by 1 otherwise go to the next pair.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int NumberOfRequiredPairs( int arr[], int N)
{
int ans = 0;
for ( int i = 0; i < N; i++)
for ( int j = i + 1; j < N; j++)
if ((arr[i] * arr[j])
== ((i + 1) + (j + 1)))
ans++;
return ans;
}
int main()
{
int N = 5;
int arr[] = { 3, 1, 5, 9, 2 };
cout << NumberOfRequiredPairs(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int NumberOfRequiredPairs( int arr[], int N)
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++)
for ( int j = i + 1 ; j < N; j++)
if ((arr[i] * arr[j])
== ((i + 1 ) + (j + 1 )))
ans++;
return ans;
}
public static void main (String[] args)
{
int N = 5 ;
int arr[] = { 3 , 1 , 5 , 9 , 2 };
System.out.println(NumberOfRequiredPairs(arr, N));
}
}
|
Python3
def NumberOfRequiredPairs(arr, N):
ans = 0
for i in range (N):
for j in range (i + 1 , N):
if ((arr[i] * arr[j]) = = ((i + 1 ) + (j + 1 ))):
ans + = 1
return ans
N = 5
arr = [ 3 , 1 , 5 , 9 , 2 ]
print (NumberOfRequiredPairs(arr, N))
|
C#
using System;
class GFG
{
static int NumberOfRequiredPairs( int []arr, int N)
{
int ans = 0;
for ( int i = 0; i < N; i++)
for ( int j = i + 1; j < N; j++)
if ((arr[i] * arr[j])
== ((i + 1) + (j + 1)))
ans++;
return ans;
}
public static void Main ()
{
int N = 5;
int []arr = { 3, 1, 5, 9, 2 };
Console.Write(NumberOfRequiredPairs(arr, N));
}
}
|
Javascript
<script>
function NumberOfRequiredPairs(arr, N)
{
let ans = 0;
for (let i = 0; i < N; i++)
for (let j = i + 1; j < N; j++)
if ((arr[i] * arr[j])
== ((i + 1) + (j + 1)))
ans++;
return ans;
}
let N = 5;
let arr = [ 3, 1, 5, 9, 2 ];
document.write(NumberOfRequiredPairs(arr, N));
</script>
|
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Efficient Approach : Rewrite the mentioned condition as
arr[j] = (i + j)/arr[i]
Therefore, for each multiple of arr[i], find the respective j and check whether arr[j] is equal to (i + j)/ arr[i]. This approach is efficient because for each i it is required to go through each multiple of i till 2*N. As all numbers in the array are distinct, it can be concluded that the total iterations for calculating j will be like:
N + N/2 + N/3 + N/4 + N/5……
This is a well-known result of the series of expansions of logN. For more information read about this here. Follow the steps below to solve the problem:
- Initialize the variable ans as 0 to store the answer.
- Iterate over the range [0, N] using the variable i and perform the following steps:
- Initialize the variable k as the value of arr[i].
- Iterate in a while loop till k is less than equal to 2*N and perform the following tasks:
- Initialize the variable j as k-i-1.
- If j is greater than equal to 1 and less than equal to N and arr[j – 1] is equal to k / arr[i] and j is greater than i+1, then increase the value of ans by 1.
- After performing the above steps, print the value of ans as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int NumberOfRequiredPairs( int arr[], int N)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
int k = arr[i];
while (k <= 2 * N) {
int j = k - i - 1;
if (j >= 1 && j <= N) {
if ((arr[j - 1] == k / arr[i])
&& j > i + 1) {
ans++;
}
}
k += arr[i];
}
}
return ans;
}
int main()
{
int N = 5;
int arr[] = { 3, 1, 5, 9, 2 };
cout << NumberOfRequiredPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int NumberOfRequiredPairs( int arr[], int N)
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
int k = arr[i];
while (k <= 2 * N) {
int j = k - i - 1 ;
if (j >= 1 && j <= N) {
if ((arr[j - 1 ] == k / arr[i])
&& j > i + 1 ) {
ans++;
}
}
k += arr[i];
}
}
return ans;
}
public static void main (String args[])
{
int N = 5 ;
int arr[] = { 3 , 1 , 5 , 9 , 2 };
System.out.println(NumberOfRequiredPairs(arr, N));
}
}
|
Python3
def NumberOfRequiredPairs(arr, N) :
ans = 0 ;
for i in range (N) :
k = arr[i];
while (k < = 2 * N) :
j = k - i - 1 ;
if (j > = 1 and j < = N) :
if ((arr[j - 1 ] = = k / / arr[i]) and j > i + 1 ) :
ans + = 1 ;
k + = arr[i];
return ans;
if __name__ = = "__main__" :
N = 5 ;
arr = [ 3 , 1 , 5 , 9 , 2 ];
print (NumberOfRequiredPairs(arr, N));
|
C#
using System;
class GFG
{
static int NumberOfRequiredPairs( int []arr, int N)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
int k = arr[i];
while (k <= 2 * N) {
int j = k - i - 1;
if (j >= 1 && j <= N) {
if ((arr[j - 1] == k / arr[i])
&& j > i + 1) {
ans++;
}
}
k += arr[i];
}
}
return ans;
}
public static void Main ()
{
int N = 5;
int []arr = { 3, 1, 5, 9, 2 };
Console.Write(NumberOfRequiredPairs(arr, N));
}
}
|
Javascript
<script>
function NumberOfRequiredPairs(arr, N)
{
let ans = 0;
for (let i = 0; i < N; i++) {
let k = arr[i];
while (k <= 2 * N) {
let j = k - i - 1;
if (j >= 1 && j <= N) {
if ((arr[j - 1] == k / arr[i])
&& j > i + 1) {
ans++;
}
}
k += arr[i];
}
}
return ans;
}
let N = 5;
let arr = [ 3, 1, 5, 9, 2 ];
document.write(NumberOfRequiredPairs(arr, N));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Last Updated :
09 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...