Using max heap we can find i smallest elements in O( n * log i ) time
this method is very popular in coding interviews
@[ Jiren ] Nice answer
thank you ;)
@KartikGawande
NO
Even if i depends on “n” like i = n
the time complexity still wont exceed n*logi because (n-n)*logi+n = n which is O(nlogn)
Also remember that we cant take any parameter as constant unless mentioned in Algorithms and Data Structures
But writing Θ( n*logi ) is wrong we should write Θ( (n-i) logi + i )
64.3k questions
77.9k answers
244k comments
80.0k users