Given an array A[] of N integers, the task is to generate an array B[] such that B[i] contains the count of indices j in A[] such that j < i and A[j] % A[i] = 0
Examples:
Input: arr[] = {3, 5, 1}
Output: 0 0 2
Explanation:
3 and 5 do not divide any element on
their left but 1 divides 3 and 5.Input: arr[] = {8, 1, 28, 4, 2, 6, 7}
Output: 0 1 0 2 3 0 1
Naive Approach: This approach is already discuss here. But the complexity of this approach is O(N2).
Efficient Approach:
- We can say that if number A divides a number B then A is a factor of B. So, we need to find the number of previous elements whose factor is the current element.
- We will maintain a count array which contains the count of the factor of each element.
-
Now, Iterate over the array and for each element
- Make the answer of current element equal to count [ arr[i] ] and
- Increment the frequency of each factor of arr[i] in count array.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print the // elements of the array void printArr(int arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; } // Function to increment the count // for each factor of given val void IncrementFactors(int count[], int val) { for (int i = 1; i * i <= val; i++) { if (val % i == 0) { if (i == val / i) { count[i]++; } else { count[i]++; count[val / i]++; } } } } // Function to generate and print // the required array void generateArr(int A[], int n) { int B[n]; // Find max element of array int maxi = *max_element(A, A + n); // Create count array of maxi size int count[maxi + 1] = { 0 }; // For every element of the array for (int i = 0; i < n; i++) { // Count[ A[i] ] denotes how many // previous elements are there whose // factor is the current element. B[i] = count[A[i]]; // Increment in count array for // factors of A[i] IncrementFactors(count, A[i]); } // Print the generated array printArr(B, n); } // Driver code int main() { int arr[] = { 8, 1, 28, 4, 2, 6, 7 }; int n = sizeof(arr) / sizeof(arr[0]); generateArr(arr, n); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG{ // Utility function to print the // elements of the array static void printArr(int arr[], int n) { for(int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Function to increment the count // for each factor of given val static void IncrementFactors(int count[], int val) { for(int i = 1; i * i <= val; i++) { if (val % i == 0) { if (i == val / i) { count[i]++; } else { count[i]++; count[val / i]++; } } } } // Function to generate and print // the required array static void generateArr(int A[], int n) { int []B = new int[n]; // Find max element of array int maxi = Arrays.stream(A).max().getAsInt(); // Create count array of maxi size int count[] = new int[maxi + 1]; // For every element of the array for(int i = 0; i < n; i++) { // Count[ A[i] ] denotes how many // previous elements are there whose // factor is the current element. B[i] = count[A[i]]; // Increment in count array for // factors of A[i] IncrementFactors(count, A[i]); } // Print the generated array printArr(B, n); } // Driver code public static void main(String[] args) { int arr[] = { 8, 1, 28, 4, 2, 6, 7 }; int n = arr.length; generateArr(arr, n); } } // This code is contributed by Amit Katiyar |
C#
// C# implementation of the approach using System; using System.Linq; class GFG{ // Utility function to print the // elements of the array static void printArr(int []arr, int n) { for(int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Function to increment the count // for each factor of given val static void IncrementFactors(int []count, int val) { for(int i = 1; i * i <= val; i++) { if (val % i == 0) { if (i == val / i) { count[i]++; } else { count[i]++; count[val / i]++; } } } } // Function to generate and print // the required array static void generateArr(int []A, int n) { int []B = new int[n]; // Find max element of array int maxi = A.Max(); // Create count array of maxi size int []count = new int[maxi + 1]; // For every element of the array for(int i = 0; i < n; i++) { // Count[ A[i] ] denotes how many // previous elements are there whose // factor is the current element. B[i] = count[A[i]]; // Increment in count array for // factors of A[i] IncrementFactors(count, A[i]); } // Print the generated array printArr(B, n); } // Driver code public static void Main(String[] args) { int []arr = { 8, 1, 28, 4, 2, 6, 7 }; int n = arr.Length; generateArr(arr, n); } } // This code is contributed by Amit Katiyar |
0 1 0 2 3 0 1
Time Complexity: O(N * root(MaxElement))
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.
Recommended Posts:
- Count of elements on the left which are divisible by current element
- Maximum count of elements divisible on the left for any element
- Largest element smaller than current element on left for every element in Array
- Find the difference of count of equal elements on the right and the left for each element
- Replace elements with absolute difference of smallest element on left and largest element on right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of array elements which are greater than all elements on its left
- Minimum elements to be added in a range so that count of elements is divisible by K
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Count of elements such that difference between sum of left and right sub arrays is equal to a multiple of k
- Find an array element such that all elements are divisible by it
- Count of elements not divisible by any other elements of Array
- Find the count of subsequences where each element is divisible by K
- Count of sub-sets of size n with total element sum divisible by 3
- Count the number of elements in an array which are divisible by k
- Count numbers in a range that are divisible by all array elements
- Count of pairs of Array elements which are divisible by K when concatenated
- Count elements whose sum with K is greater than max element
- Maximize the number of indices such that element is greater than element to its left
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : amit143katiyar

