I find it hard to guess how you actually intended to solve this, so it is hard to pin-point to any specific issue. Take for example the most inner loop, assuming that its goal was to calculate the amount of wood obtained after cutting at height sr
, then it is incorrect, because it starts from index mid
and there is no reason to think that all trees in range [0..mid)
are smaller than sr
and thus can be safely excluded. There is also no reason to think that all trees in range [mid .. n)
have a height of at least sr
, and thus arr[i] - sr
is non-negative.
Of course, my assumption about what this code was intended to do may be wrong, so this remark might be quite moot as you don't have any comments describing your intention.
Another thing that looks strange are updates to lo
and hi
. First, lo
is always zero, and hi
is unconditionally changed to mid = hi / 2
, so hi
essentially changes as follows:
- (n - 1) / 2
- (n - 1) / 4
- (n - 1) / 8
- ...
Not sure what purpose does that serve.
Note: Passing vector by value in findHi
seems quite unnecessary, similarly for using min_element
/ max_element
on sorted array.