Program:
Given an array A of size N. Given Q operations, each operation contains an integer D. In each operation you have to divide all the elements of the array by D.
For example, for each operation with a given D, the new array A would be: Finally, after processing all the operations you have to print the final array after Q operations. Note: The result of the each division will be an integer, for example 5 / 2 = 2
Input:
First line of input contains a single integer N denoting number of elements in the array A. Next line of input contains N space separated integers denoting the elements of array A. Next line contains Q denoting number of operations. Next Q lines contains a single integer D by which divide the elements of the array.
Output:
Print single line containing N space separated integers after processing Q operations.
Sample Input
5 50 20 18 27 19 3 2 3 2
Sample Output
4 1 1 2 1
Explanation
In operation 1 after dividing the whole array by D=2, the resultant array will be: [25, 10, 9, 13, 9] In operation 2 after dividing the array from operation 1 by 3, the resultant array will be: [8, 3, 3, 4, 3] In operation 3 after dividing the array from operation 2 by 2, the resultant array will be: [4, 1, 1, 2, 1] So, the resultant array will be [4, 1, 1, 2, 1]
Solution:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class TestClass1 {
public static void main(String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter No of element in Array:-");
String line = br.readLine();
int N = Integer.parseInt(line);
int a[]=new int[N];
System.out.println("Now Enter Array elemnt seperated by space :-");
String line2= br.readLine();
String s[]=line2.split(" ");
for(int p=0;p<s.length ;p++){
a[p]=Integer.parseInt(s[p]);
}
System.out.println("Now Enter No of operations :-");
int d=Integer.parseInt(br.readLine());
System.out.println("Now enter all the operationis elemnt one by one:- ");
int t[]=new int[d];
for(int i=0;i<d;i++){
t[i]=Integer.parseInt(br.readLine());
}
System.out.println(Arrays.toString(t));
for(int j=0;j<d;j++){
for(int k=0;k<N;k++){
a[k]=a[k]/t[j];
}
}
System.out.print(Arrays.toString(a));
}catch(Exception ex){
ex.printStackTrace();
}
}
}