Monday, 11 November 2019

Mark and Toys,Priyanka and Toys, Jim and the Orders Hacker Rank Solution in C++

Mark and Toys
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    int a[n],i,c=0,bill=0;
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        bill=bill+a[i];
        if(bill>k)
            break;
        else
            c++;
    }
    printf("%d",c);
}

Priyanka and Toys
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    int a[n],i;
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    int minwt=a[0];
    int noc=1;
    for(i=0;i<n;i++)
    {
        if(a[i]<=4+minwt)
            continue;
        noc++;
        minwt=a[i];
    }
    printf("%d",noc);
    return 0;
}




Jim and the Orders
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    int i;
    vector<pair<int,int>> v;
    int ono, ptime;
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&ono,&ptime);
        v.push_back(make_pair(ono+ptime,i+1));
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
        printf("%d ",v[i].second);

}

Monday, 14 October 2019

Given 2 integers a,k. Find if kth bit is set or not using bitwise right shift operator

//Given 2 integers a,k. Find if kth bit is set or not using bitwise right shift operator
#include<stdio.h>

int main() {
    int a,k,t;
    scanf("%d%d",&a,&k);
    t=a>>(k-1);
    if(t&1 == 0)
        printf("%d th bit is not set",k);
    else
        printf("%dth bit is set",k);
}

Check whether 3 numbers are equal or not using bitwise operators

//Given 3 integers a,b,c. Find if all the 3 numbers are same or not using bitwise operators
#include<stdio.h>

int main() {
    int a,b,c,t;
    scanf("%d%d%d",&a,&b,&c);
    t=(a^b)|(b^c);
    if(t==0)
        printf("same");
    else
        printf("not same");
}

You are given two integers a and b. You need to find number of digits in element a (let us call it n) and print out maximum number obtained by doing xor of 10^n and k, where k lies from 1 to b

//You are given two integers a and b. You need to find number of digits in element a (let us call it n) and print out maximum number obtained by doing xor of 10^n and k, where k lies from 1 to b
#include<stdio.h>
#include<math.h>
int main() {
    int a,b,n=0,k,max=0,temp,t,i,c=0;
    scanf("%d%d",&a,&b);
    //n=log(a)+1;
    t=a;
    while(t)
    {
        n++;
        t=t/10;
    }
    //printf("number of digits=%d\n",n);
    t=1;
    for(i=1;i<=n;i++)
        t=t*10;
    //printf("10 to power of n=%d\n",t);
    for(k=1;k<=b;k++)
    {
       
        temp=t^k;
        if(max<temp)
            max=temp;
    }
    printf("%d",max);
}

Monday, 23 September 2019

Little Boy is Uttering His First Words and Snake Procession Code chef solution in C

Little Boy is uttering his first words. If there is sub sequence mom in the words he uttered, print Mom and if it contains sub sequence dad print Dad and if it has both print the one that whose sub sequence has occurred first. If it has no words at all, print Goo-goo

Program:
#include<stdio.h>

int main() {
    char st[100],s1[]="mom",s2[]="dad";
    scanf("%s",st);
    int i,j=0,k=0;
 
    for(i=0;st[i]!='\0';i++)
    {
        if(st[i] == s1[j])
        {
            j++;
            if(j==3)
                break;
        }
        if(st[i] == s1[k])
        {
            k++;
            if(k==3)
                break;
        }
    }
    if(j==3)
        printf("Mom");
    else if(k==3)
        printf("Dad");
    else
        printf("Goo-goo");
 
        return 0;

}

here in order to check whether a sub sequence is present or not in a given string, the best way to check is given in the above program.
Also once we found the sub sequence mom first we need to break because we need to print the first sub sequence that was uttered first.

=======================================================================

Snake Procession

Problem Code: SNAKPROC

Solution:


#include <stdio.h>
#include <string.h>
int main(void) {
int t;
scanf("%d",&t);
while(t--)
{
    int n,i,j=0,f=0,k;
    scanf("%d",&n);
    char st[n+1],temp[n+1];
    scanf("%s",st);
    int l =strlen(st);
    for(i=0;i<l;i++)
        if(st[i] !='.')
            temp[j++]=st[i];
    if(j == 0)
        f=1;
    else if (j%2 == 0)
        for(k=0;k+1<j;k=k+2)
            if(temp[k] == 'H' && temp[k+1] == 'T')
                f=1;
            else
            {
                f=0;
                break;
            }
   if(f == 1)
    printf("Valid\n");
   else
    printf("Invalid\n");
 
}
return 0;
}

in the above program never write  st[k+1] !='\0' and always use string length and compare whether index+1 exceeds string length or not.
Here as the index, k is not incremented by 1 but by 2 and hence we are not supposed to use
st[k+1]!='\0'

Saturday, 31 August 2019

I Love T code chef solution in C

Read sentences until user enters 0
for each sentence, if the first character in each word in the given sentence is vowel, append T and if it is not vowel replace that character by T. 
Also only first character in each word only must be capital letter.

Sample Input:
love apples
apples are gOOOd for HelatHH
0

Sample Output:
Tove Tapples
Tapples Tare Toood Tor Telathh



#include<stdio.h>
int isvowel(char ch)
{
    if(ch >='A' && ch <= 'Z')
        ch=ch+32;
    if(ch == 'a' ||ch == 'e'||ch == 'i'||ch == 'o'||ch == 'u')
        return 1;
    else
        return 0;
}

int main()
{
    char st[1000001];
    scanf("\n%[^\n]\n",st);
    while(st[0] != '0')
    {
        int i;
        for(i=0;st[i]!='\0';i++)
        {
        if(st[i] >= 'A' && st[i] <= 'Z')
                st[i]=st[i]+32;
        if(i == 0|| (st[i-1] == ' ' && st[i] != ' '))
        {
         
            if(isvowel(st[i]))
                printf("T%c",st[i]);
            else
                printf("T");
        }
        else
            printf("%c",st[i]);
        }
        printf("\n");
        scanf("\n%[^\n]\n",st);
    }
    return 0;
 
}