Divide every element of one array by other array elements - GeeksforGeeks

26 Feb, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 arrayvoid 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 codeint 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 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 program to find# quotient of arrayelementsimport math # Function to calculate the quotient# of every element of the arraydef 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 codea = [ 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# program to find quotient// of array elementsusing 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 program to find// quotient of array elements // Function to calculate// the quotient of every// element of the arrayfunction 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.?>
<script> // JavaScript program to find quotient of array// elements // Function to calculate the quotient// of every element of the arrayfunction calculate(a, b, n, m){    let mul = 1;     // Calculate the product of all elements    for (let i = 0 ; i < m ; i++)        if (b[i] != 0)            mul = mul * b[i];     // To calculate the quotient of every    // array element    for (let i = 0 ; i < n ; i++)    {        let x = Math.floor(a[i] / mul);        document.write(x + " ");    }} // Driver code    let a = [5 , 100 , 8];    let b = [2 , 3];    let n = a.length;    let m = b.length;    calculate(a, b, n, m); // This code is contributed by Surbhi Tyagi </script>
Output : 
0 16 1
 
vt_m
surbhityagi15
C Basics
Arrays
Mathematical
School Programming
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Merge two sorted arrays