fork download
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. class Main {
  5. public static void main(String[] args) {
  6. int[] arr = {1, 2, 3, 4, 5, 6};
  7.  
  8. // Copy elements into a dynamic ArrayList so we can physically modify it
  9. ArrayList<Integer> list = new ArrayList<>();
  10. for (int num : arr) {
  11. list.add(num);
  12. }
  13.  
  14. int p1 = 0;
  15. int p2 = 0;
  16. int turn = 1;
  17.  
  18. // Loop runs until the list is completely empty
  19. while (!list.isEmpty()) {
  20. // Brute Force Rule: Always select the first element of the current array
  21. int selected = list.remove(0);
  22.  
  23. // Assign score based on whose turn it is
  24. if (turn % 2 != 0) {
  25. p1 += selected;
  26. } else {
  27. p2 += selected;
  28. }
  29.  
  30. // Rule: If the latest removed element is even, reverse the remaining array
  31. if (selected % 2 == 0) {
  32. Collections.reverse(list);
  33. }
  34.  
  35. turn++;
  36. }
  37.  
  38. // Calculate and print the score difference
  39. int scoreDifference = p1 - p2;
  40.  
  41. System.out.println("Player 1 Score: " + p1);
  42. System.out.println("Player 2 Score: " + p2);
  43. System.out.println("Score Difference (P1 - P2): " + scoreDifference);
  44. }
  45. }
  46.  
Success #stdin #stdout 0.1s 55640KB
stdin
Standard input is empty
stdout
Player 1 Score: 11
Player 2 Score: 10
Score Difference (P1 - P2): 1