Question

In: Computer Science

Input: An array of non-negative integers, where each element in the array represents your maximum jump...

Input: An array of non-negative integers, where each element in the array represents your maximum jump length at that position.

Output: A boolean value if you are able to reach the last index starting if you start at the first spot in the array.

Example 1:

  • Input: [2,4,1,2,4,1]
  • Output: True (Ex. 0 to 1 to 5 or 0 to 2 to 3 to 5)

Example 2:

  • Input: [3,2,1,0,4]
  • Output: false (You will always arrive at, and get stuck at, index 3)

Solutions

Expert Solution

#include<iostream>
using namespace std;
int main()
{
    int a[100],n=0,i=0,sum=0;
    cout<<endl<<"Enter a non negative number(-ve number to terminate input)";
    //infinite loop to input non negative numbers
while(1)
{  
    cin>>a[n];//stor ethe number
    if(a[n]<0) //validate the number
    {
        n--;//if -ve discard the number and decrease the index n
        break;//terminate the loop
       }
    n++;//increase the index
   }
   //loop to process the array values
   for(i=0;i<n;)
   {
   //update the value of i by adding the jump lengt
       i=i+a[i];
       if(i==(i+a[i])) //if value of i and i+a[i] equal then stop the loop because
       //you will be unable to movce forward
       break;
   }
   if(i==n) //if v;alue of i match with value of n then return n
   cout<<endl<<"TRUE";//display true
   else
   if(i>n) //if value of i exceeds to n then you are out of array
   cout<<endl<<"FALSE(You will go out of range)";//display false with error message
   else //display the message because you will be unable tomove forward
       cout<<endl<<"FALSE(You will always arrive at, and get stuck at index "<<i<<")";
}

OUTPUT


Related Solutions

Input: An array of non-negative integers, where each element in the array represents your maximum jump...
Input: An array of non-negative integers, where each element in the array represents your maximum jump length at that position. Output: A boolean value if you are able to reach the last index starting if you start at the first spot in the array. [Please write a recursion function!] Example 1: Input: [2,4,1,2,4,1] Output: True (Ex. 0 to 1 to 5 or 0 to 2 to 3 to 5) Example 2: Input: [3,2,1,0,4] Output: false (You will always arrive at,...
write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
The program shall take two integer arrays as input. Each array represents a non-negative number. Each...
The program shall take two integer arrays as input. Each array represents a non-negative number. Each element in the array is one digit however when all digits in the array are combined a number is formed. See example below: int Array1 = {4,5,6,7} represents number 7654 int Array2 = {2,3,4,5} represents number 5432 You will add the two numbers i.e., 7654 + 5432 = 13086 and store it in a new array similar to how the numbers were stored earlier...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function round(). Check your code with the following arrays: >> y1 = [18, -5, 89, -7, 4, 10, 12,...
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
In python write a program that gets a list of integers from input, and outputs non-negative...
In python write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest). Ex: If the input is: 10 -7 4 39 -6 12 2 the output is: 2 4 10 12 39 For coding simplicity, follow every output value by a space. Do not end with newline
How would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array.
JAVA ProgrammingHow would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array. index 4 is the 5th smallest element in the array, index 9 is the 10th smallest element in the array and so on...- this array could be small (like 5 indexes) or large (like 100,000 indexes).- all other numbers do not change position
main() gets user input for size and elements of arr[]. . Check each element for negative,...
main() gets user input for size and elements of arr[]. . Check each element for negative, positive and zero and create neg[], pos[] and zer[] accordingly in main() only.
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console. In C++. Thank you!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT