# HackerRank Plus Minus solved in Java

Today I will try to solve HackerRank “Plus Minus” problem. You can find Plus Minus problem at HackerRank website under Algorithms > Warmup > Plus Minus .

## Problem description

You can read description at Plus Minus problem page. Here is short summary:

*Given an array of integers, calculate fraction elements which can be positive, negative or can be zeroes. Print the decimal value of each fraction on a new line. The test cases are scaled to six decimal places thus round fractions to 6 decimal places.*

*As an input, the first line will contains an integer denoting the size of the array. The second line will contains space-separated integers describing an array of numbers.*

*Output should have 3 lines: The first line will be decimal representing of the fraction of positive numbers in the array, the second will be decimal representing of the fraction of negative numbers in the array and the third will be decimal representing of the fraction of zeroes in the array.*

## Solution

As in the previous problem it is important to realize how the HackerRank input data to user. It is faking user input over Scanner class. As description state: “*The first line will contains an integer denoting the size of the array. The second line will contains space-separated integers describing an array of numbers*“.

I foolishly hurry at this problem and while reading description I skipped information about input. It took my a while to realize that I am doing something wrong. For a long time I was thinking how to establish an Array in Java without setting its size if the HackerRank will provide only the numbers for array. Of course it is not possible and I should more carefully read description. For an Array it is always necessary to set its size. Without knowing the array size, ArrayList is suitable class for storing primitive data types and objects.

### Scanner class

Something I learned on this exercise is that Scanner class breaks its input into tokens using a delimiter pattern, which by default matches whitespace. This means that resulting tokens may then be converted into values of different types by using the various `next`

methods.

## Custom solution

Besides passing the right data from HackerRank making a solution is pretty easy:

**Establish variables**. Obtain data from HackerRank and set custom variables (positives, negatives, zeroes, array) .**Looping trough the input array in order to decide which fraction should be incremented**.**Count the fractions**. This step can be made along with next step.**Print the results**.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int arrSize = in.nextInt(); int arr[] = new int[arrSize]; int positive = 0; int negative = 0; int zero = 0; for (int i = 0; i < arrSize; i++) { arr[i] = in.nextInt(); if (arr[i] > 0) { positive++; } else if (arr[i] < 0) { negative++; } else { zero++; } } System.out.format("%.6f\n", (float)positive/arrSize); System.out.format("%.6f\n", (float)negative/arrSize); System.out.format("%.6f\n", (float)zero/arrSize); } } |

Only interesting thing on this solution is the end. We are dividing each fraction with array size in order to find its decimal value. Simultaneously we are casting float on result since integer divided by integer would make only integer as result. Casting `(float)`

will create floating value.

Finally we will set the result in appropriate format with `System.out.format()`

. Java documentation page contains all important explantations about percent signs. `%.6f`

means that I want to use float variable with 6 decimal places after the floating point. Using `System.out.printf()`

will make the same result. To make new line you need to just insert `\n`

on the of format.

*Note*: I did not test it but I guess `System.out.format`

can be squeezed to one line padded with all the results in appropriate order.

## Good taste solution

I have checked other solutions and I think solution below can fulfill the idea of good taste. First, it is using DecimalFormat class instance for formatting the result right away.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.io.*; import java.util.*; import java.text.DecimalFormat; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); DecimalFormat df = new DecimalFormat("#.######"); float arrSize, positive = 0, negative = 0, zero = 0, temp = 0; arrSize = in.nextInt(); for(int i = 0; i < arrSize; i++) { temp = in.nextFloat(); if(temp > 0) positive++; else if(temp < 0) negative++; else zero++; } System.out.println(df.format(positive/arrSize)); System.out.println(df.format(negative/arrSize)); System.out.println(df.format(zero/arrSize)); } } |

Interestingly this code save space by not creating another `int`

variable for temp. It rather use `float`

in same line while setting other variables. Than it is using `nextFloat()`

which output is basically the same as if the variables were `int`

and `nextInt()`

function would be used in the loop.