Hackerearth: Complete the Syllabus

Copied the code from someone’s solutions:

#include <stdio.h>
int main()
{
    int t;
    long int n,i,j,k,a[7],sum;
    char *s[7]= {"MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY",
                 "SUNDAY"};
    scanf("%d",&t);
    for (j=0; j<t; j++)
    {
///////////////////////////
        sum=0;
        scanf("%ld",&n);
        for(i=0; i<7; i++)
        {
            scanf("%ld",&a[i]);
            sum+=a[i];
        }
        k=n%sum;
        i=0;
        if(k==0)
            for(i=6; i>=0; i--)
            {
                if(a[i]!=0)
                {
                    printf("%s\n",s[i]);
                    break;
                }
            }
        else
        {
            while(k>0)
            {
                if (i==7)
                    i=0;
                k=k-a[i++];
            }
            printf("%s\n",s[i-1]);
        }
    }
    return 0;
}

code from editorial:

#include<bits/stdc++.h>
 using namespace std;
 typedef long long ll;
 int main()
 {
 ios::sync_with_stdio(false);
 vector<string> vecc(7);
 vecc[0]="MONDAY";vecc[1]="TUESDAY";
 vecc[2]="WEDNESDAY";vecc[3]="THURSDAY";
 vecc[4]="FRIDAY";vecc[5]="SATURDAY";
 vecc[6]="SUNDAY";
        ll t;   
        cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        ll s=0,arr[7],copyn=n;
        for(ll i=0;i<7;i++)
        {
            cin>>arr[i];
            s+=arr[i];
        }
        ll op=0;
        if(s!=1)        //Optimization
            op=n/s;
        n=n-op*s;
        if(n==0)
        {   
            n=copyn;
        }
        ll i=0;
        while(n>0)
        {
            i%=7;
            n-=arr[i];
            i++;
        }
        cout<<vecc[i-1]<<endl;
    }
    return 0; 
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *