알고리즘 단련장/해커랭크

[해커랭크] Plus Minus

snapcoder 2025. 7. 27. 19:12
728x90
반응형
SMALL

https://www.hackerrank.com/challenges/plus-minus/problem?isFullScreen=true

 

Plus Minus | HackerRank

Calculate the fraction of positive, negative and zero values in an array.

www.hackerrank.com

Given an array of integers, calculate the ratios of its elements that are , , and . Print the decimal value of each fraction on a new line with 6 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.

Example

There are  elements: two positive, two negative and one zero. Their ratios are ,  and . Results are printed as:

0.400000
0.400000
0.200000

Function Description

Complete the  function with the following parameter(s):

  • : an array of integers

Print
Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate line with  digits after the decimal. The function should not return a value.

Input Format

The first line contains an integer, , the size of the array.
The second line contains  space-separated integers that describe .

Constraints


Sample Input

STDIN           Function
-----           --------
6               arr[] size n = 6
-4 3 -9 0 4 1   arr = [-4, 3, -9, 0, 4, 1]

Sample Output

0.500000
0.333333
0.166667



 



 

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
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) throws IOException {
    // Write your code here
        int type[] = new int[3];    // + - 0
        for(Integer i : arr){
            if(i>0){
                type[0]++;
            }
            else if(i<0){
                type[1]++;
            }
            else if(i==0){
                type[2]++;
            }
            else{
                // except
                continue;
            }
        }
       
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
       
        String type0 = String.format("%.6f", (type[0]*1.0)/arr.size());
        String type1 = String.format("%.6f", (type[1]*1.0)/arr.size());
        String type2 = String.format("%.6f", (type[2]*1.0)/arr.size());
        bw.write(String.valueOf(type0));
        bw.newLine();
        bw.write(String.valueOf(type1));
        bw.newLine();
        bw.write(String.valueOf(type2));
        bw.close();
        return;
    }

}

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

 

 

 

728x90
반응형
LIST

'알고리즘 단련장 > 해커랭크' 카테고리의 다른 글

[해커랭크] Mini-Max Sum  (0) 2025.08.06
[해커랭크] Staircase  (1) 2025.07.27
[해커랭크] Diagonal Difference  (1) 2025.07.27
[해커랭크] A Very Big Sum  (0) 2025.07.27
[해커랭크] Compare the Triplets  (1) 2025.07.27