Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int[] playGame(int[] candyArray) {
- for(int i = 0; i < candyArray.length; i++) {
- //last child of the line
- if(i == candyArray.length-1) {
- //dividing candy
- candyArray[i] = (candyArray[i]/2);
- candyArray[0]= (candyArray[0] + (candyArray[i]/2));
- //checking if odd and adding one
- if( (candyArray[0]%2)!=0) {
- candyArray[0]++;
- }
- }
- //children not at end
- if(i != candyArray.length-1) {
- //dividing candy
- candyArray[i] = (candyArray[i]/2);
- candyArray[i+1] = (candyArray[i+1] + (candyArray[i]/2));
- //checking if odd and adding one if true
- if( (candyArray[i+1]%2)!=0) {
- candyArray[i+1]++;
- }
- System.out.print(candyArray[i+1] + " ");
- }
- }
- return candyArray;
- }
- OUTPUT
- 20 20 10 20 6 16 12 16 8 12 16 10 8 4 18
- The game is ready to be played.
- 10 26 16 24 12 20 18 20 14 16 20 16 12 8 20
- 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