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...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
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,...
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
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...
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
Given an array A[0 … n-1], where each element of the array represent a vote in...
Given an array A[0 … n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT