FRom hackerrank
Given an array of integers, calculate the fractions of its elements that are positive, negative, and are zeros. Print the decimal value of each fraction on a new line.
Solution in C# -
class Solution {
static void plusMinus(int[] arr) {
float len = arr.Length;
int positive = 0, negative = 0, zero = 0;
foreach(int a in arr)
{
if(a > 0)
positive++;
else if(a < 0)
negative++;
else
zero++;
}
Console.WriteLine(positive/len);
Console.WriteLine(negative/len);
Console.WriteLine(zero/len);
}
static void Main(string[] args) {
int n = Convert.ToInt32(Console.ReadLine());
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp));
plusMinus(arr);
}
}
Post a Comment