In: Computer Science
Stairs Jumping
One day, Jojo takes a vacation to the mountains to get away from the cities. While climbing the mountain, Jojo came to an area filled with thousands of stairs to get to the top. However, there was a rule in the mountain which stated that Jojo had to jump over the stairs with same height to get to the top of the mountain. Listening to these rules, Jojo wanted to know what is the minimum required height he should jump and the minimum number of jumps to reach the top of the mountain. Note: Jojo is on the first stair and the top of the mountain is on the last stair. Jojo can jump several stairs as long as the difference of the height of the steps does not exceed the height of Jojo’s jump.
Format Input:
There are T test cases. Each testcase contains an integer N which represents numbers of stairs that Jojo had to jump. On the next line there are N numbers where each number represents the height of the stair.
Format Output Output:
T line with format “Case # X: Y Z”, where X represents the testcase number, Y represents minimal height Jojo has to jump, and Z represents minimal number of jumps to reach the top of the mountain.
Constraints
• 1 ≤ T ≤ 50
• 2 ≤ N ≤ 10000
• 0 ≤ Ai ≤ 109 , where Ai represents i-th height of the stair. • Ai < Aj for every index i < j
Sample Input (standard input):
2
5
1 2 3 4 5
5
1 2 3 4 6
Sample Output (standard output):
Case #1: 1 4
Case #2: 2 3
Explanation In case 1, Jojo will jump 1 unit high because the biggest difference between adjacent stairs is 1. Then Jojo will jump 4 times to reach the top.
In case 2, Jojo will jump 2 units high because the biggest difference between adjacent stairs is 2 on the 4-th and 5-th stairs. Then Jojo made 3 jumps to reach the top with the following simulation.
• In jump 1, Jojo will jump from stair 1 to 3, because the height difference between the 1st and 3rd stairs is still less equal than 2.
• On jump 2, Jojo will jump from stair 3 to 4, with a height difference of 1. Jojo cannot jump directly to stair 5 because the difference in height exceeds the height of Jojo’s jump.
• On jump 3, Jojo will jump from stair 4 to 5, with a height difference of 2
NOTE: USE C LANGUAGE, DONT USE FUNCTION(RESULT,RETURN),VOID,RECURSIVE, USE BASIC CODE AND CODE IT UNDER int main (){, constraint must be the same
For this question, C code given below :-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int cases,stairs,d=0,i,count=0,count1=0;
int result[10],result1[10],v[10];
printf("Enter number of test case :"); // get
number of test cases
scanf("%d",&cases);
while(cases != 0 ) // while number of test cases
not equal to zero
{
printf("case
:%d\n",count1+1);
printf("Enter numbers of
stairs :"); // get number of stairs
scanf
("%d",&stairs);
for(i=0; i < stairs; i++)
{
fscanf(stdin, "%d", &v[i]); // get height of the each
stair
}
// now , find difference
between adjacent stairs
d= v[1]-v[0];
for (i=1;i<stairs-1;i++)
{
if (d <= v[i+1]
- v[i])
{
d = v[i+1] - v[i];
}
}
i=0;
if (d==1) // if difference is 1 then total jump
is number of stairs minus 1.
{
result[count1]=d;
result1[count1]=stairs-1;
count1++;
cases--;
}
else
{
while (i < stairs) //if difference is more
then one
{
if (v[i+d] - v[i]
<= d)
{
count++;
i=i+d;
}
else if (v[i+d] -
v[i] > d)
{
if (v[i+d-1] - v[i] <= d)
{
count++;
i=i+d-1;
}
}
}
result[count1]=d;
result1[count1]=count;
count1++;
count=0;
d=0;
cases--;
}
}
for (i=0;i<count1;i++)
{
printf("\nCase #%d
: %d %d",i+1,result[i],result1[i]); // print each case
}
return 0;
}
Screenshot of code and output :-