HackerRank Plus Minus Problem Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Home  coding problems  HackerRank Plus Minus problem solution

HackerRank
CLOSE ADS Plus Minus problem solution CLOSE ADS

 YASH PAL  March 23, 2021

In this HackerRank Plus Minus problem solution, 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.

Search

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.

Subscribe To Channel

Programmingoneonone

YouTube 797

Learn DSA For Free

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
Function Description
Crafted with  by TemplatesYard | Distributed by Blogger
Complete the plusMinus function in the editor below.
CLOSE ADS CLOSE ADS

plusMinus has the following parameter(s):

 int arr[n]: an array of integers


Most Popular Content
Print
HackerRank Mini-Max Sum
Print the ratios of positive, negative and zero values in the array. Each value should be problem solution
printed on a separate line with 6 digits after the decimal. The function should not return  March 23, 2021

a value.
HackerRank Plus Minus
Input Format problem solution
 March 23, 2021

The first line contains an integer, n, the size of the array.


HackerRank Time Conversion
The second line contains n space-separated integers that describe . problem solution
 March 23, 2021
Constraints
HackerRank Diagonal
 0 < n <= 100
Difference problem solution
 -100 <= arr[i] <= 100  March 23, 2021

HackerRank Simple Array Sum


problem solution
Output Format  March 23, 2021

Print the following 3 lines, each to 6 decimals:

1. proportion of positive values

2. proportion of negative values

3. proportion of zeros
CLOSE ADS CLOSE ADS

Problem solution in Python programming.


Code
#!/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)

HackerRank Plus Minus problem solution in Python Progra…


Progra…
CLOSE ADS CLOSE ADS

Problem solution in Java Programming.


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

Problem solution in C++ programming.


Code
#include <cmath>
CLOSE ADS CLOSE ADS
#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;
}

Problem solution in C programming.


Code
#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++;
p ;
}
else if(A[iTemp] == 0)
CLOSE ADS CLOSE ADS
{
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;
}

Problem solution in JavaScript programming.


Code
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));
CLOSE ADS CLOSE ADS
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);
});

Tags: algorithm coding problems

 Facebook  Twitter    

Posted by: YASH PAL


Yash is a Full Stack web developer. he always will to help others. and this approach
takes him to write this page.

You may like these posts


CLOSE ADS CLOSE ADS

HackerRank Smart Number HackerRank XOR Strings 2


problem solution problem solution
 July 29, 2021  July 29, 2021

HackerRank Prime Dates


problem solution
 April 15, 2022

Post a Comment

3 Comments

ASLAN
 October 2, 2021 at 8:53 AM

I didn't store the value of n in total and it gave me wrong answer(c++), don't know why this ones
happening but found out something new today!

Reply Delete

 Replies
Reply
RABIN SAPKOTA
 October 18, 2021 at 5:30 AM

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{

/*
* Complete the 'plusMinus' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/

public static void plusMinus(List arr)


pub c stat c o d p us us( st a )
{
var positiveCount =0;
var negativeCount =0;
CLOSE ADS CLOSE ADS
var zeroCount = 0;
for (var i = 0; i < arr.Count(); i++)
{

if(arr[i] > 0)
{
positiveCount++;
}
else if(arr[i] < 0)
{
negativeCount ++;
}
else
{
zeroCount++;
}
}

Console.WriteLine(((double)positiveCount/arr.Count()).ToString("0.000000"));

Console.WriteLine(((double)negativeCount/arr.Count()).ToString("0.000000"));

Console.WriteLine(((double)zeroCount/arr.Count()).ToString("0.000000"));
}
}

class Solution
{
public static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine().Trim());

List arr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp =>


Convert.ToInt32(arrTemp)).ToList();

Result.plusMinus(arr);
}
}

Reply Delete

 Replies
Reply
NEMTUDOD
 May 23, 2022 at 10:48 AM

Javascript solution doesnt work for any of the hackerrank challenges

Reply Delete

 Replies
Reply

Add comment
To leave a comment, click the button below to sign in with Blogger.

SIGN IN WITH BLOGGER

You might also like