daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest Jan 11th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.             /**
  2.              * The game starts
  3.              * By the rules - Amir always starts and Tamar chooses the highest value
  4.              * from the edges of the array
  5.              *@while as long as we have not reached the last index in the array
  6.              * Since sumEven < sumOdd - Amir picks the odd indices
  7.              * Since Amir picks the odd indices, Tamar must choose the even indices.
  8.              * In case we reached the last index the value is added to Tamar's score automatically because it is her turn
  9.              * anyway since Amir has started and the value is printed to us.
  10.              */
  11.             while (start < end) {
  12.                 if (start % 2 != 1) {   //Amir's turn - He can choose from the left side
  13.                     amirScore += arr[start];
  14.                     System.out.println("Amir took " + arr[start]);
  15.                     start++;
  16.                 } else if (end % 2 != 1) {    //Amir's turn - He can choose from the right side
  17.                     amirScore += arr[end];
  18.                     System.out.println("Amir took " + arr[end]);
  19.                     end--;
  20.                 }
  21.                 if (start % 2 == 1) {   //Tamar's turn - She can choose from the left side
  22.                     tamarScore += arr[start];
  23.                     System.out.println("Tamar took " + arr[start]);
  24.                     start++;
  25.                 } else if (end % 2 == 1) {   //Tamar's turn - She can choose from the right side
  26.                     tamarScore += arr[end];
  27.                     System.out.println("Tamar took " + arr[end]);
  28.                     end--;
  29.                 }
  30.                 if (start == end) {   //In case we reached the last index
  31.                     tamarScore += arr[end];
  32.                     System.out.println("Tamar took " + arr[end]);
  33.                     end++;
  34.  
  35.                 }
  36.             } //while loop
  37.         }  // if #2
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top