You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Intution :- Whenever mid stand it will give : either leftPartSorted or RightPartSorted so we check for the existence of target in sorted part, if not found then we again continue our search in unsorted part.
classSolution {
publicintsearch(int[] nums, intx) {
intn = nums.length;
ints = 0;
inte = n-1;
while(s<=e){
intmid = s+(e-s)/2;
if(nums[mid] == x) returnmid;
// identify the sorted part// if LPSif(nums[s] <= nums[mid]){
// check if x lies in LSPif(x >= nums[s] && x < nums[mid]) e = mid-1;
elses = mid+1;
}
// if RPSelseif(nums[mid] <= nums[e]){
if(x > nums[mid] && x <= nums[e]) s = mid+1;
elsee = mid-1;
}
}
return -1;
}
}
13. Search in an infinite sorted array [3, 5, 6, 8, 9, 10, 12, 15...♾️]
Approach
Don't think about infinity, for performing BS you just need searchSpace i.e., range where your target can lie between start and end so just figure out start and end and perform normal BS in that range. We are Increasing our search space by 2 in each itteration.
intsearchInfiniteSortedArray(intarr[], intx){
ints = 0;
inte = 1;
// find the rangewhile(x > arr[e]){
s = e+1;
e = e + (e-s+1)*2;
}
returnbs(arr, s, e, x);
}
14. You will be given a value X and a sorted array, you need to find minimum absolute difference you can get with X and eles in an array.
Approach :- Do a normal BS if found then return 0 else return MINIMUM(abs(x-ceil), abs(x-floor));
You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. Return the single element that appears only once.
classSolution {
publicintsingleNonDuplicate(int[] arr) {
intn = arr.length;
if(n==1) returnarr[0];
if(arr[0]!=arr[1]) returnarr[0];
if(arr[n-1]!=arr[n-2]) returnarr[n-1];
// here we are reducing our search space for preventing indexOutOfBound while updating start and end.ints = 2;
inte = n-3;
while(s<=e){
intmid = s + (e-s)/2;
if(isSingle(arr,mid)) returnarr[mid];
if(isRightOfSingle(arr,mid)){
if(arr[mid] == arr[mid-1]) e = mid-2;
elsee = mid - 1;
}
elseif(isLeftOfSingle(arr,mid)){
if(arr[mid] == arr[mid+1]) s = mid+2;
elses = mid + 1;
}
}
return7798;
}
// order is (o,e)privatebooleanisRightOfSingle(intarr[], intmid){
if(mid%2 != 0 && arr[mid]==arr[mid+1]) returntrue;
if(mid%2 == 0 && arr[mid]==arr[mid-1]) returntrue;
returnfalse;
}
// order is (e,o)privatebooleanisLeftOfSingle(intarr[], intmid){
if(mid%2 == 0 && arr[mid]==arr[mid+1]) returntrue;
if(mid%2 != 0 && arr[mid]==arr[mid-1]) returntrue;
returnfalse;
}
privatebooleanisSingle(intarr[], intmid){
returnarr[mid]!=arr[mid+1] && arr[mid]!=arr[mid-1];
}
}
classSolution {
publicintpeakIndexInMountainArray(int[] arr) {
returnpeakIndexMountain(arr,0,arr.length);
}
intpeakIndexMountain(intarr[], intfromIndex, inttoIndex){
intstart = 0;
intend = toIndex-1;
while(start < end) {
intmid = start + (end - start)/2;
if(arr[mid+1]>arr[mid]){
// we are in ascending part so there may be more bigE furtherstart=mid+1;
}
elseif(arr[mid]>arr[mid+1]){
// we are in descending part so our ans can be where we r standing only when there is no bigE further from behindend=mid;
}
}
// in the end start == end point to the peak of the mountainreturnstart; // return start or end since both are same
}
}