Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices
Last Updated :
31 Mar, 2023
Given an array arr[] consisting of N positive integers, the task is to find the number of pairs (i, j) such that i < j and the product of elements at these indices is equal to the absolute difference of their indices.
Examples:
Input: arr[] = {1, 1, 2, 4}
Output: 2
Explanation:
Following are the possible pairs:
- (0, 1): The sum of these indices is 0 + 1 = 1 and the product of elements at these indices is arr[0]*arr[1] = 1*1 = 1.
- (0, 2): The sum of these indices is 0 + 2 = 2 and the product of elements at these indices is arr[0]*arr[1] = 1*2 = 2.
Therefore, the total count of pairs is 2.
Input: arr[] = {1, 2, 1}
Output: 0
Naive Approach: The simple approach to solve the given problem is to generate all possible pairs of the given array and count those pairs that satisfy the given criteria. After checking for all the pairs, print the total count obtained.
Algorithm
1)Initialize a variable "count" to 0, which stores the resultant number of pairs that satisfy the given condition.
2)Loop through all possible pairs of elements in the input array, arr[].
a. The outer loop runs from i = 0 to n-2, and the inner loop runs from j = i+1 to n-1.
b. For each pair (i, j), check whether the product of the elements a[i] and a[j] is equal to the absolute difference between i and j.
(i)If this condition is satisfied, then increment the value of the "count" variable.
3)After all possible pairs have been checked, return the value of the "count" variable.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getPairsCount( int a[], int n)
{
int count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
if ((a[i] * a[j]) == j-i)
count++;
}
}
return count;
}
int main()
{
int arr[] = { 1, 1, 2, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << getPairsCount(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int getPairsCount( int a[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
for ( int j = i + 1 ; j < n; j++)
{
if ((a[i] * a[j]) == Math.abs(i - j))
count++;
}
}
return count;
}
public static void main(String args[])
{
int arr[] = { 1 , 1 , 2 , 4 };
int N = arr.length;
System.out.print(getPairsCount(arr, N));
}
}
|
Python3
def getPairsCount(a, n):
count = 0
for i in range (n - 1 ):
for j in range (i + 1 , n):
if ((a[i] * a[j]) = = abs (i - j)):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 1 , 1 , 2 , 4 ]
N = len (arr)
print (getPairsCount(arr, N))
|
C#
using System;
class GFG{
static int getPairsCount( int [] a, int n)
{
int count = 0;
for ( int i = 0; i < n - 1; i++)
{
for ( int j = i + 1; j < n; j++)
{
if ((a[i] * a[j]) == Math.Abs(i - j))
count++;
}
}
return count;
}
public static void Main()
{
int [] arr = { 1, 1, 2, 4 };
int N = arr.Length;
Console.Write(getPairsCount(arr, N));
}
}
|
Javascript
<script>
function getPairsCount(a, n) {
let count = 0;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
if ((a[i] * a[j]) == Math.abs(i - j))
count++;
}
}
return count;
}
let arr = [1, 1, 2, 4];
let N = arr.length;
document.write(getPairsCount(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by optimizing the inner loop used in the above step. The idea is to iterate over the range [0, N – 1] in the first loop, and in the second loop iterate from arr[i] – (i%arr[i]) using variable j and increment the value of j by arr[i] till N and then check for the given criteria. Follow the steps below to solve the problem:
- Initialize the variable, say count as 0 that stores the resultant count of pairs.
- Iterate over the range [0, N] using the variable i and perform the following steps:
- Iterate over the range [arr[i] – (i%arr[i]), N] using the variable j with an increment of arr[i] and if i is less than j and arr[i]*arr[j] is equal to abs(i – j), then increment the value of count by 1.
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int getPairsCount( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = arr[i] - (i % arr[i]);
j < n;
j += arr[i]) {
if (i < j && (arr[i] * arr[j]) == abs (i - j)) {
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { 1, 1, 2, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << getPairsCount(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int getPairsCount( int []arr, int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = arr[i] - (i % arr[i]);
j < n;
j += arr[i]) {
if (i < j && (arr[i] * arr[j]) == Math.abs(i - j)) {
count++;
}
}
}
return count;
}
public static void main(String args[])
{
int []arr = { 1 , 1 , 2 , 4 };
int N = arr.length;
System.out.print(getPairsCount(arr, N));
}
}
|
Python3
def getPairsCount(arr, n):
count = 0
for i in range ( 0 , n):
s = arr[i] - (i % arr[i])
for j in range (s, n):
if (i < j and (arr[i] * arr[j]) = =
abs (i - j)):
count + = 1
return count
arr = [ 1 , 1 , 2 , 4 ]
N = len (arr)
print (getPairsCount(arr, N))
|
C#
using System;
class GFG{
static int getPairsCount( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = arr[i] - (i % arr[i]);
j < n; j += arr[i])
{
if (i < j && (arr[i] * arr[j]) ==
Math.Abs(i - j))
{
count++;
}
}
}
return count;
}
public static void Main()
{
int [] arr = { 1, 1, 2, 4 };
int N = arr.Length;
Console.Write(getPairsCount(arr, N));
}
}
|
Javascript
<script>
function getPairsCount(arr, n)
{
let count = 0;
for (let i = 0; i < n; i++)
{
for (let j = arr[i] - (i % arr[i]); j < n; j += arr[i])
{
if (i < j && arr[i] * arr[j] == Math.abs(i - j)) {
count++;
}
}
}
return count;
}
let arr = [1, 1, 2, 4];
let N = arr.length;
document.write(getPairsCount(arr, N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...