HackerRank Plus Minus Problem Solution
HackerRank Plus Minus Problem Solution
HackerRank Plus Minus Problem Solution
HackerRank
CLOSE ADS Plus Minus problem solution CLOSE ADS
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
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
a value.
HackerRank Plus Minus
Input Format problem solution
March 23, 2021
3. proportion of zeros
CLOSE ADS CLOSE ADS
import math
import os
import random
import re
import sys
if __name__ == '__main__':
n = int(input())
plusMinus(arr)
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++;
}
return 0;
}
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;
}
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);
});
Facebook Twitter
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.
*/
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());
Result.plusMinus(arr);
}
}
Reply Delete
Replies
Reply
NEMTUDOD
May 23, 2022 at 10:48 AM
Reply Delete
Replies
Reply
Add comment
To leave a comment, click the button below to sign in with Blogger.