Plus Minus Question Answer – HackerRank

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'plusMinus' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static void plusMinus(List<Integer> arr) {
    // Write your code here
            int noOfPlusValues = 0;
            int noOfMinusValues = 0;
            int noOfZeroValues = 0;
            
            for(Integer value : arr){
                
                if(value == 0){
                    noOfZeroValues++;
                }
                else if(value < 0){
                    noOfMinusValues++;
                }    
                else{
                    noOfPlusValues++;
                }
            }
            
            DecimalFormat decimalFormat = new DecimalFormat("#.000000");
            decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
            double valuesListSize = arr.size();
            
            System.out.println(decimalFormat.format(noOfPlusValues/valuesListSize));
            System.out.println(decimalFormat.format(noOfMinusValues/valuesListSize));
            System.out.println(decimalFormat.format(noOfZeroValues/valuesListSize));
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        Result.plusMinus(arr);

        bufferedReader.close();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *