5
\$\begingroup\$

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();
    }
    }

}
\$\endgroup\$
4
\$\begingroup\$
  • 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.

\$\endgroup\$
  • \$\begingroup\$ I have added your suggestion in my program please see the program in comment below.But I am not able to handle overflow ,For some of the test cases its getting error. \$\endgroup\$ – vks Mar 24 '15 at 6:29
3
\$\begingroup\$

Your code would be a lot easier to read if you would separate it into different methods. One for getting the input, one for the calculation, and one for printing the output.

This would also have another advantage: you could easily add automated tests, and you could profile your code to check out where the actual bottlenecks are (no need to optimize code that is not responsible for bad performance).

My guess would be that the bottleneck is the double for loops (it's the only place where the bottleneck isn't the user input).

Misc

  • your spacing doesn't follow Java conventions, and it's also not internally consistent, which makes your code harder to read. Any IDE will fix this for you.
  • your variable names aren't very good, which makes your code hard to read. Always avoid short variable names such as a, t, p, ... and avoid numbers in variable names except when they really make sense.
\$\endgroup\$
  • \$\begingroup\$ thanks for suggestion ,I will incorporate your suggestion in my program. \$\endgroup\$ – vks Mar 23 '15 at 12:23
1
\$\begingroup\$

@vnp's insight is correct. The sequence of operations ÷ 2 ÷ 3 ÷ 2 is equivalent to ÷ (2 × 3 × 2), or ÷ 8.

I would add that the try-catch, besides being obnoxiously not indented, is pointless:

public static void main(String[] args) {
try {
    …
}catch(Exception ex){
    ex.printStackTrace();
}
}

Just write

public static void main(String[] args) throws IOException {
    …
}

Consider using a java.util.Scanner instead — it gives you a slightly nicer interface.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.