Count of elements on the left which are divisible by current element
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
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
Approach: Run a loop from the first element to the last element of the array and for the current element, run another loop for the elements on its left and check how many elements on its left are divisible by it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printArr( int arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
void generateArr( int A[], int n)
{
int B[n];
for ( int i = 0; i < n; i++) {
int cnt = 0;
for ( int j = 0; j < i; j++) {
if (A[j] % A[i] == 0)
cnt++;
}
B[i] = cnt;
}
printArr(B, n);
}
int main()
{
int A[] = { 3, 5, 1 };
int n = sizeof (A) / sizeof (A[0]);
generateArr(A, n);
return 0;
}
|
Java
class GFG
{
static void printArr( int arr[], int n)
{
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
static void generateArr( int A[], int n)
{
int []B = new int [n];
for ( int i = 0 ; i < n; i++)
{
int cnt = 0 ;
for ( int j = 0 ; j < i; j++)
{
if (A[j] % A[i] == 0 )
cnt++;
}
B[i] = cnt;
}
printArr(B, n);
}
public static void main(String args[])
{
int A[] = { 3 , 5 , 1 };
int n = A.length;
generateArr(A, n);
}
}
|
Python3
def printArr(arr, n):
for i in arr:
print (i, end = " " )
def generateArr(A, n):
B = [ 0 for i in range (n)]
for i in range (n):
cnt = 0
for j in range (i):
if (A[j] % A[i] = = 0 ):
cnt + = 1
B[i] = cnt
printArr(B, n)
A = [ 3 , 5 , 1 ]
n = len (A)
generateArr(A, n)
|
C#
using System;
class GFG
{
static void printArr( int []arr, int n)
{
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
static void generateArr( int []A, int n)
{
int []B = new int [n];
for ( int i = 0; i < n; i++)
{
int cnt = 0;
for ( int j = 0; j < i; j++)
{
if (A[j] % A[i] == 0)
cnt++;
}
B[i] = cnt;
}
printArr(B, n);
}
public static void Main(String []args)
{
int []A = { 3, 5, 1 };
int n = A.Length;
generateArr(A, n);
}
}
|
Javascript
<script>
function printArr(arr, n)
{
for (let i = 0; i < n; i++)
document.write(arr[i] + " " );
}
function generateArr(A, n)
{
let B = new Array(n);
for (let i = 0; i < n; i++) {
let cnt = 0;
for (let j = 0; j < i; j++) {
if (A[j] % A[i] == 0)
cnt++;
}
B[i] = cnt;
}
printArr(B, n);
}
let A = [ 3, 5, 1 ];
let n = A.length;
generateArr(A, n);
</script>
|
Time Complexity: O(n2), Since there are two nested loops inside each other in the worst case if the first loop run for all n elements, then for each element the inner loop also runs n*n times.
Auxiliary Space: O(n), since there is an extra array involved thus it takes O(n) extra space.
Last Updated :
03 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...