forked from super30admin/Binary-Search-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstAndLastPositionOfElementInSortedArray.java
53 lines (38 loc) · 1.72 KB
/
FirstAndLastPositionOfElementInSortedArray.java
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
package com.hashmap;
public class FirstAndLastPositionOfElementInSortedArray {
public static void main(String[] args) {
int[] nums = {5,7,7,8,8,10,8};
int target = 8;
FirstAndLastPositionOfElementInSortedArray obj = new FirstAndLastPositionOfElementInSortedArray();
System.out.println("first and last position of target; start index = "+obj.searchRange(nums,target)[0]+" end index="+obj.searchRange(nums,target)[1]);
}
public int[] searchRange(int[] nums, int target) {
return new int[]{findLeftMostOcurrence(nums,target),findRightMostOcurrence(nums,target)};
}
public int findLeftMostOcurrence(int[] nums, int target)
{
int index = -1;
int left = 0;
int right = nums.length - 1;
while(left<=right)
{ int mid = left + (right - left)/2;
if(nums[mid]<target) left = mid + 1; //ocurence of target must be on the right side of the mid
else right = mid - 1;
if(nums[mid]==target) index = mid; //we are storing the location of the latest ocurrence of target
}
return index;
}
public int findRightMostOcurrence(int[] nums, int target)
{
int index = -1;
int left = 0;
int right = nums.length - 1;
while(left<=right)
{ int mid = left + (right - left)/2;
if(nums[mid]>target) right = mid - 1; //ocurence of target must be on the left side of the mid
else left = mid + 1;
if(nums[mid]==target) index = mid; //we are storing the location of the latest ocurrence of target
}
return index;
}
}