HISTOGRAM Stack based approach

is this code correct?

@i_am_dekard_shaw Your approach is correct but the thing is that it’s too slow. You cannot call min_after and min_before function for every element because this results in time complexity of your code to be O(n^2). Instead their exists a way to find all this in O(N). Try to find the next smallest element in O(N) time for the entire array using stack.

@sdevwrat ok how about this ?

@sdevwrat hello anyone ?

I still don’t think that you are correct while calculating the indices. Here is the code that finds the indices of required elements , now you just need to apply the formula https://ide.codingblocks.com/s/188030

@sdevwrat still not passing test cases

@i_am_dekard_shaw take long long int everywhere then this will work fine.

1 Like