Friday, 28 August 2020

Minimum Distances Hacker Rank Solution in C, C++

https://www.hackerrank.com/challenges/minimum-distances/problem

#include<stdio.h>
#include<limits.h>
int main()
{
    int n,ans=INT_MAX;
    int m[100008]={0};
    scanf("%d",&n);
    int temp;
    for(int i=1;i<=n;i++) // i has to start with 1 otherwise m[temp]=i will update
                            //m[temp] to zero and if(m[temp] == 0) will become true
                            // example:2 1 1                                    
    {
        scanf("%d",&temp);
        if( m[temp] == 0)
            m[temp]=i;
        else 
        {
            int previndex = m[temp];
            if(ans> i-previndex)
                ans=i-previndex;
        }
    } 
    if(ans == INT_MAX)
        printf("-1");
    else
        printf("%d",ans);
}   

=============================in C++==========================

#include <bits/stdc++.h>

using namespace std;
int main()
{
    int n,ans=INT_MAX;
    map<int,int>m;
    scanf("%d",&n);
    int a[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if( m.find(a[i]) == m.end())
            m[a[i]]=i;
        else 
        {
            int previndex = m[a[i]];
             ans=min(ans,abs(i-previndex));
        }
    } 
    if(ans == INT_MAX)
        printf("-1");
    else
        printf("%d",ans);
}   

Chocolate Feast Hacker Rank Solution in C

 https://www.hackerrank.com/challenges/chocolate-feast/problem


#include <stdio.h>

int main(){
    int t,noc,now; 
    scanf("%d",&t);
    for(int i = 0; i < t; i++){
        int n,c,m; 
        scanf("%d %d %d",&n,&c,&m);
        noc = n/c;
        now = noc;
        while(now>=m)
            {
            noc=noc+now/m;
            now=now/m+now%m;
        }
        printf("%d\n",noc);
    }
    return 0;
}

Lisa's Workbook Hacker Rank Solution in C

https://www.hackerrank.com/challenges/lisa-workbook/copy-from/176618880 

#include <stdio.h>

int main() 
{
    int n,k; 
    scanf("%d%d", &n,&k);
    int a[n],i,np=0,ans=0,sprn,eprn;
    for(i = 0; i < n; i++)
    {
        scanf("%d",&a[i]);
        sprn=1,eprn=0;
        while(a[i]>0)
        {
            np=np+1;
            if(a[i] > k)
                eprn = sprn+k-1;                     
            else
                eprn=sprn+a[i]-1;    
            if(np>=sprn && np<=eprn)
                ans++;
            a[i]=a[i]-k;
            sprn=eprn+1;
       }
    }
    printf("%d",ans);
}

Thursday, 27 August 2020

Repeated String Hacker Rank solution in C

 https://www.hackerrank.com/challenges/repeated-string/problem

#include<stdio.h>

int main()
{
    char str[120];
    scanf("%s",str);
    unsigned long long int len,c=0,rep,i,rem;
    scanf("%llu",&len);
    for(i=0;str[i]!='\0';i++)
        if(str[i]== 'a')
            c++;
    //number of times string can be repeated 
    rep=len/i;
    rem=len%i;
    c=c*rep;
    for(i=0;i<rem;i++)
        if(str[i] == 'a')
            c++;
    printf("%llu",c);
}

Cut the sticks Hacker Rank solution in C- perfect example where you can use goto

 

https://www.hackerrank.com/challenges/cut-the-sticks/problem

#include<stdio.h>
#include<limits.h>
int main()
{
    int n,min=INT_MAX;
    scanf("%d",&n);
    int a[n],ans=0;
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    
    //finding minimum in array
    rep:
    for(int i=0;i<n;i++)
        if(a[i] > 0 && min>a[i])
            min=a[i];
    //subtracting the minimum and counting number of cuts
    int c=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]>=min)
            c++;
        a[i]=a[i]-min;
    }
     if(c == 0)
        goto print;
    printf("%d\n",c);
    // again we have to find min
    min=INT_MAX;
    goto rep;

    print:
        return 0;
}

Find Digits Hacker Rank solution in C

 https://www.hackerrank.com/challenges/find-digits/problem


#include <stdio.h>
int main()
{
    unsigned long long int t;
    scanf("%llu",&t);
    while(t--)
    {
        unsigned long long int n,c=0,temp;
        scanf("%llu",&n);
        temp=n;
        while(n>0)
        {
            int digit=(n%10);
            if(digit > 0)
                if(temp%digit == 0)
                    c++;
            n=n/10;
        }
        printf("%llu\n",c);
    }
}