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

Problem 1: Seating Arrangement

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 t,s,p; //t=no. of test cases
cin>>t; //s=seat no., p=corresponding seat no.
while(t--)
{
cin>>s;
     p=s%12; //when to use modulo see below
if(p==1)
     {
         cout<<s+11<<" WS"<<endl;
     }
     else if(p==2)
     {
         cout<<s+9<<" MS"<<endl;
     }
else if(p==3)
     {
         cout<<s+7<<" AS"<<endl;
     }
     else if(p==4)
     {
         cout<<s+5<<" AS"<<endl;
     }
     else if(p==5)
     {
         cout<<s+3<<" MS"<<endl;
     }
     else if(p==6)
     {
         cout<<s+1<<" WS"<<endl;
     }
     else if(p==7)
     {
         cout<<s-1<<" WS"<<endl;
     }
     else if(p==8)
     {
         cout<<s-3<<" MS"<<endl;
     }
     else if(p==9)
     {
         cout<<s-5<<" AS"<<endl;
     }
     else if(p==10)
     {
         cout<<s-7<<" AS"<<endl;
     }
     else if(p==11)
     {
         cout<<s-9<<" MS"<<endl;
     }
     else{
         cout<<s-11<<" WS"<<endl;
     }
} //code explaination below
}
 
Using modulo(%):

let's assume L = a larger number and S = a smaller number

therefore - L%S = remainder when L is divided by S

                   and  S%L = S

The beauty of this modulo operator is seen when there comes a period. For example the value of angles are repeated after 360 degree. Now let a larger angle which is greater than 360 be A . If we want the corresponding angle of A between 0 to 360 degree than we can use modulo operator like this ( let the corresponding angle of A is a) :

                                   a = A%360

For Example -  A = 4781  , therefore a = 4781%360 = 101 

Now getting to the question , the cycle repeats after 12th seat . Therefore the period is 12. Now if we use modulo for any seat we will get the seat within 1 to 12 . For example - 69%12 = 9 and we know the 9th seat is Aisle Seat(AS) and the seat in front of it is 5 less than 9 i.e 4. Therefore we can conclude that the 69th seat is Aisle Seat(AS) and the seat in front of it will be 69 - 5 = 64. 

In the above code i used else if ladder  and solved the problem.                                    

 




Problem 2: Zoos

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 nz=0,no=0; //nz = no. of Zs , no = no. of Os
    string s;
    cin>>s;
    for(int i=0;i<s.length();i++)
    {
        if(s[i]=='z')
        {
nz++;
        }
        else
        {
            no++;
        }
    }
    if(2*nz==no)
    cout<<"Yes"<<endl;
    else
    cout<<"No"<<endl;
}

This code is simple. There's no need for any explanation.





Problem 3:Anagrams

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--)
{
string a,b;
cin>>a>>b;
map<char,int>m1,m2; //declared two map m1 and m2
int count=0;
for(int i=0;i<a.length();i++)
{
m1[a[i]]++; //key = string a ith character
} //value = 1 for each key
for(int i=0;i<b.length();i++)
{
m2[b[i]]++; //same for string b in map m2
}
for(char c='a';c<='z';c++)
{
count+=abs(m1[c]-m2[c]); //see note below
}
cout<<count<<endl;
}
}

A map stores key , value pairs. Map_name[key_name] returns the value corresponding to the key_name and if in square brackets there's a key_name for which we never declared it's corresponding value , in that case this function returns 0.

Let the string a be: cde and string b be: abc

therefore , according to our code map m1 will look like this:

key       value
c              1

d              1

e              1

and map m2:

key       value

a              1

b              1

c              1

The for loop will run from c='a' to c='z' , first c = a:

in this case abs(m1[c]-m2[c]) ==> abs(m1[a]-m2[a]) ==> abs(0 - 1) ==> 1 that means 1 deletion because 'a' only lies in one of the string and must be deleted. The count variable stores the no. of deletions.

If the value of abs(m1[c]-m2[c]) = 0 means the character exists in both strings , therefore no deletion  required and the value of count remains unchanged.

 

 

 

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

Comments

Popular posts from this blog

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

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