Menu Close

Find the number of potion must the character take to jump the hurdles

A video player plays a game in which the character competes in a hurdle race. Hurdles are of varying heights, and the characters have a maximum height they can jump.

There is a magic potion they can take that will increase their maximum jump height by ‘1’ unit for each dose. How many doses of the potion must the character take to be able to jump all of the hurdles.

If the character can already clear all of the hurdles, return 0.

Constraints:
1<=n, k<=10
1<=height[i] <=100

Input:
The first line contains two integers 'n' and 'k', the number of hurdles, and the maximum height the character can jump naturally.

The second line contains 'n' space-separated integers height[i] where 0<=i<n.

Output:
Print the No: of potion must the character take to be able to jump all of the hurdles. 
If the character can already clear all of the hurdles, return 0.

Explanation: 
height = [ 1, 2, 3, 3, 2] k=1

The character can jump 1 unit high initially and must take 3-1=2 doses of potion to be able to jump all of the hurdles.

#include <stdio.h>
#include <stdlib.h>
int main()
{ int *h;
  int i,n,k,max=0;
  scanf("%d%d",&n,&k);
  h=(int *)malloc(n*sizeof(int));
  for(i=0;i<n;i++)
  {
    scanf("%d", h);
    if(max < *h) max = *h;
    h++;
  }
  printf("%d",max-k);
return 0;
}

INPUT_1:
10  4
1  6  3  5  2  8  7  9  4  2

OUTPUT:
5


INPUT_2:
5  5
2  15  4  5  2

OUTPUT:
10


INPUT_3:
8  3
2  2  5  10  2  3  5  6

OUTPUT:
7


ILLUSTRATION

Executed using gcc

Morae Q!