HackerRank - Solving the problem

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:

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

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.

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.