Question

In: Computer Science

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

Solutions

Expert Solution

C Code

#include

int max(int a, int b) { return (a > b)? a: b; }

// This function will give minimum number of jumps to reach end of the array
int minJumps(int arr[], int n)
{

   // The number of jumps needed to reach the starting index is 0
   if (n <= 1)
       return 0;

   // Return -1 if not possible to jump
   if (arr[0] == 0)
       return -1;

// initialization
   int maxIndex = arr[0]; // stores all time the maximal reachable index in the array.
   int stepSize = arr[0];   // stores the number of steps
   int jump =1;//stores the number of jumps
   int i=1;
   for (i = 1; i < n; i++)
   {
      
// Check if we have reached the end of the array
       if (i == n-1)
           return jump;

      // updating maxIndex
       maxIndex = max(maxIndex, i+arr[i]);

       // we use a stepSize to get to the current index
       stepSize--;

       // If no further steps left
       if (stepSize == 0)
       {
         
// we must have used a jump
           jump++;

           // Check if the current index/position or lesser index
           // is the maximum index point from the previous indexes
           if(i >= maxIndex)
               return -1;

           // make the steps to the amount
           // of steps to reach maxIndex from position i.
           stepSize = maxIndex - i;
       }
   }

   return -1;
}

int main()
{
   int arr[]={3, 3, 5, 6, 0, 2, 6, 7, 6, 8, 9};
   int size = sizeof(arr)/sizeof(int);

   // Calling the minJumps function
   printf("Minimum number of jumps to reach end is %d ", minJumps(arr,size));
   return 0;
}

Input

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,...
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...
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Write a code using c# Maximum Sub Array.
Write a code using c# Maximum Sub Array.
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
Write code that would allocate the space for an array of 20 integers. It will be...
Write code that would allocate the space for an array of 20 integers. It will be pointed to by a variable named "junk" Write code that puts “file did not open” into an error stream.
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT