HackerRank Plus Minus Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Plus Minus Solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank Plus Minus Solution
HackerRank Plus Minus Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Plus Minus Solution

Problem

Given an array of integers, 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.

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

Example

arr = [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

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 6 digits after the decimal. The function should not return a value.

Input Format

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

Constraints

  • 0 < n <= 100
  • -100 <= arr[i] <= 100

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 numbers, 2 negative numbers, and 1 zero in the array.
The proportions of occurrence are positive: 3/6 = 0.500000, negative: 2/6 = 0.333333 and zeros: 1/6 = 0.166667.

HackerRank Plus Minus Solution

Plus Minus Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int N , a[100],i;
    float f1=0,f2=0,f3=0;
    scanf("%d",&N);
    for(i=0;i<N;i++)
 {       scanf("%d",&a[i]);
  if(a[i]>0)
      f1++;
  else if(a[i]<0)
      f2++;
      else
      f3++;
 }
    printf("%f\n%f\n%f",f1/N,f2/N,f3/N);
    return 0;
}

Plus Minus Solution in Cpp

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    int p=0,n=0,z=0,a,i,j;
    cin>>j;
    for(i=0;i<j;i++){
        cin>>a;
        
        if(a>0)
            p++;
        else if(a<0)
            n++;
        else
            z++;
    }
    
    printf("%.3f\n",(float)p/j);
    printf("%.3f\n",(float)n/j);
    printf("%.3f",(float)z/j);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}

Plus Minus Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int pos = 0;
        int zero = 0;
        int neg = 0;
        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
            if (x > 0) {
                pos++;
            } else if (x == 0) {
                zero++;
            } else {
                neg++;
            }
        }
        System.out.println(pos / (double) n);
        System.out.println(neg / (double) n);
        System.out.println(zero / (double) n);
    }
}
Ezoicreport this ad

Plus Minus Solution in Python

T=int(input())
lists=[int(_) for _ in input().split()]
p=0
m=0
z=0
for ele in lists:
    if(ele>0):
        p+=1
    else:
        if(ele<0):
            m+=1
        else:
            z+=1
p=p/T
m=m/T
z=z/T
round(p,3)
round(m,3)
round(z,3)
print(p)
print(m)
print(z)

Plus Minus Solution JavaScript

function processData(input) {
    var ls = input.split("\n");
    var n = parseInt(ls[0]);
    var gt=0,lt=0,z=0;
    var ns = ls[1].split(" ").forEach(function(v){
        v = parseInt(v);
        if(v < 0){
            lt++;
        } else if(v > 0){
            gt++;
        } else {
            z++;
        }
    });
    console.log(Math.round((gt/n)*1000)/1000);
    console.log(Math.round((lt/n)*1000)/1000);
    console.log(Math.round((z/n)*1000)/1000);
} 
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 Solution in Scala

object Solution {
    def main(args: Array[String]) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
*/
        val n = readInt
        val ar = readLine.split(" ").map(_.toInt)
        val neg = ar.filter(_ < 0).length.toFloat
        val pos = ar.filter(_ > 0).length.toFloat
        val zeros = n - neg - pos
        println("%.5f".format(pos/n))
        println("%.5f".format(neg/n))
        println("%.5f".format(zeros/n))
    }
}

Ezoicreport this adPlus Minus Solution in Pascal

(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
var n,p,ne,z,a,i:longint;
begin
  readln(n);
  p:=0;
  ne:=0;
  z:=0;
  for i:=1 to n do
  begin
    read(a);
    if a<0 then inc(ne)
    else if a=0 then inc(z)
    else inc(p);
  end;
  writeln(p/(p+ne+z):5:3);
  writeln(ne/(p+ne+z):5:3);
  writeln(z/(p+ne+z):5:3);
end.

Disclaimer: This problem (Plus Minus) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Apple and Orange Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad