- the numbers are looped through to check for numbers that are the target.
- the index of the numbers are then added to an array.
- since we are looking for the range , the minimum and max value of the indexes are returned
-
Time complexity:O(N)
-
Space complexity:O(N)
function searchRange(nums: number[], target: number): number[] {
let positions = []
for (let i = 0; i < nums.length; i++) {
if (nums[i] === target) {
positions.push(i)
}
}
if (positions.length === 0) positions.push(-1, -1)
return [Math.min(...positions),Math.max(...positions)]
};