All puzzles
Per page
V
Array · New array · Index math

A reversed copy

Return a new array with the numbers of nums in reverse order. Do not change nums.

reversed(new int[] {1, 2, 3, 4}) returns {4, 3, 2, 1}.

8 strips: V1 to V8

}
V1
return result;
V2
result[i] = nums[nums.length - i];
V3
for (int i = 0; i < nums.length; i++) {
V4
int[] result = new int[nums.length];
V5
result[i] = nums[nums.length - 1 - i];
V6
public static int[] reversed(int[] nums) {
V7
}
V8

Answer key: V. A reversed copy

Order: V7, V5, V4, V6, V8, V2, V1. Extra: V3

public static int[] reversed(int[] nums) {
	int[] result = new int[nums.length];
	for (int i = 0; i < nums.length; i++) {
		result[i] = nums[nums.length - 1 - i];
	}
	return result;
}

Extra strip: result[i] = nums[nums.length - i];. When i is 0, nums[nums.length] is one past the last index, so it throws an ArrayIndexOutOfBoundsException.