Monday, June 29, 2015

Plus Minus

Problem Statement
You're given an array containing integer values. You need to print the fraction of count of positive numbers, negative numbers and zeroes to the total numbers. Print the value of the fractions correct to 3 decimal places.
Input Format
First line contains N, which is the size of the array.
Next line contains N integers A1,A2,A3,,AN, separated by space.
Sample Input
6
-4 3 -9 0 4 1          
Sample Output
0.500
0.333
0.167
Explanation
There are 3 positive numbers, 2 negative numbers and 1 zero in the array.
Fraction of the positive numbers, negative numbers and zeroes are 36=0.50026=0.333and 16=0.167 respectively.



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) {double negative=0;
double positive=0;
double zero=0;
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int [] arr=new int[n];
for(int m:arr)
{
m=scn.nextInt();
if(m==0)
{
zero++;
}
else if(m<0)
{
negative++;
}
else if(m>0)
{
positive++;
}
}
System.out.println(positive/n);
System.out.println(negative/n);
System.out.println(zero/n);
    }
}

0 comments:

Post a Comment