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

Problem 42: Minimize Cost

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)



"Still working on this problem....."
 

 

 

 

Problem 43: Magical Word

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 l;
      string s;
      cin>>l>>s;
      for(int i=0;i<s.length();i++)
       {
         if(s[i]<=69 && s[i]>=65)
         s[i]='C';
         else if(s[i]<=72 && s[i]>=70)
         s[i]='G';
         else if(s[i]<=76 && s[i]>=73)
         s[i]='I';
         else if(s[i]<=81 && s[i]>=77)
         s[i]='O';
         else if(s[i]<=86 && s[i]>=82)
         s[i]='S';
         else if(s[i]<=93 && s[i]>=87)
         s[i]='Y';
         else if(s[i]<=99 && s[i]>=94)
         s[i]='a';
         else if(s[i]<=102 && s[i]>=100)
         s[i]='e';
         else if(s[i]<=105 && s[i]>=103)
         s[i]='g';
         else if(s[i]<=108 && s[i]>=106)
         s[i]='k';
         else if(s[i]<=111 && s[i]>=109)
         s[i]='m';
         else if(s[i]<=127 && s[i]>=112)
         s[i]='q';
else
         s[i]='C';
         }
         cout<<s<<endl;
     }  
}

This code is simple. There's no need for any explanation. Take a look at the ASCII table and try to understand what i did.
 

 

 

 

Problem 44: Best Index

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 i,j,l,k,n;
cin>>n;
long long a[n+1];
long double sum,val=-9999999999;
a[0]=0;
for(i=1;i<n+1;i++)
{
//same approach used as in play with numbers
cin>>a[i];
a[i]+=a[i-1];
}
for(i=1;i<n+1;i++)
{
sum=0;
k=i;
l=1;
while(k+l<n)
{
l++;
k+=l;
}
sum=a[k]-a[i-1];
if(sum>val)
val=sum;
}
cout<<fixed<<setprecision(0)<<val<<endl; //see note below
}

If you will use only setprecision(0) , the result will come in scientific notation , so use fixed with it , if you don't want that. The answer will not be accepted if you use alone setprecision(0).

By the way you can also use directly this ---> printf("%.0Lf\n",val);

 

 

 

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