-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFindFirstAndLastPositionOfElementInSortedArray.java
More file actions
78 lines (58 loc) · 2.38 KB
/
FindFirstAndLastPositionOfElementInSortedArray.java
File metadata and controls
78 lines (58 loc) · 2.38 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Runtime: 0 ms, faster than 100.00% of Java online submissions for Find First and Last Position of Element in Sorted Array.
Memory Usage: 47.2 MB, less than 60.82% of Java online submissions for Find First and Last Position of Element in Sorted Array.
*/
// Optimized Solution O(logn) SC: O(1)
class Solution {
public int[] searchRange(int[] nums, int target) {
// Creating Two Functions Of Modified Binary Search, One For Finding First Element And Another One For Last Element.
int[] result = new int[2];
result[0] = firstIndex(nums, target);
result[1] = lastIndex(nums, target);
// Returning Final Result
return result;
}
public int firstIndex(int[] nums, int target){
int index = -1;
int start = 0;
int end = nums.length - 1;
// Creating A Modified Binary Search For Finding The First Element
while(start <= end){
int mid = start + (end - start) / 2;
// When Target Is Greater Or Equal To The Target, We Move Left
// But In Case Of Equal We Also Update The Index In The Last If Block
if(nums[mid] >= target){
end = mid - 1;
}else{
start = mid + 1;
}
// Storing Index Whenever Current Element Is Equal To The Target
if(nums[mid] == target){
index = mid;
}
}
//Returning First Index If It Exists, Else Returning -1
return index;
}
public int lastIndex(int[] nums, int target){
int index = -1;
int start = 0;
int end = nums.length - 1;
// Creating A Modified Binary Search For Finding The Last Element
while(start <= end){
int mid = start + (end - start) / 2;
// When Target Is Smaller Or Equal To The Target, We Move Right
// But In Case Of Equal We Also Update The Index In The Last If Block
if(nums[mid] <= target){
start = mid + 1;
}else{
end = mid - 1;
}
if(nums[mid] == target){
index = mid;
}
}
//Returning Last Index If It Exists, Else Returning -1
return index;
}
}