We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The formula for calculation sum of squares of first natural number is:
(n * (n+1) * (2*n + 1))/6
So the thought process is start giving numbers to this formula from 1 and increment is by 1 till it's return values is less or equal to given "x" by testcases
Note: This solution is a brute force approach as well as it pass all the test cases
Solution:
#include<math.h>#define ll unsigned long longllsum_of_squares(lln){return(n*(n+1)*(2*n+1))/6;}voidsuccessful_donations(llx){intcounter=1;while(sum_of_squares(counter)<=x){counter++;}printf("%d\n",counter-1);}intmain(){intt;llx;scanf("%d",&t);while(t--){scanf("%lld",&x);successful_donations(x);}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Little Ashish's Huge Donation
You are viewing a single comment's thread. Return to all comments →
The formula for calculation sum of squares of first natural number is:
(n * (n+1) * (2*n + 1))/6
So the thought process is start giving numbers to this formula from 1 and increment is by 1 till it's return values is less or equal to given "x" by testcases
Note: This solution is a brute force approach as well as it pass all the test cases
Solution: