Complete the code to check if the array length is zero.
if ([1] == 0) { return 1; }
The variable length holds the size of the array. Checking if it is zero means the array is empty.
Complete the code to update the maximum reachable index.
maxReach = (maxReach > i + nums[i]) ? maxReach : [1];We update maxReach to the farthest index reachable from current position i, which is i + nums[i].
Fix the error in the loop condition to iterate over the array correctly.
for (int i = 0; i [1] length; i++) { if (i > maxReach) return 0; maxReach = (maxReach > i + nums[i]) ? maxReach : i + nums[i]; }
The loop should run while i is less than length to avoid going out of bounds.
Fill both blanks to complete the function signature and return statement.
int canJump([1] nums[], int [2]) { int maxReach = 0; for (int i = 0; i < length; i++) { if (i > maxReach) return 0; maxReach = (maxReach > i + nums[i]) ? maxReach : i + nums[i]; } return 1; }
bool instead of int for parameters.The function takes an integer array nums and its length length as parameters.
Fill all three blanks to complete the main function that tests the jump game.
int main() {
int nums[] = {2, 3, 1, 1, 4};
int [1] = sizeof(nums) / sizeof(nums[0]);
int result = canJump(nums, [2]);
if (result [3] 1) {
printf("Can reach the end\n");
} else {
printf("Cannot reach the end\n");
}
return 0;
}We calculate the array length as length, pass it to canJump, and check if the result equals 1.