HackerEarth's basic programming solutions( Back to School, Minimum Steps, Cipher ) :

Problem 39: Back to School

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 a,b,c;
    cin>>a>>b>>c;
    if(a>b && a>c)
    {
        cout<<a;
    }
    else if(b>a && b>c)
    {
cout<<b;
    }
    else{
        cout<<c;
    }   
}

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

 

 

 

Problem 40: Minimum Steps

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 test,count,k,m,n,x;
cin>>test;
while(test--)
{
        count=0;
cin>>k>>m>>n;
while(k<m)
{
if(m%n==0)
{
m=m/n;
count++;
}
else
{
            x=(n-(m%n));
m+=(n-(m%n))/2*2+(n-(m%n))%2;
count+=x/2+x%2;
}
}
if(k>m)
{
count+=(k-m)/2+(k-m)%2;
}
cout<<endl<<count;
}
return 0;
}

I am trying to find out a simple and logical solution for this problem. Wait till i get one.
 

 

 

 

Problem 41: Cipher

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()
{
    string s;
    cin>>s;
    int k;
    cin>>k;
    int n=s.length();
    for(int i=0;i<n;i++)
    {
        if(s[i]>='a' && s[i]<='z')
        {
           s[i]=(s[i]+k-'a')%26+'a'; // %26 ==> in case k value exceeds 26 , the
             // result won't get affected
        }
        else if(s[i]>='A' && s[i]<='Z')
        {
            
            s[i]=(s[i]+k-'A')%26;
            if(s[i]<'A')
            s[i]=(s[i])%26+'A';
        }
        else if(s[i]>='0' && s[i]<='9')
        {
           s[i]=(s[i]+k-'0')%10+'0'; // for s[i]-'0' explanation , refer
             // problem 37 i.e Seven-segment display
        }
        else
        {
        continue;
        }
    }
    cout<<s;
}

This code is simple. There's no need for any explanation. Try to understand this code using example to understand the formulas and expressions used.
 

 

 

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

Comments

  1. Well explained and really it's very helpful for someone who is struggling with this type of assignment question. Thanks :) and I'm looking forward for your next blog...

    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 ) :