Hackerearth – Jarvis and Numbers

code i have understand for last 6 hours:
problem statement was little bit clumsy but understood atlast:

#include<bits/stdc++.h>
using namespace std;
int gcd(int n1,int n2){
    if(n2!=0)
        return gcd(n2,n1%n2);
    else
        return n1;
}
int main()
{
    int t,i,n,temp,commonfactor,sum,ans;
    cin>>t;
    while(t--)
    {
        sum=0;
        int n;
        cin>>n;
        printf("Main temp %d\n",n);
        for(i=2; i<n; i++)
        {
            int temp=n;
            ans=0;
            while(temp>0)
            {
                ans=ans+temp%i;
                temp=temp/i;
                printf("i=%d temp=%d ",i,temp);
                cout<<"ans="<<ans<<endl;
            }
            sum=sum+ans;
            cout<<"\nSum="<<sum<<endl;
        }
        //cout<<"Sum="<<sum<<endl;
        commonfactor=gcd(sum,n-2);
        cout<<"Common factor :"<<commonfactor<<" ";
        cout<<"N-2 :"<<n-2;
        ans=(n-2)/commonfactor;
        printf("\n%d\n",ans);
    }
    return 0;
}

accepted code took help from editorial:

#include<bits/stdc++.h>
using namespace std;
int gcd(int n1,int n2){
    if(n2!=0)
        return gcd(n2,n1%n2);
    else
        return n1;
}
int main()
{
    int t,i,n,temp,commonfactor,sum,ans;
    cin>>t;
    while(t--)
    {
        sum=0;
        int n;
        cin>>n;
        //printf("Main temp %d\n",n);
        for(i=2; i<n; i++)
        {
            int temp=n;
            ans=0;
            while(temp>0)
            {
                ans=ans+temp%i;
                temp=temp/i;
              //  printf("i=%d temp=%d ",i,temp);
                //cout<<"ans="<<ans<<endl;
            }
            sum=sum+ans;
           // cout<<"\nSum="<<sum<<endl;
        }
        //cout<<"Sum="<<sum<<endl;
        commonfactor=gcd(sum,n-2);
       // cout<<"Common factor :"<<commonfactor<<" ";
       // cout<<"N-2 :"<<n-2;
        ans=(n-2)/commonfactor;
        printf("%d\n",ans);
    }
    return 0;
}

official editorial:

#include<stdio.h>

int gcd(int n1, int n2)
{
    if (n2 != 0)
       return gcd(n2, n1%n2);
    else
       return n1;
}

int main(){
    int t,i,n,temp,c,sum,common_factor,ans;
    scanf("%d",&t);             // Take input from user
    while(t--){                 // loop it for t test-cases
        sum = 0;                // Sum variable as average = sum/(n-2)
        scanf("%d",&n);         // Take n as input
        for(i=2;i<=n-1;i++){    // loop for changing the number n to all the bases from 2 to (n-1)
            temp = n;           // storing at temporary variable
            c=0;                // to find the sum of all the numbers formed on conversions and there sum so c = 0
            while(temp > 0){
                c+=temp%i;
                temp=temp/i;
            }
            sum+=c;             // Adding sum of each conversion to the total
        }
        common_factor = gcd(sum,n-2);       // Common factor in sum and n-2 to find the irreducible form of average
        ans = (n-2)/common_factor;          // denominator of average
        printf("%d\n",ans);
    }
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 *