The common interview problem of determining the missing value in a range from 1 to N has been done a thousand times over. Variations include 2 missing values up to K missing val
Whether the given solution is theoretically better than the sorting one depends on N and K. While your solution has complexity of O(N*log(N))
, the given solution is O(N*K)
. I think that the given solution is (same as the sorting solution) able to solve any range [A, B]
just by transforming the range [A, B]
to [1, N]
.