Related Articles

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

• Difficulty Level : Hard
• Last Updated : 03 Sep, 2021

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*arr = 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*arr = 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.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``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]) == ``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);``    ``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

 ``
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 ``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);``    ``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 isontributed 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

 ``
Output:
`2`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up