forked from super30admin/PreCourse-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_1.java
More file actions
40 lines (36 loc) · 1.48 KB
/
Exercise_1.java
File metadata and controls
40 lines (36 loc) · 1.48 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
// Time Complexity :O(nlogn) if sort the input array first otherwise if it is already sorted then O(logn)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode : N/A
// Any problem you faced while coding this : No
import java.util.Arrays;
import java.util.Collections;
class BinarySearch {
// Returns index of x if it is present in arr[l.. r], else return -1
int binarySearch(int arr[], int l, int r, int x) {
//Write your code here
Arrays.sort(arr); // sorting array in ascending order
int mid = (l + r) / 2; // calculating mid element
if (x > arr[mid]) { // if x > mid, then search in right half
mid++;
return binarySearch(arr,mid, r, x);
} else if (x < arr[mid]) { // if x < mid search in left half
mid--;
return binarySearch(arr,l, mid, x);
} else if (x == arr[mid]) // if mid == x, then return result
return mid;
return 0; // if not found return 0
}
// Driver method to test above
// public static void main(String args[])
// {
// BinarySearch ob = new BinarySearch();
// int arr[] = { 2, 3, 4, 10, 40 };
// int n = arr.length;
// int x = 10;
// int result = ob.binarySearch(arr, 0, n - 1, x);
// if (result == -1)
// System.out.println("Element not present");
// else
// System.out.println("Element found at index " + result);
// }
}