is this code correct?
HISTOGRAM Stack based approach
@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.
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