HackerEarth's basic programming solutions( Palindromic String, Find Product, Cost of balloons ) :

Problem 4: Palindromic String

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;
//comparing string s with the reversed string s 
if(s==string(s.rbegin(),s.rend()))
{
cout<<"YES"<<endl;
}
else{
    cout<<"NO"<<endl;
}
}

Here rbegin() and rend() are string methods.

 

 

 

 

Problem 5: Find Product

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 ele;
    vector<int>v; //declared a vector STL
    long long int n,i,prod=1;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>ele;
        v.push_back(ele);
    }
for(i=0;i<n;i++)
    {
      prod=(prod*v[i])%1000000007;
    }
    cout<<prod<<endl;
}

 In the same way , you can also do it using arrays.





Problem 6: Cost of balloons

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; //t=no. of test cases
cin>>t;
while(t--)
{
int n,g,p,f=0,s=0;
//n=no. of participants, g=green balloon cost, p=purple balloon cost
//f will count no. of participants who solved first question 
//s will count no. of participants who solved second question 
cin>>g>>p;
cin>>n;
for(int i=0;i<n;i++)
{
int l,m; //l:1 or 0, m:1 or 0(as per question)
cin>>l>>m;
if(l==1)
f++;
if(m==1)
s++;
}
int price1=0,price2=0;
price1=f*g+s*p; //see note below
price2=f*p+s*g;
if(price1<price2)
{
cout<<price1<<endl;
}
else
{
cout<<price2<<endl;
}
}
}

price1 will calculate the price in case if green colored balloon is given to the first question solver and purple colored balloon to those who have solved the second question.

price2 will calculate the price in case if purple colored balloon is given to the first question solver and green colored balloon to those who have solved the second question.

Now we have to output , which price is minimum price1 or price2.

 

 

 

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

Comments

  1. can u please upload solution of HackerEarth Questions in Python language.

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