-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvancingThroughArray.cpp
More file actions
54 lines (45 loc) · 1.62 KB
/
AdvancingThroughArray.cpp
File metadata and controls
54 lines (45 loc) · 1.62 KB
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
53
54
#include <iostream>
#include <vector>
using namespace std;
vector<int> clean_up(vector<int> temp)
{
int current = 1, next = 1;
for (int i = 1; i < temp.size(); ++i)
{
if (temp[current-1] != temp[i] ) { temp[current++] = temp[i];}
}
for (int i = current; i < temp.size(); ++i)
{
temp[i] = 0;
}
cout << "Minimum number of steps is " << current << endl;
return temp;
}
bool CanReachEnd(const vector<int>& max_advance_steps)
{
int furthest_reach_so_far = 0; // get first index
int last_index = max_advance_steps.size() -1; // get last index
vector<int> temp; temp.push_back(furthest_reach_so_far);
for (int i = 0; i <= furthest_reach_so_far && furthest_reach_so_far < last_index; i++) // iterate from first to last index
{
cout << max_advance_steps[i] + i << " furthest_reach_so_far " << furthest_reach_so_far << endl; // output current index
furthest_reach_so_far = max(furthest_reach_so_far, max_advance_steps[i] +i); //
//cout << "furthest_reach_so_far " << furthest_reach_so_far << endl;
temp.push_back(furthest_reach_so_far);
}
temp.push_back(furthest_reach_so_far);
//cout << "furthest_reach_so_far " << furthest_reach_so_far << endl; // print last index reached
for (int x:temp)
{ cout << temp[x] << " ";}
cout << endl;
vector<int> temp1 = clean_up(temp);
for (int x = 0; x < temp1.size(); x++)
{ cout << temp1[x] << " ";}
return furthest_reach_so_far >= last_index; // true if last index reahed is bigger or equal to last index in array
}
int main()
{
vector<int> mar = {3,3,1,0,2,0,1}; // input data
cout << CanReachEnd(mar) << endl; //
return 0;
}