Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerEarth Fredo and Game problem solution

YASH PAL, 31 July 2024
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

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
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes