Hackerrank's Problem Solving solutions( Staircase, Mini-Max Sum, Birthday Cake Candles ) :
Problem 7: Staircase
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, space, rows, k=0;
cin>>rows;
for (i=1; i<=rows; ++i,k=0) {
for (space=1; space<=rows-i; ++space)
{
cout<<" ";
}
while (k!=i)
{
cout<<"#";
++k;
}
cout<<endl;
}
}
This code is simple. There's no need for any explanation.
Problem 8: Mini-Max Sum
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 a[5],i,sum=0;
for(i=0;i<5;i++)
{
cin>>a[i];
sum+=a[i];
}
sort(a,a+5);
cout<<sum-a[4]<<" "<<sum-a[0]; //see note below
}
The minimum sum will be given by (sum of the array - maximum_element_of_array) and the maximum sum will be given by (sum of the array - minimum_element_of_array)
After sorting the array using the sort function we know that the first element is minimum and the last element is maximum.
Problem 9: Birthday Cake Candles
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,c,count=0,j;
cin>>n;
long long a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
long long max=a[0];
for(c=0;c<n;c++) //finding the maximum element in the array
{
if(a[c]>=max)
{
max=a[c];
}
}
for(j=0;j<n;j++) //traversing through the array and increasing count by 1
{ //everytime we come accross the max element
if(max==a[j])
{
count++;
}
}
cout<<count;
}
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 for a question , comment below!
Comments
Post a Comment