|def findSortedPosition(nums, val):|
|"This solution takes O(n) time complexity and O(1) space complexity"|
|if nums == None: return [-1, -1]|
|if val == None or type(val) != int: return [-1, -1]|
|nums_less = 0|
|num_val = 0|
|for num in nums:|
|if num < val:|
|nums_less += 1|
|elif num == val:|
|num_val += 1|
|if num_val > 0:|
|return [nums_less, (nums_less+num_val)-1] #subtract one here because of 0 based indexing|
|return [-1, -1]|
Hello @Fredpwol, congratulations, you are one of the winners of the $20 award
Your solution was selected because it is most optimal in terms of time complexity. You were able to find a way to solve the problem without sorting the array. Smart!
I have made a blog post here about the different solutions and also announcing you as one of the winners for Week 3 of #AlgorithmFridays.
We will contact you in less than 24 hours for your award.
Congratulations once again and thanks for participating!