I had the simple problem :
the top level of the pyramid must consist of 1 cube, the second level must consist of 1 + 2 = 3 cubes, the third level must have 1 + 2 + 3 = 6 cubes, and so on. Thus, the i-th level of the pyramid must have 1 + 2 + ... + (i - 1) + i cubes.
What is the maxmum height possible given n cubes.
I coded easily the following solution and passed :
int main()
{
int i,sum,n;
cin >> n;
i = 1;
sum = 0;
while(sum <= n)
{
sum = sum + (i*(i+1))/2;
i++;
}
cout << i-2;
return 0;
}
But, I am not confident on the complexity of this solution. Shall it be O(log n) or something else. I asked a similar question, where I had the same confusion. Can anyone please explain some brief general theory in such cases, such that it covers all such scenarios. Or a link to some good tutorial will be really helpful