Plus Minus Hacker Rank Solution – Queslers

Problem: Plus Minus Hacker Rank Solution

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  places after the decimal.

Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to  are acceptable.

Example

There are  elements, two positive, two negative and one zero. Their ratios are ,  and . Results are printed as:

0.400000
0.400000
0.200000

Function Description

Complete the plusMinus function in the editor below.

plusMinus has the following parameter(s):

  • int arr[n]: an array of integers

Print
Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate line with  digits after the decimal. The function should not return a value.

Input Format

The first line contains an integer, , the size of the array.
The second line contains  space-separated integers that describe .

Constraints

Output Format

Print the following  lines, each to  decimals:

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

Sample Input

STDIN           Function
-----           --------
6               arr[] size n = 6
-4 3 -9 0 4 1   arr = [-4, 3, -9, 0, 4, 1]

Sample Output

0.500000
0.333333
0.166667

Explanation

There are 3 positive numbers2 ,  negative numbers 1 , and  zero in the array.
The proportions of occurrence are positive:3/6=0.50000 , negative2/6= 0.33333:  and zeros:1/6=0.166667 .

Plus Minus Hacker Rank Solution Using Python

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the plusMinus function below.
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))

if __name__ == '__main__':
    n = int(input())

    arr = list(map(int, input().rstrip().split()))

    plusMinus(arr)

Plus Minus Hacker Rank Solution Using Java

import java.text.DecimalFormat;
import java.util.Scanner;


public class Solution {
	public static void main(String args[])
	{
		Scanner scan = new Scanner(System.in);
		int N=Integer.parseInt(scan.nextLine());
		int arr[]= new int[N];
		for(int i=0;i<N;i++)
		{
			arr[i]=scan.nextInt();
		}
		scan.close();
		double pos=0;
		double neg=0;
		double zero=0;
		for(int i=0;i<N;i++)
		{
			if(arr[i]>0)
			{
				pos=pos+1;
			}
			else if(arr[i]<0)
			{
				neg=neg+1;
			}
			else
			{
				zero=zero+1;
			}
		}
		DecimalFormat df= new DecimalFormat("#.000");
		System.out.println(df.format(pos/N));
		System.out.println(df.format(neg/N));
		System.out.println(df.format(zero/N));
	}

}

Plus Minus Hacker Rank Solution Using C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


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;
}

Plus Minus Hacker Rank Solution Using C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int N,A[100],iTemp;
    float minus = 0,zeros = 0,plus = 0;
    scanf("%d",&N);
    for(iTemp=0;iTemp<N;iTemp++)
    {
        scanf("%d",&A[iTemp]);
        if(A[iTemp] > 0)
        {
            plus++;    
        }
        else if(A[iTemp] == 0)
        {
            zeros++;    
        }
        else
        {
            minus++;    
        }
        
    }
    printf("%.3f\n%.3f\n%.3f\n",plus/N,minus/N, zeros/N);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Plus Minus Hacker Rank Solution Using JavaScript

function processData(input) {
    //Enter your code here
    input = input.split("\n");
    var n = input.shift();
    input = input.shift().split(' ');
    var len = input.length;
    
    var neg = 0.0;
    var zero = 0.0;
    var pos = 0.0;
    
    input.forEach(function (num) {
        num = parseInt(num);
        if (num < 0) { neg++ }
        else if (num > 0) { pos++ }
        else { zero++ }
    });

    console.log((pos / len).toPrecision(3));
    console.log((neg / len).toPrecision(3));
    console.log((zero / len).toPrecision(3));
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});
Plus Minus Hacker Rank Solution Review:

In our experience, we suggest you solve this Plus Minus Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Plus Minus Problem is available on Hacker Rank for Free, Loops Hacker Rank Problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Plus Minus Hacker Rank Solution.

Conclusion:

I hope this Plus Minus Hacker Rank Solution would be useful for you to learn something new from this problem. I hope this Python: Division Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Staircase Hacker Rank Solution

A Very Big Sum Hacker Rank Solution

Diagonal Difference Hacker Rank Solution

Nested Lists Hacker Rank Solution

Lists Hacker Rank Solution

Leave a Reply

Your email address will not be published. Required fields are marked *