Hackerrank's Problem Solving solutions( Utopian Tree, Angry Professor, Beautiful Days at the Movies ) :
Problem 28: Utopian Tree
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;        //t=no. of test cases
    cin>>t;
    while(t--)
    {
        int n,h=1,i;   //h=height
        cin>>n;
        if(n==0)
        {
            cout<<"1"<<endl;
        }
        else
        {
          for(i=1;i<=n;i++)
          {
            if(i%2==0)
            {
               h=h+1;
            }
             else
            {
               h=h*2;
            }
          }
          cout<<h<<endl;
        }
    }
}
This code is simple. There's no need for any explanation.
Problem 29: Angry Professor
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,k,ct=0,e;  //ct=count , e=arrival time
        cin>>n>>k;
        while(n--)
        {
            cin>>e;
            if(e<=0)
            {
                ct++;
            }
        }
        if(ct>=k)
        {
            cout<<"NO"<<endl;
        }
        else
        {
            cout<<"YES"<<endl;
        }
    }
}
This code is simple. There's no need for any explanation.
Problem 30: Beautiful Days at the Movies
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
#include<bits/stdc++.h>
using namespace std;
long long rev(long long n)   //function for finding the reverse of a number
{
    long long reverse=0,r;
    while(n!=0)
    {
       r=n%10;
       reverse=reverse*10 + r;
       n=n/10;
    }
    return reverse;
}
int main()
{
   long long i,j,k,p,ct=0;
   cin>>i>>j>>k;
   for(p=i;p<=j;p++)
   {
     if((abs(p-rev(p))%k==0))   //doing what is asked in the question
     {
        ct++;
     }
   }
   cout<<ct;
}
Comments
Post a Comment