Skip to content

Commit accf9cf

Browse files
authoredOct 5, 2020
Merge pull request #85 from shikhar8434/master
Added PlusMinus Problem(HACKERRANK)
2 parents 841b440 + 2d45ccf commit accf9cf

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.*;
2+
import java.math.*;
3+
import java.security.*;
4+
import java.text.*;
5+
import java.util.*;
6+
import java.util.concurrent.*;
7+
import java.util.regex.*;
8+
9+
public class Solution {
10+
11+
12+
static void plusMinus(int[] arr) {
13+
14+
int dimension = arr.length;
15+
double positives = 0;
16+
double negatives = 0;
17+
double zeros = 0;
18+
19+
for (int i = 0; i < dimension; i++) {
20+
if (arr[i] > 0) {
21+
positives++;
22+
} else if (arr[i] < 0) {
23+
negatives++;
24+
} else {
25+
zeros++;
26+
}
27+
}
28+
29+
positives /= dimension;
30+
negatives /= dimension;
31+
zeros /= dimension;
32+
33+
System.out.printf("%.6f\n%.6f\n%.6f\n", positives, negatives, zeros);
34+
35+
}
36+
37+
private static final Scanner scanner = new Scanner(System.in);
38+
39+
public static void main(String[] args) {
40+
int n = scanner.nextInt();
41+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
42+
43+
int[] arr = new int[n];
44+
45+
String[] arrItems = scanner.nextLine().split(" ");
46+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
47+
48+
for (int i = 0; i < n; i++) {
49+
int arrItem = Integer.parseInt(arrItems[i]);
50+
arr[i] = arrItem;
51+
}
52+
53+
plusMinus(arr);
54+
55+
scanner.close();
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# PLUS MINUS HACKERRANK PROBLEM
3+
4+
## Problem statement
5+
6+
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 with 6 places after the decimal.
7+
8+
e.g., array =[1,1,0,-1,-1] i.e., n=5 elements
9+
10+
so positives= 2/5, negatives= 2/5 and zeros = 1/5
11+
12+
then Output :
13+
0.400000
14+
0.400000
15+
0.200000

0 commit comments

Comments
 (0)
Failed to load comments.