Programs


Title : WAP to return the min number of jumps to reach the end of array

#include <stdio.h>

#include <limits.h>

int noOfJumps(int arr1[], int low, int high)

{

   if (high == low)

       return 0;

   if (arr1[low] == 0)

       return INT_MAX;

   int min = INT_MAX;

   for (int i = low + 1; i <= high && i <= low + arr1[low]; i++)

   {

       int jumps = noOfJumps(arr1, i, high);

       if (jumps != INT_MAX && jumps + 1 < min)

           min = jumps + 1;

   }

   return min;

}

int main()

{

   int arr1[] = {1, 2, 4, 7, 9, 3, 6, 5, 6, 8, 9, 2, 0, 1};

   int n = sizeof(arr1) / sizeof(arr1[0]);

   int i;

   printf("The given array is :  ");

   for (i = 0; i < n; i++)

   {

       printf("%d  ", arr1[i]);

   }

   printf("\n");

   printf("The minimum of number of jumps is required to reach the end is:  %d ", noOfJumps(arr1, 0, n - 1));

   return 0;

}

OUTPUT:

The given array is :  1  2  4  7  9  3  6  5  6  8  9  2  0  1 

The minimum of number of jumps is required to reach the end is:  4

Related Topics

  • Integer x appears more than n by 2 times in sorted array

      Jan 15, 2022

  • Program to find majority element of an array

      Jan 03, 2022

  • Program to print a matrix in spiral form

      Jan 03, 2022

  • WAP to find the maximum circular sub array sum of given array

      Jan 03, 2022

  • Program to count the number of triangles from a given array

      Jan 03, 2022

  • WAP to find the number of times occurs a given number in an array

      Jan 03, 2022

  • Subscribe Now