Plus Minus HackerRank Solution in C, C++, Java, Python

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

100<=arr[i]<=100

Output Format

Print the following 3 lines, each to 6 decimals:

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

Sample Input

6

-4 3 -9 0 4 1

Sample Output

0.500000

0.333333

0.166667

Plus Minus HackerRank Solution in C

#include
#include
#include
#include
int main() {
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 HackerRank Solution in C++

#include
#include
#include
#include
#include
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);
return 0;
}

Plus Minus HackerRank 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);
}
}

Plus Minus HackerRank Solution in Python

from __future__ import division
n = input()
arr = map(int,raw_input().split())
c1 = len(filter(lambda x:x>0,arr))
c2 = len(filter(lambda x:x<0,arr))
c3 = len(filter(lambda x:x==0,arr))
print “%.7f” % (c1/n)
print “%.7f” % (c2/n)
print “%.7f” % (c3/n)

Plus Minus HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
string firstLine = Console.ReadLine();
int length = Convert.ToInt32(firstLine);
string nextLine = Console.ReadLine();
string[] brokenLine = nextLine.Split(new char[] { ‘ ‘ });
int positiveCount = 0;
int negativeCount = 0;
int zero = 0;
for (int i = 0; i < length; i++)
{
if (Convert.ToInt32(brokenLine[i]) > 0)
{
positiveCount++;
}
else if (Convert.ToInt32(brokenLine[i]) < 0)
{
negativeCount++;
}
else
{
zero++;
}
}
Console.WriteLine(Math.Round((double)positiveCount / length, 3));
Console.WriteLine(Math.Round((double)negativeCount / length, 3));
Console.WriteLine(Math.Round((double)zero / length, 3));
}
}

 

Attempt – Plus Minus HackerRank Challenge

Link – https://www.hackerrank.com/challenges/plus-minus

Next Challenge – Staircase HackerRank Solution

Link – https://exploringbits.com/staircase-hackerrank-solution/

Leave a Comment