Monday, 31 August 2020

War Hacker Earth Solution in C

 Now, the fight begins. But, how does one come to know who wins? Simple way actually. Every soldier in either army starts going on a rampage, and starts killing every soldier of the opposite army, which has less strength. The army which destroys the other army wins the war, and wins the game between two newbies. If such a case is not possible, the result will thus be a TIE!


https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/warcakewalk/description/


#include<stdio.h>
int main()
{
int t;
scanf("%d", &t);
while(t--)
    {
long long int n;
scanf("%lld", &n);
long long int temp, mb=0, ma=0;
for(long long int i=0; i<n; i++)
        {
scanf("%lld", &temp);
            if(mb < temp)
    mb = temp;
}
for(long long int i=0; i<n; i++)
        {
scanf("%lld", &temp);
            if(ma < temp)
    ma = temp;
}

if(mb == ma)
printf("Tie\n");
else if(mb > ma)
printf("Bob\n");
else
printf("Alice\n");
}
return 0;
}

Bob and Bombs Hacker Earth Solution in C

 


https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/bob-and-bombs-cake-walk/submissions/

#include<stdio.h>
int main ()
{
int tc,i,j,k,wd,len,x,y;
scanf("%d",&tc);
while(tc--)
{
wd=0;
char str[100000];
scanf("%s",&str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]=='W'&& i+1 < len &&str[i+1]=='B')
{
wd++;
str[i]='#';
}
else if(str[i]=='W'&& i>=1 && str[i-1]=='B')
{
wd++;
str[i]='#';
}
else if(str[i]=='W'&& i+2 < len &&str[i+2]=='B')
{
wd++;
str[i]='#';
}
else if(str[i]=='W'&& i>=2 && str[i-2]=='B')
{
wd++;
str[i]='#';
}
}
printf("%d\n",wd);
}
return 0;
}

Final Destination Hacker Earth Solution in C

 https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/final-destination-cakewalk/submissions/


#include<stdio.h>
#include<string.h>
int main()
{
char str[1000000];
scanf(" %s", str);
    int i,x=0,y=0;
    for(i=0;str[i]!='\0';i++)
{
if(str[i] == 'L')
x=x-1;
if(str[i] == 'D')
y=y-1;
if(str[i] == 'R')
x=x+1;
if(str[i] == 'U')
y=y+1;
}
printf("%d %d",x,y);
}

Shreya and Non-Palindrome Hacker Earth Solution in C

 https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/shreya-and-non-palindrome/submissions/


#include<stdio.h>
#include<string.h>
int main()
{
    char str[1000000];
    scanf(" %s", str);  
    int l = strlen(str) - 1, j, flag = 0,i;
    for( i= l; i > 0; i--)
    {
        if(str[i] != str[0])
        {
            flag = 1;
            break;
        }
    }
    if(flag == 0)
        printf("0");
    else
    {
            for(j = 0; j <= i/2; j++)
                if(str[j] != str[i-j])
                {
                    printf("%d", i+1);
                        return 0;
                }
    }
}

Case conversion Hacker Earth Solution in C

 https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/case-conversion-d19fbcfe/submissions/

#include<stdio.h>
#include<string.h>
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
char s[105],temp[200];
scanf(" %s", s);
strcpy(temp,s);
int i,j;
if(s[0] >= 'A' && s[0] <= 'Z')
temp[0]=s[0]+32;
for(i=1,j=1;s[i] != '\0';i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')
{
temp[j++]='_';
temp[j++]=s[i]+32;
}
else
temp[j++]=s[i];
}
temp[j]='\0';
printf("%s\n", temp);
}
}

String Game Hacker Earth Solution in C

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/alice-and-string-game-dbd6adc3/submissions/

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char s[100001];
        int i,h[26]={0},c=0;
        scanf("%s",s);
        for(i=0;s[i] != '\0'; i++)
            h[s[i]-'a']++;
        for(i=0;i<26;i++)
        {
            if(h[i]!=0)
                c++;
        }
        if(c%2!=0)
            printf("Player1\n");
        else
            printf("Player2\n");
    }
}

String Sum Hacker Earth Solution in C

 

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/string-sum/?layout=old

#include<stdio.h>

int main()
{
    char str[1010];
    scanf(" %s",str);
    int i;
    unsigned long long int ans=0;
    for(i=0;str[i] != '\0';i++)
        ans=ans+str[i]-'a'+1;
    printf("%llu",ans);
}

Sunday, 30 August 2020

Min Max Hacker Earth solution in C

 https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/min-max-3/submissions/


#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int a[n],i,max,min,h[1001]={0},flag=0;
    scanf("%d",&a[0]);
    max=min=a[0];
    h[a[0]]++;
    for(i=1;i<n;i++)
    {
        scanf("%d",&a[i]);
        h[a[i]]++;
        if(max < a[i])
            max=a[i];
        if(min > a[i])
            min=a[i];
    }
for(i=min;i<=max;i++)
        if(h[i] == 0)
        {
            flag=1;
            break;
        }
    if(flag == 1)
        printf("NO");
    else
        printf("YES");
}

Find the Pattern Hacker Earth Solution in C

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/find-the-pattern-6/submissions/

 #include<stdio.h>

int main()

{
unsigned long long int n;
scanf("%llu",&n);
    unsigned long long int temp;
    scanf("%llu",&temp);
    unsigned long long int max=temp, min= temp;
    while(n--)
    {
        
        scanf("%llu",&temp);
        if(max < temp)
            max=temp;
        if(min>temp)
            min=temp;
    }
    printf("%llu",max+min);
}

Saturday, 29 August 2020

Rain sound Hacker Earth Solution in C

 #include<stdio.h>

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int l,r,s;
        scanf("%d%d%d",&l,&r,&s);
        int min,max;
        min=l/s;
        if(min*s < l)
            min++;

        max=r/s;
        if(min<=max)
            printf("%d %d\n",min,max);
        else
            printf("-1 -1\n");
    }
}

Print hackerearth HackerEarth Solution in C

 #include <stdio.h>


int main()
{
    int n,min;
    scanf("%d", &n);
char str[1000007];
    int h[26]={0};
scanf("%s", str);
int c0=0,c1=0,flag=0;
for(int i=0;str[i] != '\0';i++)
        h[str[i]-'a']++;
    int nc=h['c'-'a'];
    int nk=h['k'-'a'];
    int nt=h['t'-'a'];
    if(nc<nk && nc<nt)
        min=nc;
    else if(nk<nt)
        min=nk;
    else
        min=nt;
    if(h['h'-'a']/2 < min )
        min=h['h'-'a']/2;
    if(h['e'-'a']/2 < min )
        min=h['e'-'a']/2;
    if(h['r'-'a']/2 < min )
        min=h['r'-'a']/2;
    if(h['a'-'a']/2 < min )
        min=h['a'-'a']/2;
    
    printf("%d",min);
}

The Dice Hacker Earth Problem Solution in C - good example for usage of continue in C

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/the-dice-d4dc5b11/submissions/


 #include <stdio.h>

int main(){
    char str[1000000];
    scanf("%s", str);
    int c=0,i,flag=0;
    for( i=0;str[i]!='\0';i++)
    {
        if(str[i] > '6')
        {
            flag=1;
            break;
        }
        else if(str[i] == '6')
            continue;
        c++;
    }
    if(flag == 1 || str[i-1] == '6')
        printf("-1");     
    else
        printf("%d", c);
}


One String No Trouble Hacker Earth Solution in c,Little Jhool and psychic powers Hacker Earth Solution in c

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/one-string-no-trouble-37037871/submissions/

#include <stdio.h>
#include <string.h>
int main(){
    char str[1000000];
    scanf("%s", str);
    int l=strlen(str),c=1,ans=0;
    for(int i=0;i+1 < l;i++)
    {
        if(str[i] != str[i+1])
            c=c+1;
        else
        {
            if(ans < c)
                ans=c;
            c=1;
        }
    }
    if(ans < c)
        printf("%d", c);
    else    
        printf("%d",ans);
}
===============================
Little Jhool and psychic powers

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/psychic-powers/submissions/


#include <stdio.h>
#include <string.h>
int main()
{
char str[1000000];
scanf("%s", str);
    int c0=0,c1=0,flag=0;
for(int i=0;str[i] != '\0';i++)
    {
        if(str[i] == '1')
        {
            c1++;
            if(c1 >= 6)
            {
                flag=1;
                break;
            }
            c0=0;
        }
        else
        {
            c0++;
            if(c0 >= 6)
            {
                flag=1;
                break;
            }
            c1=0;
        }
    }
    if(flag == 0)
        printf("Good luck!");
    else
        printf("Sorry, sorry!");
}

Friday, 28 August 2020

Flatland Space Stations Hacker Rank Solution in C- using merge sort

 https://www.hackerrank.com/challenges/flatland-space-stations/problem


#include <stdio.h>
#define MAX 10000

int c[MAX];

void merge(int a[], int ,int ,int , int );
void mergesort(int a[], int ,int);


void mergesort(int a[],int low, int high)
{
                int mid;
                if(low<high)
                {
                                mid=(low+high)/2;
                                mergesort(a,low,mid);
                                mergesort(a, mid+1,high);
                                merge(a,low,mid,mid+1,high);
                }
}

/* merge two subparts obtained by dividing the array into 2 halves (low1 to high1 and low2 to high2)*/
void merge(int a[], int low1,int high1,int low2, int high2)
{
                int i,j,k;
                i=low1;
                j=low2;
                k=low1;    /*after merging, lower index of merged array begins with low1,ends with high2*/
                while(i<= high1 && j<=high2)
                {
                                while(a[i]<=a[j] && i <=high1)
                                {
                                                c[k++]=a[i++];
                                }
                                while(a[j]<a[i]&& j <= high2)
                                {
                                                c[k++]=a[j++];
                                }
                }
                while(i<=high1)    /* if i <= high1 means there are elements still in subpart1 (low1 to high1)*/
                {
                                c[k++]=a[i++];  
                }
                while(j<=high2)
                {
                                c[k++]=a[j++];                  
                }
                for(k=low1;k<=high2;k++)  /* the sorted sub array in 'c' is written back to original array 'a'*/
                {
                                a[k]=c[k];
                }             
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int a[m];
    for(int i=0;i<m;i++)
        scanf("%d",&a[i]);
    mergesort(a,0,m-1);
    int max=a[0];
    for(int i=1;i<m;i++)
    {
        if(max<(a[i]-a[i-1])/2)
            max=(a[i]-a[i-1])/2;
    }
    if(n-1-a[m-1] > max)
        max=n-1-a[m-1];
    printf("%d",max);
}

Manasa and Stones Hacker Rank Solution in C

 https://www.hackerrank.com/challenges/manasa-and-stones/

Below is the explanation given by user Connor Rowe in hacker rank

The key to solving this quickly is to realize that any permutation of moves containing the same amount of "a" and "b" will have the same end result. From this, we know that n stones will have n possible distinct results.

For example, if n is 4, then we have possible results:

0 + a + a + a

0 + a + a + b

0 + a + b + b

0 + b + b + b


We can use this pattern to reduce the problem to a simple O(n) loop, and we must handle keeping elements ordered and avoiding duplicates.


#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,a,b,sum,h[1000000]={0};
        scanf("%d%d%d",&n,&a,&b);
        for(int i=0;i<n;i++)
        {
            sum= i*a+(n-i-1)*b;
            h[sum]++;
        }
        for(int i=0;i < 1000000;i++)
            if(h[i] != 0)
                printf("%d ",i);
        printf("\n");
    }
}

Modified Kaprekar Numbers Hacker Rank Solution in C

 in modified kaprekar number, say x

let number of digits in x, say nd

let square = x*x

then 

x = square/10^nd + square%(10^nd) then x would be a modified kaprekar number


https://www.hackerrank.com/challenges/kaprekar-numbers/problem


#include<stdio.h>
#include<math.h>
int main() 
{
    long int n,m,r,f;
    int flag = 0;
    scanf("%ld%ld",&n,&m);
    for(long int i=n;i<=m;i++)
    {
        long int sum;
        long int a = log10(i)+1;
        
       long int p = i * i;
        long int g = pow(10,a);
        sum = p%g + p/g;
        if(sum == i)
        {
            printf("%ld ",sum);
            flag++;
        }
    }
    if(flag == 0)
        printf("INVALID RANGE");       
    return 0;
}


ACM ICPC Team Hacker Rank Solution in C

 https://www.hackerrank.com/challenges/acm-icpc-team/problem

int main(){
    int n, m; 
    scanf("%d %d",&n,&m);
    char t[n][m];
    for(int i = 0; i < n; i++)
       scanf("%s",t[i]);
    int k1, max = 0, ans = 0;
    for(int i = 0; i < n-1; i++) 
    {
        for(int j = i+1; j < n; j++) 
        {
            k1 = 0;
            for(int k = 0; k < m; k++) 
                if(t[i][k] == '1' || t[j][k] == '1')
                    k1++;
            if(max < k1)
            {
                max = k1;
                ans = 1;
            }
            else if(max == k1)
                ans++;
        }
    }
    printf("%d\n%d\n", max, ans);
    return 0;
}

Halloween Sale Hacker Rank Solution in C- infinte loop and usage of break

https://www.hackerrank.com/challenges/halloween-sale/problem 

#include <stdio.h>

int main (void)
{
    int p, d, m, s;
    int n, t;
    scanf("%d%d%d%d", &p, &d, &m, &s);
    int c = 0;
    while(1)
    {
        int price;
        if(p>m)
            price=p;
        else
            price=m;
        s = s-price;
        p = p-d;
        if(s>=0)
            c++;
        else
            break;
    }
    if(c < 0)
        printf("0");
    else
        printf("%d",c);
}

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);
    }
}