Given two arrays and the operation to be performed is that the every element of a[] should be divided by all the element of b[] and their floor value has to be calculated.
Examples :
Input : a[] = {5, 100, 8}, b[] = {2, 3}
Output : 0 16 1
Explanation :
Size of a[] is 3.
Size of b[] is 2.
Now 5 has to be divided by the elements of array b[]
i.e. 5 is divided by 2, then the quotient obtained
is divided by 3 and the floor value of this is
calculated. The same process is repeated for the other
array elements.
First Approach : This solution is of complexity O(n * m) where size of a[] is n and size of array b[] is m. In this solution we fix the elements of array a[] and iterate it with the elements of array b[].
Second Approach : In this method we have used simple maths. We first find product of array B and then divide it by each array element of a[]
The complexity of this solution is O(n).
C++
// CPP program to find quotient of array // elements #include <bits/stdc++.h> using namespace std; // Function to calculate the quotient // of every element of the array void calculate(int a[], int b[], int n, int m) { int mul = 1; // Calculate the product of all elements for (int i = 0 ; i < m ; i++) if (b[i] != 0) mul = mul * b[i]; // To calculate the quotient of every // array element for (int i = 0 ; i < n ; i++) { int x = floor(a[i] / mul); cout << x << " "; } } // Driver code int main() { int a[] = {5 , 100 , 8}; int b[] = {2 , 3}; int n = sizeof(a)/sizeof(a[0]); int m = sizeof(b)/sizeof(b[0]); calculate(a, b, n, m); return 0; } |
Java
// Java program to find quotient of array // elements import java.io.*; class GFG { // Function to calculate the quotient // of every element of the array static void calculate(int a[], int b[], int n, int m) { int mul = 1; // Calculate the product of all // elements for (int i = 0; i < m; i++) if (b[i] != 0) mul = mul * b[i]; // To calculate the quotient of every // array element for (int i = 0; i < n; i++) { int x = (int)Math.floor(a[i] / mul); System.out.print(x + " "); } } public static void main(String[] args) { int a[] = { 5, 100, 8 }; int b[] = { 2, 3 }; int n = a.length; int m = b.length; calculate(a, b, n, m); } } // This code is contributed by Ajit. |
Python3
# Python3 program to find # quotient of arrayelements import math # Function to calculate the quotient # of every element of the array def calculate(a, b, n, m): mul = 1 # Calculate the product # of all elements for i in range(m): if (b[i] != 0): mul = mul * b[i] # To calculate the quotient # of every array element for i in range(n): x = math.floor(a[i] / mul) print(x, end = " ") # Driver code a = [ 5, 100, 8 ] b = [ 2, 3 ] n = len(a) m = len(b) calculate(a, b, n, m) # This code is contributed by Anant Agarwal. |
C#
// C# program to find quotient // of array elements using System; class GFG { // Function to calculate the quotient // of every element of the array static void calculate(int []a, int []b, int n, int m) { int mul = 1; // Calculate the product of all // elements for (int i = 0; i < m; i++) if (b[i] != 0) mul = mul * b[i]; // To calculate the quotient of every // array element for (int i = 0; i < n; i++) { int x = (int)Math.Floor((double)(a[i] / mul)); Console.Write(x + " "); } } // Driver code public static void Main() { int []a = { 5, 100, 8 }; int []b = { 2, 3 }; int n = a.Length; int m = b.Length; calculate(a, b, n, m); } } // This code is contributed by Anant Agarwal. |
PHP
<?php // PHP program to find // quotient of array elements // Function to calculate // the quotient of every // element of the array function calculate( $a, $b, $n, $m) { $mul = 1; // Calculate the product // of all elements for ( $i = 0 ; $i < $m ; $i++) if ($b[$i] != 0) $mul = $mul * $b[$i]; // To calculate the quotient // of every array element for ( $i = 0 ; $i < $n ; $i++) { $x = floor($a[$i] / $mul); echo $x , " "; } } // Driver code $a = array(5 , 100 , 8); $b = array(2 , 3); $n = count($a); $m = count($b); calculate($a, $b, $n, $m); // This code is contributed by anuju_67. ?> |
Output :
0 16 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.
Recommended Posts:
- Count array elements that divide the sum of all other elements
- Find the element that appears once in an array where every other element appears twice
- Check if minimum element in array is less than or equals half of every other element
- Replace every element of the array by sum of all other elements
- Replace every element with the smallest of all other array elements
- Replace every element of the array by product of all other elements
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Length of array pair formed where one contains all distinct elements and other all same elements
- Replace every element of the array with BitWise XOR of all other
- Sub-strings that start and end with one character and have at least one other
- Largest element smaller than current element on left for every element in Array
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Equally divide into two sets such that one set has maximum distinct elements
- Print array elements that are divisible by at-least one other
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Insert N elements in a Linked List one after other at middle position
- Maximum possible remainder when an element is divided by other element in the array
- Closest greater element for every array element from another array
- Longest Subsequence with at least one common digit in every element
- Find the element having different frequency than other array elements
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 : vt_m

