Open In App

Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. (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.
  2. (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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
int getPairsCount(int a[], int n)
{
    // Stores the resultant number
    // of pairs
    int count = 0;
 
    // Generate all possible pairs
    // from the array arr[]
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // If the given condition
            // satisfy then increment
            // the value of count
            if ((a[i] * a[j]) == j-i)
                count++;
        }
    }
 
    // Return the resultant count
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 1, 2, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << getPairsCount(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
static int getPairsCount(int a[], int n)
{
     
    // Stores the resultant number
    // of pairs
    int count = 0;
 
    // Generate all possible pairs
    // from the array arr[]
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
             
            // If the given condition
            // satisfy then increment
            // the value of count
            if ((a[i] * a[j]) == Math.abs(i - j))
                count++;
        }
    }
 
    // Return the resultant count
    return count;
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 1, 1, 2, 4 };
    int N = arr.length;
     
    System.out.print(getPairsCount(arr, N));
}
}
 
// This code is contributed by avijitmondal1998


Python3




# Python3 program for the above approach
 
# Function to count the number of
# pairs (i, j) such that arr[i]*arr[j]
# is equal to abs(i - j)
def getPairsCount(a, n):
     
    # Stores the resultant number
    # of pairs
    count = 0
 
    # Generate all possible pairs
    # from the array arr[]
    for i in range(n - 1):
        for j in range(i + 1, n):
             
            # If the given condition
            # satisfy then increment
            # the value of count
            if ((a[i] * a[j]) == abs(i - j)):
                count += 1
 
    # Return the resultant count
    return count
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 1, 2, 4 ]
    N = len(arr)
     
    print(getPairsCount(arr, N))
 
# This code is contributed by mohit kumar 29


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
static int getPairsCount(int[] a, int n)
{
     
    // Stores the resultant number
    // of pairs
    int count = 0;
 
    // Generate all possible pairs
    // from the array arr[]
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
             
            // If the given condition
            // satisfy then increment
            // the value of count
            if ((a[i] * a[j]) == Math.Abs(i - j))
                count++;
        }
    }
 
    // Return the resultant count
    return count;
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, 1, 2, 4 };
    int N = arr.Length;
 
    Console.Write(getPairsCount(arr, N));
}
}
 
// This code is contributed by subhammahato348


Javascript




<script>
 
        // JavaScript program for the above approach
 
        // Function to count the number of
        // pairs (i, j) such that arr[i]*arr[j]
        // is equal to abs(i - j)
        function getPairsCount(a, n) {
            // Stores the resultant number
            // of pairs
            let count = 0;
 
            // Generate all possible pairs
            // from the array arr[]
            for (let i = 0; i < n - 1; i++) {
                for (let j = i + 1; j < n; j++) {
 
                    // If the given condition
                    // satisfy then increment
                    // the value of count
                    if ((a[i] * a[j]) == Math.abs(i - j))
                        count++;
                }
            }
 
            // Return the resultant count
            return count;
        }
 
        // Driver Code
 
        let arr = [1, 1, 2, 4];
        let N = arr.length;
        document.write(getPairsCount(arr, N));
 
    // This code is contributed by Potta Lokesh
    </script>


Output: 

2

 

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
int getPairsCount(int arr[], int n)
{
    // Stores the resultant number
    // of pairs
    int count = 0;
 
    // Iterate over the range [0, N)
    for (int i = 0; i < n; i++) {
 
        // Now, iterate from the value
        // arr[i]-(i%arr[i]) till N
        // with an increment of arr[i]
        for (int j = arr[i] - (i % arr[i]);
             j < n;
             j += arr[i]) {
 
            // If the given criteria
            // satisfy then increment
            // the value of count
            if (i < j && (arr[i] * arr[j]) == abs(i - j)) {
                count++;
            }
        }
    }
 
    // Return the resultant count
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 1, 2, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << getPairsCount(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
static int getPairsCount(int []arr, int n)
{
    // Stores the resultant number
    // of pairs
    int count = 0;
 
    // Iterate over the range [0, N)
    for (int i = 0; i < n; i++) {
 
        // Now, iterate from the value
        // arr[i]-(i%arr[i]) till N
        // with an increment of arr[i]
        for (int j = arr[i] - (i % arr[i]);
             j < n;
             j += arr[i]) {
 
            // If the given criteria
            // satisfy then increment
            // the value of count
            if (i < j && (arr[i] * arr[j]) == Math.abs(i - j)) {
                count++;
            }
        }
    }
 
    // Return the resultant count
    return count;
}
 
// Driver Code
public static void main(String args[])
{
    int []arr = { 1, 1, 2, 4 };
    int N = arr.length;
    System.out.print(getPairsCount(arr, N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Python3




# Python3 program for the above approach
 
# Function to count the number of
# pairs(i, j) such that arr[i]*arr[j]
# is equal to abs(i - j)
def getPairsCount(arr, n):
     
    # Stores the resultant number
    # of pairs
    count = 0
     
    # Iterate over the range[0, N)
    for i in range(0, n):
         
        # Now, iterate from the value
        #  arr[i]-(i % arr[i]) till N
        #  with an increment of arr[i]
        s = arr[i]-(i % arr[i])
        for j in range(s, n):
             
            # If the given criteria
            # satisfy then increment
            # the value of count
            if (i < j and (arr[i] * arr[j]) ==
                            abs(i - j)):
                count += 1
                 
    # Return the resultant count
    return count
 
#  Driver Code
arr = [ 1, 1, 2, 4 ]
N = len(arr)
 
print(getPairsCount(arr, N))
 
# This code is contributed by amreshkumar3


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
static int getPairsCount(int[] arr, int n)
{
     
    // Stores the resultant number
    // of pairs
    int count = 0;
 
    // Iterate over the range [0, N)
    for(int i = 0; i < n; i++)
    {
         
        // Now, iterate from the value
        // arr[i]-(i%arr[i]) till N
        // with an increment of arr[i]
        for(int j = arr[i] - (i % arr[i]);
                j < n; j += arr[i])
        {
             
            // If the given criteria
            // satisfy then increment
            // the value of count
            if (i < j && (arr[i] * arr[j]) ==
                Math.Abs(i - j))
            {
                count++;
            }
        }
    }
 
    // Return the resultant count
    return count;
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 1, 1, 2, 4 };
    int N = arr.Length;
     
    Console.Write(getPairsCount(arr, N));
}
}
 
// This code is contributed by ukasp


Javascript




<script>
// Javascript program for the above approach
 
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
function getPairsCount(arr, n)
{
 
  // Stores the resultant number
  // of pairs
  let count = 0;
 
  // Iterate over the range [0, N)
  for (let i = 0; i < n; i++)
  {
   
    // Now, iterate from the value
    // arr[i]-(i%arr[i]) till N
    // with an increment of arr[i]
    for (let j = arr[i] - (i % arr[i]); j < n; j += arr[i])
    {
     
      // If the given criteria
      // satisfy then increment
      // the value of count
      if (i < j && arr[i] * arr[j] == Math.abs(i - j)) {
        count++;
      }
    }
  }
 
  // Return the resultant count
  return count;
}
 
// Driver Code
 
let arr = [1, 1, 2, 4];
let N = arr.length;
document.write(getPairsCount(arr, N));
 
// This code is contributed by gfgking.
</script>


Output: 

2

 

Time Complexity: O(N*log N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads