- Plus Minus Problem
-
- Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal.
-
- Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to are acceptable.
-
-
-
- public static void plusMinus(List<Integer> arr) {
- // Write your code here
- double pos=0;
- double neg=0;
- double zero=0;
- double n=arr.size();
- for(int i:arr)
- {
- if(i>0)
- pos++;
- if(i<0)
- neg++;
- if(i==0)
- zero++;
- }
-
-
- Formatter fm=new Formatter();
- fm.format("%.6f", pos/n);
- System.out.println(fm);
-
- fm=new Formatter();
- fm.format("%.6f", neg/n);
- System.out.println(fm);
-
- fm=new Formatter();
- fm.format("%.6f", zero/n);
- System.out.println(fm);
-
-
-
-
- }
-