The **Plus-Minus** challenge is a standard data classification problem on HackerRank. Given an array of integers, you must calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with six places after the decimal.

In this guide, we will implement this classification using Java 8 Stream filtering and display formatting.

Illustration representing math ratios and double values
Real-World Analogy: Sorting Candy

Imagine having a bag containing 6 candies: 3 Red (Positive), 2 Blue (Negative), and 1 Green (Zero):

  • To find the ratio of Red candy, you count the Reds and divide by the total: 3 / 6 = 0.500000.
  • To find the ratio of Blue candy: 2 / 6 = 0.333333.
  • To find the ratio of Green candy: 1 / 6 = 0.166667.

1. Filtering with Streams

Using Java 8 Streams, we can apply filter() on the list to separate positive, negative, and zero elements, and call count() to count them:

double countNegative = arr.stream().filter(element -> element < 0).count();
double countPositive = arr.stream().filter(element -> element > 0).count();
double countZero     = arr.stream().filter(element -> element == 0).count();

By declaring these variables as double, we avoid integer division issues (where 3/6 would evaluate to 0 instead of 0.5).

2. Formatting Decimals

To print the output rounded to exactly six decimal places, use String.format() with the format specifier %.6f:

System.out.println(String.format("%.6f", (countPositive / arr.size())));
System.out.println(String.format("%.6f", (countNegative / arr.size())));
System.out.println(String.format("%.6f", (countZero / arr.size())));

Java Implementation

Below is the complete Java code solving the challenge:

package io.practise.hackerrank;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;

public class PlusMinusRatio {
    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());

        plusMinus(arr);
        bufferedReader.close();
    }

    private static void plusMinus(List<Integer> arr) {
        double total = arr.size();
        double countNegative = arr.stream().filter(element -> element < 0).count();
        double countPositive = arr.stream().filter(element -> element > 0).count();
        double countZero     = arr.stream().filter(element -> element == 0).count();

        System.out.println(String.format("%.6f", countPositive / total));
        System.out.println(String.format("%.6f", countNegative / total));
        System.out.println(String.format("%.6f", countZero / total));
    }
}

Conclusion

Classifying elements inside arrays is simple using Java Streams. By combining filter() with double casting, you create clean, declarative logic that is easy to read and formats numbers correctly for competitive submissions.