Given an array of integers arr, the task is to calculate the number of pairs (i, j) where i < j such that arr[j] % arr[i] = 0.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 5Input: arr[] = {1, 1, 2, 2, 3, 3}
Output: 11
Approach 1:
Iterate over all pairs of the array and keep incrementing the count of pairs that satisfy the required condition.
Below code is the implementation of the above approach:
C++
// C++ Program to find // the number of pairs // (i, j) such that arr[i] // is a factor of arr[j] #include <bits/stdc++.h> using namespace std; // Function to return the // count of Pairs int numPairs( int arr[], int n) { int ans = 0; for ( int i = 0; i < n; i++) { for ( int j = i + 1; j < n; j++) { if (arr[j] % arr[i] == 0) ans++; } } return ans; } // Driver code int main() { int arr[] = { 1, 1, 2, 2, 3, 3 }; int n = sizeof (arr) / sizeof (arr[0]); cout << numPairs(arr, n) << endl; return 0; } |
Java
// Java Program to find the number of pairs // (i, j) such that arr[i] is a factor of arr[j] import java.util.*; import java.lang.*; class GFG{ // Function to return the // count of Pairs static int numPairs( int arr[], int n) { int ans = 0 ; for ( int i = 0 ; i < n; i++) { for ( int j = i + 1 ; j < n; j++) { if (arr[j] % arr[i] == 0 ) ans++; } } return ans; } // Driver code public static void main(String[] args) { int arr[] = { 1 , 1 , 2 , 2 , 3 , 3 }; int n = arr.length; System.out.println(numPairs(arr, n)); } } // This code is contributed by offbeat |
Python3
# Python3 program to find the number # of pairs (i, j) such that arr[i] # is a factor of arr[j] # Function to return the # count of Pairs def numPairs(arr, n): ans = 0 for i in range (n): for j in range (i + 1 , n): if arr[j] % arr[i] = = 0 : ans + = 1 return ans # Driver code arr = [ 1 , 1 , 2 , 2 , 3 , 3 ] n = len (arr) print (numPairs(arr, n)) # This code is contributed by divyamohan123 |
C#
// C# Program to find the number of pairs // (i, j) such that arr[i] is a factor of arr[j] using System; class GFG{ // Function to return the // count of Pairs static int numPairs( int []arr, int n) { int ans = 0; for ( int i = 0; i < n; i++) { for ( int j = i + 1; j < n; j++) { if (arr[j] % arr[i] == 0) ans++; } } return ans; } // Driver code public static void Main() { int []arr = { 1, 1, 2, 2, 3, 3 }; int n = arr.Length; Console.Write(numPairs(arr, n)); } } // This code is contributed by Code_Mech |
11
Approach 2: Store the indices of all array elements in a map. Traverse the map and for every occurrence of an element:
- Add the occurrences of the same element after the current occurence.
- Add the occurrences of all its multiples after the current occurrence using upper_bound
Below code is the implementation of the above approach:
C++
// C++ Program to find // the number of pairs // (i, j) such that arr[i] // is a factor of arr[j] #include <bits/stdc++.h> using namespace std; // Function to return the // count of Pairs int numPairs( int arr[], int n) { map< int , vector< int > > mp; int mx = 0; for ( int i = 0; i < n; i++) { // Update the maximum mx = max(mx, arr[i]); // Store the indices of // every element mp[arr[i]].push_back(i); } int ans = 0; for ( auto i : mp) { int ctr = 1; // Access all indices of i for ( int j : i.second) { // Add the number of // occurences of i // after j-th index ans += i.second.size() - ctr; // Traverse all multiples of i for ( int k = 2 * i.first; k <= mx; k += i.first) { // Find their occurrences // after the j-th index int numGreater = 0; if (mp.find(k) != mp.end()) numGreater = int ( mp[k] .end() - upper_bound( mp[k].begin(), mp[k].end(), j)); // Add the count ans += numGreater; } ctr++; } } return ans; } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5 }; int n = sizeof (arr) / sizeof (arr[0]); cout << numPairs(arr, n) << endl; return 0; } |
5
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.