Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void win(int[] k) {
- //finding the highest score path between the odd indexes and the even indexes
- int countOdd = 0;
- int countEven = 0;
- boolean amirTookOddPath;
- for (int i = 0; i < k.length; i++) {
- if (i % 2 == 0)
- countEven += k[i];
- else
- countOdd += k[i];
- }
- amirTookOddPath = countOdd >= countEven ? true : false;
- int min = 0, max = k.length - 1;
- //the odd path is the winning path
- while (min < max) {
- //Amir's path - the highest count of values between the odd indexes and the even indexes
- if (amirTookOddPath ? min % 2 != 0 : min % 2 == 0) {
- System.out.println("Amir took " + k[min]);
- min++;
- } else {
- System.out.println("Amir took " + k[max]);
- max--;
- }
- //Tamar's path - the opposite path of Amir, always choosing the highest value between the two
- if (k[max] > k[min]) {
- System.out.println("Tamar took " + k[max]);
- max--;
- } else {
- System.out.println("Tamar took " + k[min]);
- min++;
- }
- }
- System.out.println("Final score:");
- System.out.println("Amir total " + (amirTookOddPath? countOdd : countEven));
- System.out.println("Tamar total " + (amirTookOddPath? countEven : countOdd));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement