// Given an integer list where each number represents the number of hops you can make,
// determine whether you can reach to the last index starting at index 0.
// For example, [2, 0, 1, 0] returns true while[1, 1, 0, 1] returns false.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool recursHop(vector<int> vec, unsigned index)
{
unsigned vecSize = vec.size();
cout << "Checcking Index: " << index << endl;
if (index == (vecSize - 1))
return true;
else if (index >= vecSize)
return false;
else if (vec[index] == 0)
return false;
for (unsigned i = 1; i <= vec[index]; ++i)
{
recursHop(vec, index + i);
}
}
int main()
{
vector<int> vec = vector<int>();
vec.push_back(1);
vec.push_back(1);
vec.push_back(1);
vec.push_back(0);
recursHop(vec, 0) ? cout << "Yes, you can hop through the vector" : cout << "No, you cannot hop through the vector" ;
return 0;
}
Comments