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.
Input Format
The first line contains an integer, N, the size of the array. The second line contains N space-separated integers of the array.
Output Format
Print the following 3 lines, each to decimals (use double in java):
proportion of positive values
proportion of negative values
proportion of zeros
Sample Input
6 -4 3 -9 -5 4 1
Sample Output
0.5 0.5 0
Explanation
There are 3 positive numbers, 3 negative numbers, and no zero in the array.
The proportions of occurrence are positive: 0.5, negative: 0.5 and zeros: 0.
In case you are solving in C++, setprecision to 3
Solution:–
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { //your code here Scanner scanner = new Scanner(System.in); int input = scanner.nextInt(); int arr[] = new int[input]; float positiveCount=0,negativeCount=0,zeroCount=0; for(int i=0;i<input;i++) { int x = scanner.nextInt(); arr[i]=x; } for(int i=0;i<input;i++) { if(arr[i]>0) { positiveCount++; } else if(arr[i]<0) { negativeCount++; } else{ zeroCount++; } } System.out.println((double)positiveCount/input); System.out.println((double)negativeCount/input); System.out.println(zeroCount/input); } }
Add a Comment