HackerEarth's basic programming solutions( Divisible, Seven-Segment Display, A. Movement ) :

Problem 36: Divisible

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 n,sum=0,i;
cin>>n;
long long a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n/2;i++) //considering first half elements
{
while(a[i]>=10) //using this you can find first digit of a number
{
a[i]=a[i]/10;
}
 
if(i%2==0) //see note below
{
sum+=a[i];
}
else
{
sum+=(a[i]*-1);
}
}
for(i=n/2;i<n;i++) //considering next half elements
{
a[i]=a[i]%10; //using this you can find the last digit of a number
 
if(i%2==0)
{
sum+=a[i];
}
else
{
sum+=(a[i]*-1);
}
}
if(sum==0 || abs(sum)%11==0)
{
cout<<"OUI";
}
else{
cout<<"NON";
}

}

To understand the solution, we must know the divisibility rule for 11 i.e we have to consider the digits at even places as well as odd places of the final number formed. If we put the rule in simple way, we can say that given a number, if we add all the digits at even places and then subtract the summation of digits at odd places , the resultant must be divisible by 11 or the result is 0.

For example: consider the number : 188452

                       sum of digits at even places = 1+8+5 = 14

                       sum of digits at even places = 8+4+2 = 14

Now, (14 - 14) = 0 which means 188452 is divisible by 11.

What i have done in the question is, consider a variable sum which is initially equal to zero. If the digit is at even place we will add it to sum and update it's value. If the digit is at odd place we will subtract it from the sum and update it's value. Doing this will give us the desired result. Now with the help of sum value we can find out whether the number formed is divisible by 11 or not.


 

 

 

 

Problem 37: Seven-Segment Display

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;
    int t,count=0; //stores the number of matchsticks
    cin>>t;
    while(t--)
    {
        cin>>s;
        for(int i=0;i<s.length();i++) //counting total number of matchsticks used
        {
            if(s[i]-'0'==0) //see note below
     count+=6;
            else if(s[i]-'0'==1)
            count+=2;
            else if(s[i]-'0'==2)
            count+=5;
            else if(s[i]-'0'==3)
            count+=5;
            else if(s[i]-'0'==4)
            count+=4;
            else if(s[i]-'0'==5)
            count+=5;
            else if(s[i]-'0'==6)
            count+=6;
            else if(s[i]-'0'==7)
            count+=3;
            else if(s[i]-'0'==8)
            count+=7;
            else if(s[i]-'0'==9)
            count+=6;
            else
            cout<<"";
        }
if(count%2==0)
        {
            for(int j=0;j<count/2;j++)
            cout<<"1";
            count=0;
        }
        else{
            cout<<"7"; //see note below
            for(int p=1;p<(count-1)/2;p++)
            cout<<"1";
            count=0;
        }
        cout<<endl;
    }
}

Whenever there is a digit in a string , we cant directly do this string_name[index] = = 2 as string is made up of characters and so this is not valid. What's valid is this string_name[index] - '0' = = 2.

In case , the total no. of matchsticks are even , we can simply say that the maximum number that can be formed will be 1111... where no. of 1s are equal to (no. of matchsticks)/2 .

If the total number of matchsticks are odd , then we have to include 7 also as it is made up of 3 matchsticks.

for example: No. of matchsticks = 13 , therefore the numbers can be formed are 711111 or 111117 or 111111( not possible as one matchstick left) , therefore the optimal solution for this is to use 7 in front followed by 1s.
 

 

 

 

Problem 38: A. Movement

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 n;
    cin>>n;
    if(n%5!=0)
    cout<<((n-(n%5))/5)+1<<endl;
    else
    cout<<n/5<<endl;
}

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

 

 

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

Comments

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