Loading...
Loading...
Loading...
A music app stores a playlist as an array of song IDs. The "reverse" button must turn the same array around, without making a second array. Reverse it in place and print it.
Example 1
Input
5 11 22 33 44 55
Output
55 44 33 22 11
Swap 11 with 55, then 22 with 44; 33 stays in the middle.
Example 2
Input
4 1 2 3 4
Output
4 3 2 1
With an even length every song moves.
Try the problem first. Open one hint at a time.
Use two indexes: left starts at 0 and right at n - 1.
Swap the two songs with a temporary variable, then move left forward and right back. Stop when they meet.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor