HackerEarth's basic programming solutions( Number of steps, Friend's Relationship ) :

Problem 45: Number of 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()
{
int n,count=0;
cin>>n;
int a[n],b[n];
int m=1000000000;
for (int i=0; i<n; i++)
{
cin>>a[i];
m=min(m,a[i]);
}
for(int j=0; j<n; j++)
{
cin>>b[j];
}
bool notSame=true;
while(notSame)
{
notSame=false;
for(int i=0; i<n; i++)
{
while(a[i]>m && a[i]!=0)
{
a[i]=a[i]-b[i];
notSame=true;
count++;
}
if(m>a[i])
m=a[i];
if(m<0)
break;
}
}
if(m<0)
cout<<-1;
else
cout<<count;
}

Stay tuned for the explanation!!!
 

 

 

 

Problem 46: Friend's Relationship

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;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<i;j++)
            {
                cout<<"*";
            }
            for(int j=1;j<=2*(n-i);j++)
            {
                cout<<"#";
            }
            for(int j=0;j<i;j++)
            {
                cout<<"*";
            }
            cout<<endl;
        }
        cout<<endl;
    }
}

For these types of questions and to know from where the formulas strike , you should first practice the basic pyramids and patterns problems  , then you can easily understand this code.


 

 

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

Comments

  1. hello bro can explain this code for problem-45, I m not understand what u have done it.

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