HackerEarth's basic programming solutions( Split houses, e-maze-in, Bricks Game, Lift queries ) :

Problem 11: Split houses

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 n,x,f=0;
    string s,p;
    cin>>n>>s;
    p=s; //p=string same as string s
    for(int i=0;i<n;i++)
    {
        if(s[i]=='.')
        {
            s[i]='B'; //replacing all dots with a fence i.e B
        }
    }
    x=p.compare(s); //comparing the updated string s with the original string
    for(int j=0;j<n;j++)
        {
            if((s[j]=='H' && s[j+1]=='H')) //checking if two H's are together
            {
                f=-1;
                break;
            }
        }
    if((f==-1 || x==0 )&& n!=1) //see note below
    {
        cout<<"NO"<<endl;
    }
    else
    {
        cout<<"YES"<<endl<<s<<endl;
    }
}

We replaced all the dots in our string s with B because it's written in the question "if there are many answers, then you are required to print the one where most fences are placed".

string1.compare(string2) function compares two strings and if the two strings are identical it returns 0 else 1.

Initially f = 0 , if f becomes -1 , it means that in the updated string s two H are together (which we don't want)

n=1 means , there is only one character in the string.





Problem 12: e-maze-in

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 x=0,y=0;
    string s;
    cin>>s;
    for(int i=0;i<s.length();i++)
    {
        if(s[i]=='L')
        {
x--;
        }
        else if(s[i]=='R')
        {
            x++;
        }
        else if(s[i]=='U')
        {
         y++;
        }
        else
        {
            y--;
        }
    }
    cout<<x<<" "<<y;
}

Here the concept used is: In a two-dimensional Cartesian system , you can see if we move left by 1unit the value of x is decreased by 1 unit and there's no change in the value of y. similarly if we move right by 1 unit , x is increased by 1 unit and there's no change in the value of y. Similarly if we go up by 1 unit y increases by 1 and no change in the value of x and if down by 1 unit , y decreases by 1 value and no change in x value.





Problem 13: Bricks Game

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 n,i=1;
   cin>>n;
while(n!=0)
{
   n=n-i;
   if(n<1)
   {
      cout<<"Patlu";
      break;
   }
   n=n-2*i;
   if(n<1)
   {
      cout<<"Motu";
      break;
   }
   i++;
}
}

Here i did what is exactly said in the question.

Example: 13 bricks are there :

Patlu Motu

1         2

2         4

3         1 ( Only 1 remains)

Hence, Motu puts the last one.

Similarly in the above code , while loop runs till n becomes 0. We initialize i with 1 and it will get incremented by 1 every time in the end of the while loop using i++. Use the example to understand the above code by putting values. 





Problem 14: Lift queries

 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()
{
    long long int t,a=0,b=7,f; //a=ground floor i.e lift A initial position,
    cin>>t; //b=top floor i.e lift B initial position ,
    while(t--) //f=current floor,t=no. of test cases
    {
        cin>>f;
if(abs(f-a)<abs(f-b)) //comparing differences
     {
         cout<<"A"<<endl;
         a=f; //position of lift A is updated
     }
     else if(abs(f-a)>abs(f-b))
     {
cout<<"B"<<endl;
         b=f; //position of lift B is updated
     }
     else{ //the differences are equal
         if(a<b) //see note below
         {
         cout<<"A"<<endl;
         a=f; //position of lift A is updated
         }
         else{
             cout<<"B"<<endl;
             b=f; //position of lift B is updated
         }
     }
    }
}

When the differences are equal , it means that both the lift are equidastance from the given floor and in the question it is clearly mentioned "If both the lifts are at equidistant from the N th floor, them the lift from the lower floor comes up" , Therefore checking which lift is at lower position by comparing a and b.

 

 

 

Guys , if you have any queries related to a question or need more explanation, comment down below! 

Comments

  1. Very well written about hackerearth splithouses emazein bricksgame. If you want videos of bricks game, please download vidmate.Not only this, if you love watching TV shows or any reality show coming on TV is your favorite then you don't want to miss it but due to some problem you are not able to watch that show on TV, Vidmate help application from. You can easily watch TV on your mobile phone. . With its help, you can also watch reality shows. And if you want to watch comfortably then you can download that reality show in your phone which is directly saved in gallery. And you can enjoy watching offline anytime later. You can also download bricks game and Vidmate from 9apps

    ReplyDelete

Post a Comment

Popular posts from this blog

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

HackerEarth's basic programming solutions( Seating Arrangement, Zoos, Anagrams ) :

HackerEarth's basic programming solutions( Minimize Cost, Magical Word, Best Index ) :