In this

**HackerEarth Fredo and Game problem solution**Fredo is playing a game. The rules of the game are:Initially, you are given A units of ammo. There are N obstacles placed on a path. When you hit an obstacle, you gain three units of ammo and lose one unit of ammo. When you don't hit an obstacle, you lose one unit of ammo. If at any instance, you are left with 0 ammo units, the game ends there.

Fredo has an array Arr containing N elements corresponding to the N obstacles. If Fredo will hit obstacle i , then Arr[i] = 1 else Arr[i] = 0.

Fredo asks you to tell him if he will be able to reach the end of the path. If yes, then also tell him the remaining number of ammo units.

If he is not able to reach the end of the path, tell him the obstacle index at which his game would end.

## HackerEarth Fredo and Game problem solution.

```
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,a,ind,rem;
cin>>a>>n;
rem=a;
//cout<<a<<" "<<n<<"\n";
ind=n;
for(int i=0;i<n;i++)
{
int temp;
cin>>temp;
if(ind==n)
{
if(temp)rem+=2;
else rem--;
if(!rem)ind=i+1;
}
}
if(ind==n)cout<<"Yes "<<rem<<"\n";
else cout<<"No "<<ind<<"\n";
}
return 0;
}
```

### Second solution

```
t = int(raw_input())
for __ in xrange(t):
a,n = map(int, raw_input().split())
arr = map(int, raw_input().split())
cur = 0
while cur < n and a > 0:
if arr[cur] == 0:
a -= 1
cur += 1
else:
a += 2
cur += 1
if cur == n:
print "Yes", a
else:
print "No", cur
```

## 0 Comments