Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. public int[] playGame(int[] candyArray) {
  2.  
  3. for(int i = 0; i < candyArray.length; i++) {
  4. //last child of the line
  5. if(i == candyArray.length-1) {
  6. //dividing candy
  7. candyArray[i] = (candyArray[i]/2);
  8. candyArray[0]= (candyArray[0] + (candyArray[i]/2));
  9.  
  10. //checking if odd and adding one
  11. if( (candyArray[0]%2)!=0) {
  12.  
  13.  
  14. candyArray[0]++;
  15. }
  16.  
  17. }
  18. //children not at end
  19. if(i != candyArray.length-1) {
  20.  
  21. //dividing candy
  22. candyArray[i] = (candyArray[i]/2);
  23. candyArray[i+1] = (candyArray[i+1] + (candyArray[i]/2));
  24.  
  25.  
  26. //checking if odd and adding one if true
  27. if( (candyArray[i+1]%2)!=0) {
  28.  
  29.  
  30. candyArray[i+1]++;
  31. }
  32. System.out.print(candyArray[i+1] + " ");
  33. }
  34. }
  35.  
  36. return candyArray;
  37. }
  38.  
  39.  
  40.  
  41.  
  42. OUTPUT
  43.  
  44. 20 20 10 20 6 16 12 16 8 12 16 10 8 4 18
  45. The game is ready to be played.
  46. 10 26 16 24 12 20 18 20 14 16 20 16 12 8 20
  47. 16 13 8 12 6 10 9 10 7 8 10 8 6 4 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement