-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorldBitsWar.java
36 lines (30 loc) · 959 Bytes
/
WorldBitsWar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.smlnskgmail.jaman.codewarsjava.kyu6;
import java.math.BigInteger;
// https://www.codewars.com/kata/58865bfb41e04464240000b0
public class WorldBitsWar {
private final int[] input;
public WorldBitsWar(int[] input) {
this.input = input;
}
public String solution() {
int oddsResult = 0;
int evensResult = 0;
for (int number : input) {
int bitCount = new BigInteger(String.valueOf(Math.abs(number))).bitCount();
if (number % 2 != 0) {
oddsResult += number < 0 ? -bitCount : bitCount;
} else {
evensResult += number < 0 ? -bitCount : bitCount;
}
}
String result;
if (oddsResult > evensResult) {
result = "odds win";
} else if (evensResult > oddsResult) {
result = "evens win";
} else {
result = "tie";
}
return result;
}
}