Skip to content

Commit 1b528b4

Browse files
authored
Add files via upload
1 parent 19e1e78 commit 1b528b4

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Minimum number of jumps
3+
Given an array of integers where each element represents the max number of steps that can be made forward from that element. Write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then cannot move through that element.
4+
5+
Input:
6+
The first line contains an integer T, depicting total number of test cases.
7+
Then following T lines contains a number n denoting the size of the array.
8+
Next line contains the sequence of integers a1,a2,...,an.
9+
10+
Output:
11+
Each separate line showing the minimum number of jumps. If answer is not possible print -1.
12+
13+
Constraints:
14+
1<=T<=30
15+
1<=N<=100
16+
0<=a[N]<=100
17+
1
18+
11
19+
1 3 5 8 9 2 6 7 6 8 9
20+
3
21+
1
22+
11
23+
2 4 6 1 3 5 4 6 7 8 9
24+
3
25+
code by @ shivendra k Jha
26+
*/
27+
28+
29+
#include <iostream>
30+
using namespace std ;
31+
int main ()
32+
{
33+
int t;
34+
cin >>t;// input the no of test case
35+
for (int i=0;i<t;i++){
36+
int no;
37+
cin>>no; // input no of elements in array
38+
int x=no, count=1;
39+
int a[no];
40+
for (int j=0;j<no;j++){
41+
cin>>a[i];
42+
}
43+
44+
45+
while(x>0){
46+
int l;
47+
i=0;
48+
l=a[i];
49+
i=i+l;
50+
count++;
51+
x=x-l;
52+
}
53+
cout<<count<<"\n";
54+
}
55+
return 0;
56+
}
57+
58+
59+
60+
61+

0 commit comments

Comments
 (0)