patternjavaMinor
Performing several integer division operations on an array
Viewed 0 times
performingoperationsdivisionarrayseveralinteger
Problem
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
Sample Output
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(" ");
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
2Sample Output
4 1 1 2 1Explanation
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(" ");
Solution
-
Correctness. Technically the program is incorrect. The assignment requires the result to be printed once. Your solution prints out the state of array after each division.
-
Optimization. Since you only need to print the result once, you don't have to preform an actual division on each query. Accumulate the product of divisors, and perform the division once at the last step. Certain care to avoid overflow is necessary.
Most likely the program statement included restrictions on the input data. It would be nice to see them.
Correctness. Technically the program is incorrect. The assignment requires the result to be printed once. Your solution prints out the state of array after each division.
-
Optimization. Since you only need to print the result once, you don't have to preform an actual division on each query. Accumulate the product of divisors, and perform the division once at the last step. Certain care to avoid overflow is necessary.
Most likely the program statement included restrictions on the input data. It would be nice to see them.
Context
StackExchange Code Review Q#84784, answer score: 4
Revisions (0)
No revisions yet.