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;
}