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).
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void calculate( int a[], int b[], int n, int m)
{
int mul = 1;
for ( int i = 0 ; i < m ; i++)
if (b[i] != 0)
mul = mul * b[i];
for ( int i = 0 ; i < n ; i++)
{
int x = floor (a[i] / mul);
cout << x << " " ;
}
}
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
import java.io.*;
class GFG {
static void calculate( int a[], int b[],
int n, int m)
{
int mul = 1 ;
for ( int i = 0 ; i < m; i++)
if (b[i] != 0 )
mul = mul * b[i];
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);
}
}
|
Python3
import math
def calculate(a, b, n, m):
mul = 1
for i in range (m):
if (b[i] ! = 0 ):
mul = mul * b[i]
for i in range (n):
x = math.floor(a[i] / mul)
print (x, end = " " )
a = [ 5 , 100 , 8 ]
b = [ 2 , 3 ]
n = len (a)
m = len (b)
calculate(a, b, n, m)
|
C#
using System;
class GFG {
static void calculate( int []a, int []b,
int n, int m)
{
int mul = 1;
for ( int i = 0; i < m; i++)
if (b[i] != 0)
mul = mul * b[i];
for ( int i = 0; i < n; i++) {
int x = ( int )Math.Floor(( double )(a[i] / mul));
Console.Write(x + " " );
}
}
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);
}
}
|
PHP
<?php
function calculate( $a , $b ,
$n , $m )
{
$mul = 1;
for ( $i = 0 ; $i < $m ; $i ++)
if ( $b [ $i ] != 0)
$mul = $mul * $b [ $i ];
for ( $i = 0 ; $i < $n ; $i ++)
{
$x = floor ( $a [ $i ] / $mul );
echo $x , " " ;
}
}
$a = array (5 , 100 , 8);
$b = array (2 , 3);
$n = count ( $a );
$m = count ( $b );
calculate( $a , $b , $n , $m );
?>
|
Javascript
<script>
function calculate(a, b, n, m)
{
let mul = 1;
for (let i = 0 ; i < m ; i++)
if (b[i] != 0)
mul = mul * b[i];
for (let i = 0 ; i < n ; i++)
{
let x = Math.floor(a[i] / mul);
document.write(x + " " );
}
}
let a = [5 , 100 , 8];
let b = [2 , 3];
let n = a.length;
let m = b.length;
calculate(a, b, n, m);
</script>
|
Time complexity: O(N + M), where N and M are the sizes of given arrays.
Auxiliary space: O(1) since constant space is being used
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Feb, 2023
Like Article
Save Article