## Friday, April 8, 2016

### Plus minus hackerrank solution - Java-fries

Plus minus hackerrank solution - Java-fries
Given an array of integers, calculate which fraction of the elements are positive, negative, and zeroes, respectively. Print the decimal value of each fraction.

Sample Input:6
-4 3 -9 0 4 1
Sample Output:0.500000
0.333333
0.166667
ExplanationThere are 3 positive numbers, 2 negative numbers, and 1 zero in the array.
The fraction of the positive numbers, negative numbers and zeroes are3/6=0.500000, 2/6=0.333333 and 1/6=0.166667 respectively.
private static void calculateFraction(int[] arr) {
int length = arr.length;
float positiveNumberCount = 0;
float negativeNumberCount = 0;

for (int i = 0; i < length; i++) {
if (arr[i] > 0)
positiveNumberCount++;
else if (arr[i] < 0)
negativeNumberCount++;
}

System.out.printf("%.6f\n", positiveNumberCount / length);
System.out.printf("%.6f\n", negativeNumberCount / length);
System.out.printf("%.6f",
(float) (length - (positiveNumberCount + negativeNumberCount))
/ length);
}