Skip to content

Commit cff21d7

Browse files
solves plus minus
1 parent 6f8f964 commit cff21d7

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ I have also solved other domains on HackerRank that can be viewed at:
6262
| [Simple Array Sum](https://www.hackerrank.com/challenges/simple-array-sum) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/hackerrank-algorithms/blob/master/src/warmup/SimpleArraySum.java) |
6363
| [Compare The Triplets](https://www.hackerrank.com/challenges/compare-the-triplets) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/hackerrank-algorithms/blob/master/src/warmup/CompareTheTriplets.java) |
6464
| [A very Big Sum](https://www.hackerrank.com/challenges/a-very-big-sum) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/hackerrank-algorithms/blob/master/src/warmup/AVeryBigSum.java) |
65-
| [Diagonal Difference](https://www.hackerrank.com/challenges/diagonal-difference) | Easy | |
66-
| [Plus Minus](https://www.hackerrank.com/challenges/plus-minus) | Easy | |
65+
| [Diagonal Difference](https://www.hackerrank.com/challenges/diagonal-difference) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/hackerrank-algorithms/blob/master/src/warmup/DiagonalDifference.java) |
66+
| [Plus Minus](https://www.hackerrank.com/challenges/plus-minus) | Easy | []() |
6767
| [Staircase](https://www.hackerrank.com/challenges/staircase) | Easy | |
6868
| [Mini-Max Sum](https://www.hackerrank.com/challenges/mini-max-sum) | Easy | |
6969
| [Birthday Cake Candles](https://www.hackerrank.com/challenges/birthday-cake-candles) | Easy | |

src/warmup/PlusMinus.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package warmup;
2+
3+
import java.util.Scanner;
4+
5+
public class PlusMinus {
6+
private static final Scanner scanner = new Scanner(System.in);
7+
8+
public static void main(String[] args) {
9+
int length = scanner.nextInt();
10+
int[] array = getArray(length);
11+
printFractions(array);
12+
}
13+
14+
private static int[] getArray(int length) {
15+
int[] array = new int[length];
16+
for (int index = 0 ; index < array.length ; index++) {
17+
array[index] = scanner.nextInt();
18+
}
19+
return array;
20+
}
21+
22+
private static void printFractions(int[] array) {
23+
double positives = getPositives(array);
24+
double negatives = getNegatives(array);
25+
double zeros = array.length - positives - negatives ;
26+
System.out.println(positives / array.length);
27+
System.out.println(negatives / array.length);
28+
System.out.println(zeros / array.length);
29+
}
30+
31+
private static double getPositives(int[] array) {
32+
int count = 0;
33+
for (int number : array) {
34+
if (number > 0) count++;
35+
}
36+
return count;
37+
}
38+
39+
private static double getNegatives(int[] array) {
40+
int count = 0;
41+
for (int number : array) {
42+
if (number < 0) count++;
43+
}
44+
return count;
45+
}
46+
}

0 commit comments

Comments
 (0)