This topic contains 1 reply, has 1 voice, and was last updated by  Nav Singh 1 year, 8 months ago.

  • Author
  • #4448 Reply

    Nav Singh

    Given an array of integers, calculate which fraction of its elements are positive, which fraction of its elements are negative, and which fraction of its elements are zeroes, respectively. Print the decimal value of each fraction on a new line.

    Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to are acceptable.

    Input Format

    The first line contains an integer, , denoting the size of the array.
    The second line contains space-separated integers describing an array of numbers .

    Output Format

    You must print the following lines:

    A decimal representing of the fraction of positive numbers in the array.
    A decimal representing of the fraction of negative numbers in the array.
    A decimal representing of the fraction of zeroes in the array.
    Sample Input

    -4 3 -9 0 4 1
    Sample Output


    There are positive numbers, negative numbers, and zero in the array.
    The respective fractions of positive numbers, negative numbers and zeroes are , and , respectively.

  • #4454 Reply

    Nav Singh
    import java.util.*;
    public class Solution {
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner in = new Scanner(;
            int la = in.nextInt();
            int arr[] = new int[la];
            int negativeCount = 0;
            int zeroCount = 0;
            int positiveCount = 0;
            for(int i=0; i<la;i++){
            arr[i] = in.nextInt();
                int n = arr[i];
              if(n < 0){
              negativeCount += 1;  
            else if(n ==0){
              zeroCount += 1;
             positiveCount += 1;  
            double fractionForPositiveNumbers = (double)positiveCount/(double)la;
            double fractionForNegativeNumbers = (double)negativeCount/(double)la;
            double fractionForZero = (double)zeroCount/(double)la;
Reply To: Plus Minus
Your information: