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("%d\n",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("%d\n",i+1);
      else
      {
        ans=0;
        for(i=0;i<noOfDays;i++)
        {
          if(arr[i]>0)
          {
            ans=i;
          }
        } 
        printf("%d\n",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("%d\n",ans+1);
  }
  return 0;
}