Skip to content

Instantly share code, notes, and snippets.

@husnain-iqbal
Created February 15, 2016 16:02
Show Gist options
  • Save husnain-iqbal/963bf7f7e6d0130cbe44 to your computer and use it in GitHub Desktop.
Save husnain-iqbal/963bf7f7e6d0130cbe44 to your computer and use it in GitHub Desktop.
Given an array of integers, calculate which fraction of the elements are positive, negative, and zeroes, respectively. Print the decimal value of each fraction. Input Format The first line, NN, is the size of the array. The second line contains NN space-separated integers describing the array of numbers (A1,A2,A3,⋯,ANA1,A2,A3,⋯,AN). Output Forma…
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) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int arr[] = new int[n];
int posCount = 0 ;
int negCount = 0 ;
int zeroCount = 0 ;
for(int arr_i=0; arr_i < n; arr_i++){
arr[arr_i] = in.nextInt();
if(arr[arr_i] > 0){ posCount++;}
if(arr[arr_i] < 0){ negCount++;}
if(arr[arr_i] == 0){ zeroCount++;}
}
System.out.println((double)posCount/(double)n);
System.out.println((double)negCount/(double)n);
System.out.println((double)zeroCount/(double)n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment