-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathindex_position.java
46 lines (42 loc) · 1.46 KB
/
index_position.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
/* BINARY SEARCH
Problem Statement:
Given an array of integers nums sorted in ascending order and a target ,
You need to search the target in the array nums.
If the target is found ,return the index position,else return -1
Time complexity:
WorstCase: O(log N) N--> length of the array
BestCase: O(1)
Example :
Input: nums = [-5,-2,0,2,5,30], target = 2
Output: 3
Explanation : target 2 is found at index 3
*/
public class IndexPosition {
// method for implementation of binary search
public static int search(int[] nums, int target) {
int start=0,end=nums.length-1;
while(start<=end){
int mid = start + (end-start)/2;
// if middle value is equal to target return the mid index
if(nums[mid] == target){
return mid;
}
// if mid value is greater than the target, search the left sub array
else if(nums[mid] > target){
end = mid - 1;
}
// if mid value is lesser than the target, search the right sub array
else if(nums[mid] < target){
start = mid + 1;
}
}
return -1;
}
public static void main(String[] args){
//get input array and target value
// you can also get input dynamically using Scanner class
int [] array = {-5,-2,0,2,5,30};
int target = 2;
System.out.print(search(array,target));
}
}