Skip to content
Programming101
Programming101

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
Programming101

Learn everything about programming

HackerEarth Aniruddhas Queue problem solution

YASH PAL, 31 July 2024
In this HackerEarth Aniruddha’s Queue problem solution Aniruddha is given a milestone M to reach in terms of distance. He is living in a different Galaxy where there are N days in a year. On an ith day, he can walk almost X distance. Assuming he walks optimally you need to output the minimum day number on which he will reach the milestone.
HackerEarth Aniruddha's Queue problem solution

HackerEarth Aniruddha’s Queue problem solution.

#include<iostream>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<functional>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<deque>
using namespace std;
long long arr[100010];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,noOfDays;
scanf("%d",&noOfDays);
for(i=0;i<noOfDays;i++)
{
scanf("%lld",&arr[i]);
}
unsigned long long sum,ans=0;
scanf("%llu",&sum);
bool flag=false;
for(i=0;i<noOfDays;i++)
{
ans+=arr[i];
if(ans>=sum)
{
flag=true;
break;
}
}
if(flag)
{
printf("%dn",i+1);
}
else
{
sum=sum%ans;
ans=0;
for(i=0;i<noOfDays;i++)
{
ans+=arr[i];
if(ans>=sum)
{
flag=true;
break;
}
}
if(sum!=0)
printf("%dn",i+1);
else
{
ans=0;
for(i=0;i<noOfDays;i++)
{
if(arr[i]>0)
{
ans=i;
}
}
printf("%dn",ans+1);
}
//cout<<i+1<<endl;
}
}
return(0);
}

Second solution

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
ll ar[n],cnt=0;
for(int i=0;i<n;++i)
{
scanf("%lld",&ar[i]);
cnt+=ar[i];
}
ll m;
scanf("%lld",&m);
m%=cnt;
int ans;
if(m==0)
{
for(int i=n-1;i>=0;--i)
{
if(ar[i]!=0)
{
ans=i;
break;
}
}
}
else
{
for(int i=0;i<n;++i)
{
m-=ar[i];
if(m<=0)
{
ans=i;
break;
}
}
}
printf("%dn",ans+1);
}
return 0;
}
coding problems

Post navigation

Previous post
Next post
  • 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
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes