Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
531 views

imageimageimage

in Online Assessments by Expert (2,200 points) | 531 views

1 Answer

0 like 0 dislike

apply bs on equation k >= n*(n+1)*(2*n+1)/6 and n == answer

code: -

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

 

int square_sum(int n){

int res = n*(n+1)*(2*n+1)/6;

return res;

}

 

void solve(){

int ramSize;

cin>>ramSize;

int h=300,l=0,ans=-1;

while(l<=h){

int mid = l + (h-l)/2;

int total = square_sum(mid);

if(total<=ramSize){

ans = mid;

l = mid+1;

}

else{

h=mid-1;

}

}

cout<<ans<<endl;

}

 

int main(){

ll t = 1;

while(t--){

solve();

}

return 0;

}

 

by (180 points)