-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindElementPositionsInDupSortedArray.java
More file actions
179 lines (132 loc) · 4.68 KB
/
FindElementPositionsInDupSortedArray.java
File metadata and controls
179 lines (132 loc) · 4.68 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package Algorithms.BinarySearch;
import java.util.Arrays;
/**
* Find First and Last Position of Element in Sorted Array
*
*
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 06 Feb 2025
* @link 34. Find First and Last Position of Element in Sorted Array <a href="https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/">LeetCode link</a>
* @topics Array, Binary Search
* @companies amazon, google, microsoft, linkedin, applied, instacart, bloomberg, goldman, apple, oracle, tiktok, splunk, atlassian, capgemini, airtel, adobe, uber, tinkoff, yahoo, accenture, paypal, tcs, de, pinterest, zillow
*/
public class FindElementPositionsInDupSortedArray {
public static void main(String[] args) {
int[] nums = {5,7,7,8,8,10};
int target = 8;
System.out.println("searchRange(nums, target) => " + Arrays.toString(searchRange(nums, target)));
System.out.println("searchRangeMyApproach(nums, target) => " + Arrays.toString(searchRangeMyApproach(nums, target)));
}
public static int[] searchRange(int[] nums, int target) {
int[] res = new int[2];
res[0] = binarySearch(nums, target, true);
res[1] = binarySearch(nums, target, false);
return res;
}
// this is the combination of below #findFirst and #findLast methods
private static int binarySearch(int[] nums, int target, boolean isFirst) {
int n = nums.length, l = 0, r = n-1, res = -1, mid;
while(l<=r) {
mid = l + (r-l)/2;
if(nums[mid]==target) {
res = mid;
if(isFirst) {
r=mid-1;
} else {
l=mid+1;
}
} else if(nums[mid] < target) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return res;
}
private static int findFirst(int[] nums, int target) {
int l = 0, r = nums.length - 1, res = -1;
while (l <= r) {
int mid = (r + l) / 2;
if (nums[mid] == target) {
res = mid;
r = mid - 1;
} else if (nums[mid] < target) l = mid + 1;
else r = mid - 1;
}
return res;
}
private static int findLast(int[] nums, int target) {
int l = 0, r = nums.length - 1, res = -1;
while (l <= r) {
int mid = (r + l) / 2;
if (nums[mid] == target) {
res = mid;
l = mid + 1;
} else if (nums[mid] < target) l = mid + 1;
else r = mid - 1;
}
return res;
}
public int[] searchRange2(int[] nums, int target) {
int n = nums.length;
int[] res = new int[2];
res[0] = findFirstOccurence(nums, target, 0, n-1);
res[1] = findLastOccurence(nums, target, res[0], n-1);
return res;
}
private int findFirstOccurence(int[] nums, int target, int l, int r) {
int n = nums.length;
while(l<=r) {
int m = l + (r-l)/2;
if(nums[m] < target) {
l = m+1;
} else {
r = m-1;
}
}
return (l == n || nums[l] != target)? -1 : l;
}
private int findLastOccurence(int[] nums, int target, int l, int r){
if(l == -1) {
return -1;
}
while(l<=r) {
int mid = l + (r-l)/2;
if(nums[mid]==target) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return r;
}
public static int[] searchRangeMyApproach(int[] nums, int target) {
int l=0, r=nums.length-1;
while(l<=r) {
int mid = (l+r)/2;
if (target == nums[mid]) {
l = r = mid;
break;
} else if (target > nums[mid]) {
l = mid + 1;
if (l < nums.length && target == nums[l]){
r = l;
break;
}
// optional: if (l > nums.length-1) continue; int num = nums[l]; while (l+1 < nums.length && nums[l+1] == num) l++;
}
else {
r = mid-1;
if (r >= 0 && target == nums[r]){
l = r;
break;
}
// optional: if (r < 0) continue; int num = nums[r]; while (r-1 >= 0 && nums[r-1] == num) r--;
}
}
if (l>r) return new int[]{-1, -1}; // not found
while (l-1 >= 0 && nums[l-1] == target) l--;
while (r+1 < nums.length && nums[r+1] == target) r++;
return new int[]{l,r};
}
}