-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram1.java
More file actions
67 lines (41 loc) · 1.33 KB
/
program1.java
File metadata and controls
67 lines (41 loc) · 1.33 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
/*
34. Find First and Last Position of Element in Sorted Array
Medium
Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
If target is not found in the array, return [-1, -1].
You must write an algorithm with O(log n) runtime complexity.
*/
package LeetCode;
public class program1 {
public static void main(String[] args) {
int nums[]={5,7,7,8,8,10};
int target =8;
int left =0;
int right = nums.length-1;
int output[] = new int[2];
while(left<=right){
if(nums[left]==target){
output[0] = left;
while(left<right){
if(nums[right]==target){
output[1] = right;
System.out.println(output[0]+" "+output[1]);
break;
}
else{
right -=1;
}
}
output[1] = left;
System.out.println(output[0]+" "+output[1]);
break;
}
else if(nums[left]<target){
left +=1;
}
else if(nums[right]>target){
right -=1;
}
}
}
}