-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_first_and_last_position_of_element_in_sorted_array.js
46 lines (44 loc) · 1.53 KB
/
find_first_and_last_position_of_element_in_sorted_array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
//Runtime: 61 ms, faster than 96.49% of JavaScript online submissions for Find First and Last Position of Element in Sorted Array.
//Memory Usage: 42 MB, less than 98.27% of JavaScript online submissions for Find First and Last Position of Element in Sorted Array.
//https://leetcode.com/submissions/detail/812104599/
var searchRange = function(nums, target) {
let arrayIndex = [];
let aux1 = 1;//flag to find first position
let aux2 = 0;//flag to find last position
let left = 0;
let right = nums.length - 1;
let middle = 0;
while(left <= right){
middle = Math.floor((left + right)/2)
if(nums[middle] == target && nums[middle - 1 ] != target && aux1 == 1){//find first position
aux1 = 0;
aux2 = 1;
left = middle;
right = nums.length - 1;
arrayIndex.push(middle);
middle = right;
}
if(nums[middle] == target && nums[middle + 1 ] != target && aux2 == 1){//find last position
aux2 = 0;
left = middle + 1;
right = 0;
arrayIndex.push(middle);
}
if(aux1 == 1){
if(nums[middle] < target ){
left = middle + 1;
}else{
right = middle - 1;
}
}
if(aux2 == 1){
if(nums[middle] > target ){
right = middle - 1;
}else{
left = middle + 1;
}
}
}
return (arrayIndex.length)? arrayIndex : [-1,-1];
};