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++
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Function of find the count of unordered // pairs (i, j) in the array such that // arr[j] / arr[i] = j / i. int countPairs( int arr[], int n) { // Stores the count of valid pairs int count = 0; // Iterating over all possible pairs for ( int i = 0; i < n - 1; i++) { for ( int j = i + 1; j < n; j++) { // Check if the pair is valid if ((arr[j] % arr[i] == 0) && (j + 1) % (i + 1) == 0 && (arr[j] / arr[i] == (j + 1) / (i + 1))) { count++; } } } // Return answer return count; } // Driver Code int main() { int arr[] = { 5, -2, 4, 20, 25, -6 }; int n = sizeof (arr) / sizeof (arr[0]); // Function Call cout << countPairs(arr, n); return 0; } |
Java
// Java program of the above approach import java.util.*; class GFG { // Function of find the count of unordered // pairs (i, j) in the array such that // arr[j] / arr[i] = j / i. static int countPairs( int arr[], int n) { // Stores the count of valid pairs int count = 0 ; // Iterating over all possible pairs for ( int i = 0 ; i < n - 1 ; i++) { for ( int j = i + 1 ; j < n; j++) { // Check if the pair is valid if ((arr[j] % arr[i] == 0 ) && (j + 1 ) % (i + 1 ) == 0 && (arr[j] / arr[i] == (j + 1 ) / (i + 1 ))) { count++; } } } // Return answer return count; } // Driver Code public static void main(String[] args) { int arr[] = { 5 , - 2 , 4 , 20 , 25 , - 6 }; int n = arr.length; // Function Call System.out.print(countPairs(arr, n)); } } // This code is contributed by shikhasingrajput |
Python3
# Python 3 program of the above approach # Function of find the count of unordered # pairs (i, j) in the array such that # arr[j] / arr[i] = j / i. def countPairs(arr, n): # Stores the count of valid pairs count = 0 # Iterating over all possible pairs for i in range (n - 1 ): for j in range (i + 1 , n): # Check if the pair is valid if ((arr[j] % arr[i] = = 0 ) and (j + 1 ) % (i + 1 ) = = 0 and (arr[j] / / arr[i] = = (j + 1 ) / / (i + 1 ))): count + = 1 # Return answer return count # Driver Code if __name__ = = "__main__" : arr = [ 5 , - 2 , 4 , 20 , 25 , - 6 ] n = len (arr) # Function Call print (countPairs(arr, n)) # This code is contributed by ukasp. |
C#
// C# program for the above approach using System; public class GFG { // Function of find the count of unordered // pairs (i, j) in the array such that // arr[j] / arr[i] = j / i. static int countPairs( int [] arr, int n) { // Stores the count of valid pairs int count = 0; // Iterating over all possible pairs for ( int i = 0; i < n - 1; i++) { for ( int j = i + 1; j < n; j++) { // Check if the pair is valid if ((arr[j] % arr[i] == 0) && (j + 1) % (i + 1) == 0 && (arr[j] / arr[i] == (j + 1) / (i + 1))) { count++; } } } // Return answer return count; } // Driver Code public static void Main ( string [] args) { int [] arr = { 5, -2, 4, 20, 25, -6 }; int n = arr.Length; // Function Call Console.WriteLine(countPairs(arr, n)); } } // This code is contributed by avijitmondal1998. |
Javascript
<script> // JavaScript Program to implement // the above approach // Function of find the count of unordered // pairs (i, j) in the array such that // arr[j] / arr[i] = j / i. function countPairs(arr, n) { // Stores the count of valid pairs let count = 0; // Iterating over all possible pairs for (let i = 0; i < n - 1; i++) { for (let j = i + 1; j < n; j++) { // Check if the pair is valid if ((arr[j] % arr[i] == 0) && (j + 1) % (i + 1) == 0 && (arr[j] / arr[i] == (j + 1) / (i + 1))) { count++; } } } // Return answer return count; } // Driver Code let arr = [5, -2, 4, 20, 25, -6]; let n = arr.length; // Function Call document.write(countPairs(arr, n)); // This code is contributed by Potta Lokesh </script> |
3
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++
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Function of find the count of unordered // pairs (i, j) in the array such that // arr[j] / arr[i] = j / i. int countPairs( int arr[], int n) { // Stores the count of valid pairs int count = 0; // Iterating over all values of // x in range [1, N]. for ( int x = 1; x <= n; x++) { // Iterating over all values // of y that are divisible by // x in the range [1, N]. for ( int y = 2 * x; y <= n; y += x) { // Check if the pair is valid if ((arr[y - 1] % arr[x - 1] == 0) && (arr[y - 1] / arr[x - 1] == y / x)) { count++; } } } // Return answer return count; } // Driver Code int main() { int arr[] = { 5, -2, 4, 20, 25, -6 }; int n = sizeof (arr) / sizeof (arr[0]); // Function Call cout << countPairs(arr, n); return 0; } |
Java
// Java program of the above approach import java.io.*; public class GFG{ // Function of find the count of unordered // pairs (i, j) in the array such that // arr[j] / arr[i] = j / i. static int countPairs( int arr[], int n) { // Stores the count of valid pairs int count = 0 ; // Iterating over all values of // x in range [1, N]. for ( int x = 1 ; x <= n; x++) { // Iterating over all values // of y that are divisible by // x in the range [1, N]. for ( int y = 2 * x; y <= n; y += x) { // Check if the pair is valid if ((arr[y - 1 ] % arr[x - 1 ] == 0 ) && (arr[y - 1 ] / arr[x - 1 ] == y / x)) { count++; } } } // Return answer return count; } // Driver Code public static void main(String[] args) { int arr[] = { 5 , - 2 , 4 , 20 , 25 , - 6 }; int n = arr.length; // Function Call System.out.print(countPairs(arr, n)); } } // This code is contributed by shikhasingrajput |
Python3
# Python 3 program of the above approach # Function of find the count of unordered # pairs (i, j) in the array such that # arr[j] / arr[i] = j / i. def countPairs(arr, n): # Stores the count of valid pairs count = 0 # Iterating over all values of # x in range [1, N]. for x in range ( 1 , n + 1 , 1 ): # Iterating over all values # of y that are divisible by # x in the range [1, N]. for y in range ( 2 * x, n + 1 , x): # Check if the pair is valid if ((arr[y - 1 ] % arr[x - 1 ] = = 0 ) and (arr[y - 1 ] / / arr[x - 1 ] = = y / / x)): count + = 1 # Return answer return count # Driver Code if __name__ = = '__main__' : arr = [ 5 , - 2 , 4 , 20 , 25 , - 6 ] n = len (arr) # Function Call print (countPairs(arr, n)) # This code is contributed by SURENDRA_GANGWAR. |
C#
// C# program of the above approach using System; class GFG { // Function of find the count of unordered // pairs (i, j) in the array such that // arr[j] / arr[i] = j / i. static int countPairs( int [] arr, int n) { // Stores the count of valid pairs int count = 0; // Iterating over all values of // x in range [1, N]. for ( int x = 1; x <= n; x++) { // Iterating over all values // of y that are divisible by // x in the range [1, N]. for ( int y = 2 * x; y <= n; y += x) { // Check if the pair is valid if ((arr[y - 1] % arr[x - 1] == 0) && (arr[y - 1] / arr[x - 1] == y / x)) { count++; } } } // Return answer return count; } // Driver Code public static void Main() { int [] arr = { 5, -2, 4, 20, 25, -6 }; int n = arr.Length; // Function Call Console.WriteLine(countPairs(arr, n)); } } // This code is contributed by ukasp. |
Javascript
<script> // javascript program of the above approach // Function of find the count of unordered // pairs (i, j) in the array such that // arr[j] / arr[i] = j / i. function countPairs(arr , n) { // Stores the count of valid pairs var count = 0; // Iterating over all values of // x in range [1, N]. for ( var x = 1; x <= n; x++) { // Iterating over all values // of y that are divisible by // x in the range [1, N]. for ( var y = 2 * x; y <= n; y += x) { // Check if the pair is valid if ((arr[y - 1] % arr[x - 1] == 0) && (arr[y - 1] / arr[x - 1] == y / x)) { count++; } } } // Return answer return count; } // Driver Code var arr = [ 5, -2, 4, 20, 25, -6 ]; var n = arr.length; // Function Call document.write(countPairs(arr, n)); // This code is contributed by shikhasingrajput </script> |
3
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!