# Hacker Rank Plus Minus problem solution 2022

hackerrank-plus-minus-solution

In this HackerRank Plus Minus problem solution, you have Given an array of integers, and calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with 6 places after the decimal. Let’s first see the actual problem:

## Hacker Rank Plus Minus Orignal Problem:

Given an array of integers, calculate the ratios of its elements that are positivenegative, and zero. Print the decimal value of each fraction on a new line with

6

places after the decimal.
Example 1 :

array = [1, 1, 0, -1, -1]

There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:

0.400000
0.400000
0.200000

1. proportion of positive values
2. proportion of negative values
3. proportion of zeros

Example 2 :

array = [-4, 3, -9, 0, 4, 1]

There are 3 positive numbers, 2 negative numbers, and 1 zero in the array. The following is the answer :

3/6 = 0.500000
2/6 = 0.333333
1/6 = 0.166667

## Hacker Rank Plus Minus Problem Solution:

### Javascript Solution:

function plusMinus(arr) {
let pos=0, neg=0, zero=0;
for(let i =0; i<=arr.length;i++) {
if(arr[i]>0)
{
pos++
}
else if(arr[i]<0)
{
neg++
}
else if(arr[i]==0)
{
zero++
}
}
console.log((pos/arr.length).toFixed(6));
console.log((neg/arr.length).toFixed(6));
console.log((zero/arr.length).toFixed(6));
}

### Problem solution in Java

public static void plusMinus(List<Integer> arr) {
int pos = 0;
int neg = 0;
int zero = 0;
int n = arr.size();
for(int number = 0; number < n; number++){
if(arr.get(number) == 0){
zero++;
}else if(arr.get(number) > 0){
pos++;
}else{
neg++;
}
}
double posValue = (double)pos/n;
double negValue = (double)neg/n;
double zeroValue = (double)zero/n;

System.out.println(posValue);
System.out.println(negValue);
System.out.println(zeroValue);

}

}

### Problem solution in C++

int main() {
int N, n, total;
float pos = 0., neg = 0., zer = 0.;

cin >> N;

total = N;

while (N--) {
cin >> n;
if (n > 0) pos++;
else if (n < 0) neg++;
else zer++;
}

cout << pos / total << endl;
cout << neg / total << endl;
cout << zer / total << endl;

return 0;
}

## Problem solution in Python

def plusMinus(arr):
x,z,y=0,0,0
for i in range(0,len(arr)):
if arr[i]>0:
x = x + 1
elif arr[i]<0:
y = y + 1
else:
z = z + 1
print(x/len(arr))
print(y/len(arr))
print(z/len(arr))