-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSearchInRotatedSortedArray.java
More file actions
60 lines (47 loc) · 1.71 KB
/
SearchInRotatedSortedArray.java
File metadata and controls
60 lines (47 loc) · 1.71 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
import java.util.Scanner;
public class SearchInRotatedSortedArray {
public int search(int[] nums, int target) {
int n = nums.length;
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[low] <= nums[mid]) {
if (target >= nums[low] && target < nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else {
if (target > nums[mid] && target <= nums[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
return -1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
SearchInRotatedSortedArray searchObj = new SearchInRotatedSortedArray();
System.out.print("Enter the number of elements in the rotated sorted array: ");
int n = scanner.nextInt();
int[] nums = new int[n];
System.out.println("Enter the elements of the rotated sorted array:");
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
System.out.print("Enter the target element to search for: ");
int target = scanner.nextInt();
int result = searchObj.search(nums, target);
if (result != -1) {
System.out.println("Target element " + target + " is found at index " + result);
} else {
System.out.println("Target element " + target + " is not found in the array.");
}
scanner.close();
}
}