forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing_jump.cpp
More file actions
43 lines (41 loc) · 750 Bytes
/
missing_jump.cpp
File metadata and controls
43 lines (41 loc) · 750 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<bits/stdc++.h>
using namespace std;
class Solution{
public:
int minJumps(int a[], int n){
int pos = 0;
int jump = 0;
int destination = 0;
for (int i = 0; i < n-1; i++)
{
destination = max(destination, a[i] + i);
if (pos == i)
{
jump++;
if(destination<=i)
{
return -1;
}
pos = destination;
}
}
return jump;
}
};
int main()
{
int t;
cin>>t;
while(t--)
{
int n,i,j;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
cin>>arr[i];
Solution obj;
cout<<obj.minJumps(arr, n)<<endl;
}
return 0;
}
// } Driver Code Ends