|
| 1 | +public class RBS { |
| 2 | + public static void main(String[] args) { |
| 3 | + int[] arr = {1,2,3,4,5,5,6}; |
| 4 | + System.out.println(findPivotWithDuplicates(arr)); |
| 5 | + } |
| 6 | + |
| 7 | + static int search(int[] nums, int target) { |
| 8 | + int pivot = findPivot(nums); |
| 9 | + |
| 10 | + // if you did not find a pivot, it means the array is not rotated |
| 11 | + if (pivot == -1) { |
| 12 | + // just do normal binary search |
| 13 | + return binarySearch(nums, target, 0 , nums.length - 1); |
| 14 | + } |
| 15 | + |
| 16 | + // if pivot is found, you have found 2 asc sorted arrays |
| 17 | + if (nums[pivot] == target) { |
| 18 | + return pivot; |
| 19 | + } |
| 20 | + |
| 21 | + if (target >= nums[0]) { |
| 22 | + return binarySearch(nums, target, 0, pivot - 1); |
| 23 | + } |
| 24 | + |
| 25 | + return binarySearch(nums, target, pivot + 1, nums.length - 1); |
| 26 | + } |
| 27 | + |
| 28 | + static int RotationCount(int [] arr){ |
| 29 | + int pivot=findPivot(arr); |
| 30 | + return pivot+1; |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + static int binarySearch(int[] arr, int target, int start, int end) { |
| 35 | + while(start <= end) { |
| 36 | + // find the middle element |
| 37 | +// int mid = (start + end) / 2; // might be possible that (start + end) exceeds the range of int in java |
| 38 | + int mid = start + (end - start) / 2; |
| 39 | + |
| 40 | + if (target < arr[mid]) { |
| 41 | + end = mid - 1; |
| 42 | + } else if (target > arr[mid]) { |
| 43 | + start = mid + 1; |
| 44 | + } else { |
| 45 | + // ans found |
| 46 | + return mid; |
| 47 | + } |
| 48 | + } |
| 49 | + return -1; |
| 50 | + } |
| 51 | + |
| 52 | + // this will not work in duplicate values |
| 53 | + static int findPivot(int[] arr) { |
| 54 | + int start = 0; |
| 55 | + int end = arr.length - 1; |
| 56 | + while (start <= end) { |
| 57 | + int mid = start + (end - start) / 2; |
| 58 | + // 4 cases over here |
| 59 | + if (mid < end && arr[mid] > arr[mid + 1]) { |
| 60 | + return mid; |
| 61 | + } |
| 62 | + if (mid > start && arr[mid] < arr[mid - 1]) { |
| 63 | + return mid-1; |
| 64 | + } |
| 65 | + if (arr[mid] <= arr[start]) { |
| 66 | + end = mid - 1; |
| 67 | + } else { |
| 68 | + start = mid + 1; |
| 69 | + } |
| 70 | + } |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + static int findPivotWithDuplicates(int[] arr) { |
| 75 | + int start = 0; |
| 76 | + int end = arr.length - 1; |
| 77 | + while (start <= end) { |
| 78 | + int mid = start + (end - start) / 2; |
| 79 | + // 4 cases over here |
| 80 | + if (mid < end && arr[mid] > arr[mid + 1]) { |
| 81 | + return mid; |
| 82 | + } |
| 83 | + if (mid > start && arr[mid] < arr[mid - 1]) { |
| 84 | + return mid-1; |
| 85 | + } |
| 86 | + |
| 87 | + // if elements at middle, start, end are equal then just skip the duplicates |
| 88 | + if (arr[mid] == arr[start] && arr[mid] == arr[end]) { |
| 89 | + // skip the duplicates |
| 90 | + // NOTE: what if these elements at start and end were the pivot?? |
| 91 | + // check if start is pivot |
| 92 | + if (start < end && arr[start] > arr[start + 1]) { |
| 93 | + return start; |
| 94 | + } |
| 95 | + start++; |
| 96 | + |
| 97 | + // check whether end is pivot |
| 98 | + if (end > start && arr[end] < arr[end - 1]) { |
| 99 | + return end - 1; |
| 100 | + } |
| 101 | + end--; |
| 102 | + } |
| 103 | + // left side is sorted, so pivot should be in right |
| 104 | + else if(arr[start] < arr[mid] || (arr[start] == arr[mid] && arr[mid] > arr[end])) { |
| 105 | + start = mid + 1; |
| 106 | + } else { |
| 107 | + end = mid - 1; |
| 108 | + } |
| 109 | + } |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments