-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum_jumps.cpp
52 lines (41 loc) · 1012 Bytes
/
Minimum_jumps.cpp
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
44
45
46
47
48
49
50
51
52
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// Function to return minimum number of jumps to end of array
class Solution {
public:
int minJumps(vector<int>& arr) {
int jump = 0;
int pos = 0;
int des = 0;
for(int i = 0; i < arr.size()-1; i++)
{
des = max(des, arr[i] + i);
if(pos == i)
pos = des, jump++;
}
return pos >= arr.size()-1 ? jump : -1;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
int n, i, j;
vector<int> arr;
string ip;
int number;
getline(cin, ip);
stringstream ss(ip);
while (ss >> number) {
arr.push_back(number);
}
Solution obj;
cout << obj.minJumps(arr) << endl;
}
return 0;
}
// } Driver Code Ends