Sunday, 30 June 2019

Halloween Party Hacker Rank Solution in C, Minimum Height Triangle Hacker Rank Solution in C,Summing the N series Hacker Rank Solution in C,



Minimum Height Triangle

https://www.hackerrank.com/challenges/lowest-triangle/submissions/code/112097635

int lowestTriangle(int base, int area){
 
    return ceil((2.0*area)/base);
}
summing-the-n-series

https://www.hackerrank.com/challenges/summing-the-n-series/problem

Tn = n*n - (n-1)*(n-1) = n*n -n*n - 1 +2n = 2n-1

So the series is 1+3+ 5+....+2*n-1
Sum of n terms in Arithmetic Progression =  (n/2)*(2a+(n-1)*d) here a is 1 (first term)
d is common difference = 2
So sum = (n/2)*(2*1+(n-1)*2)  = (n/2)*(2+2*n-2)  = (n/2)*(2*n) = n*n

int summingSeries(long n) {
return ((n%1000000007)*(n%1000000007))%1000000007;

}
Halloween Party

Here in k lines, some must be horizontal and some vertical. We get maximum 1x1 pieces
if number of horizontal lines and vertical lines are equal

So if k is even then we cut the chocolate by (k/2) horizonatl lines and (k/2) vertical lines
giving (k/2)*(k/2) pieces

when k is odd we give extra one horizontal cut.
(K+1)/2 horizontal and
remaining lines would be k-((k+1)/2) vertical lines


unsigned long long int halloweenParty(unsigned long long int k) {
   
if(k%2 == 0)
    return (k/2)*(k/2);
return((k+1)/2)*((k-((k+1)/2)));

}

No comments:

Post a Comment