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