Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Search Problem solved of Leetcode #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions leetcode/BinarySearch/CheckIfNAndDoubleExist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package leetcode.BinarySearch;

public class CheckIfNAndDoubleExist {

public static boolean checkIfExist(int[] arr) {
// if the arr size is two then we will check if the arr[0] is 2*arr1
// of arr0/2 == arr1 the true else false
if(arr.length == 2){
if(arr[0] == 2*arr[1] || arr[0]/2 == arr[1]){
return true;
}
return false;
}
// here we will check for every value and pass to binery search array
for(int i = 0;i < arr.length;i++){
if(binarySearch(arr,(2*arr[i]),i)){
return true;
}
if(arr[i] % 2 == 0 && binarySearch(arr,(arr[i]/2),i)){
return true;
}
}
return false;
}
public static boolean binarySearch(int[] arr,int target,int i){
int start = i+1;
int end = arr.length-1;
while(start <= end){
int mid = start +(end - start)/2;
if(arr[mid] == target || arr[start] == target || arr[end] == target){
return true;
}else {
start = start + 1;
end = end - 1;
}
}
return false;
}
}
79 changes: 79 additions & 0 deletions leetcode/BinarySearch/FindInMountain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package leetcode.BinarySearch;

//here they have provided the interface which contain the get() index and length
// and catch is it should not call the get() function more then 100 times

/**
* // This is MountainArray's API interface.
* // You should not implement it, or speculate about its implementation
* interface MountainArray {
* public int get(int index) {}
* public int length() {}
* }
*/

public class FindInMountain {

public int findInMountainArray(int target, MountainArray mountainArr) {
// find the peakValue which ic pivot value
// the array me contain duplicate
int peakIndex = peakValue(mountainArr);

// and check if the value present in left first as this is not sorted array

int leftSide = binarySearch(mountainArr, target, 0, peakIndex);

// if value is present in left then return
if(leftSide != -1){
return leftSide;
}else{
// else check if the value is present in right array
return binarySearch(mountainArr, target, peakIndex+1, mountainArr.length()-1);
}
};
public static int binarySearch (MountainArray array, int target,int startIndex,int endIndex){
boolean isAscen = array.get(startIndex) < array.get(endIndex);

while(startIndex <= endIndex){
int midIndex = (startIndex + endIndex) / 2 ;

int midValue = array.get(midIndex);
if (midValue == target){
return midIndex;
}

if(isAscen){
if(midValue < target){
startIndex = midIndex + 1;
}
if(midValue > target){
endIndex = midIndex - 1;
}
}else{
if(midValue > target){
startIndex = midIndex + 1;
}
if(midValue < target){
endIndex = midIndex - 1;
}
}

}
return -1;
};

public static int peakValue(MountainArray array){
int start = 0;
int mid = 0;
int end = array.length()-1;
while(start != end){
mid = start + (end - start)/2;
if(array.get(mid+1) > array.get(mid)){
start = mid + 1;
}else{
end = mid;
}
}
return start;
};
}
21 changes: 21 additions & 0 deletions leetcode/BinarySearch/FindPeakElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package leetcode.BinarySearch;

public class FindPeakElement {
// this is similar to finding pivot
public int findPeakElement(int[] nums) {
int start = 0;
int mid = 0;
int end = nums.length-1;
while(start != end){
mid = start + (end - start)/2;
if(nums[mid+1] > nums[mid]){
start = mid + 1;
}else{
end = mid;
}
}
// the loop will exist when start == end so we can pass any value start or end
return start;
}
}

45 changes: 45 additions & 0 deletions leetcode/BinarySearch/FirstAndTheLastPositionOfElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package leetcode.BinarySearch;

class FirstAndTheLastPositionOfElement{
public int[] searchRange(int[] nums, int target) {
int[] ans = {-1,-1};
// we will be dividing the searching in two parts as starting searching from
// starting index or start searching form the end part
int startIndex = binarySearch(nums,target,true);
int endIndex = binarySearch(nums,target,false);
ans[0] = startIndex;
ans[1] = endIndex;
return ans;
}


public int binarySearch(int[] nums, int target,boolean isSearchingFromStart){
int ans = -1;
int start = 0;
int end = nums.length - 1;
// search in simple binary search
while(start <= end){
int mid = start + (end - start)/2;
// if mid element is greater then target then search in left side by change
// the position of end
if(nums[mid] > target){
end = mid - 1;
// if mid is less the target then search in right by changing the position
// of the start index
}else if(nums[mid] < target){
start = mid + 1;
}else{
// if we found target the search for the start point on left and end point
// form the right
ans = mid;
if(isSearchingFromStart){
end = mid - 1;
}else{
start = mid+1;
}
}
}
return ans;

}
}
30 changes: 30 additions & 0 deletions leetcode/BinarySearch/FirstBadVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package leetcode.BinarySearch;

/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */

public class FirstBadVersion {
public int firstBadVersion(int n) {
int start = 1;
int end = n;
int ans = 0;
// here they have provided the funtion of badVersion which will return the
// if the version is true or false
if(isBadVersion(start)){
return start;
}
while(start <= end){
int mid = start +(end - start)/2;
if(isBadVersion(mid)){
// where the mide version is bad means we can save the ans
//and try to check finding the first bad version by changing the
// end index position
ans = mid;
end = mid - 1;
}else{
start = mid + 1;
}
}
return ans;
}
}
31 changes: 31 additions & 0 deletions leetcode/BinarySearch/GuessNumberHigherOrLower.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package leetcode.BinarySearch;

/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is higher than the picked number
* 1 if num is lower than the picked number
* otherwise return 0
* int guess(int num);
*/

// they have provided the Interface which has method guess will return if the number provided
// is higher or lower or equal
public class GuessNumberHigherOrLower {
public int guessNumber(int n) {
int start = 0;
int end = n;
while(start <= end){
int mid = start + (end - start)/2;
if(guess(mid) == 1){
start = mid + 1;
}else if(guess(mid) == -1){
end = mid - 1;
}else{
return mid;
}
}
return -1;
}

}
19 changes: 19 additions & 0 deletions leetcode/BinarySearch/LargetOrGreaterletterThenTarget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package leetcode.BinarySearch;

public class LargetOrGreaterletterThenTarget {
// similar to binary search ["c","f","j"] finding greater letter

public char nextGreatestLetter(char[] letters, char target) {
int start = 0;
int end = letters.length - 1;
while(start <= end){
int mid = start + (end - start)/2;
if(letters[mid] > target){
end = mid - 1;
}else{
start = mid + 1;
}
}
return letters[start % letters.length];
}
}
20 changes: 20 additions & 0 deletions leetcode/BinarySearch/PeakIndexInMountain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package leetcode.BinarySearch;


public class PeakIndexInMountain{
// similar as finding pivot
public int peakIndexInMountainArray(int[] arr) {
int start = 0;
int end = arr.length-1;
while(start < end){
int mid = start + (end - start)/2;
if(arr[mid] > arr[mid+1]){
end = mid;
}else{
start = mid + 1;
}
}
return start;
}

}
32 changes: 32 additions & 0 deletions leetcode/BinarySearch/SearchInsertPosition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package leetcode.BinarySearch;

public class SearchInsertPosition {
// this is similer solution as floor of the giving target number
// floor means - get the greater smallest value form the given target
// where we have to find the index of value which is greater smallest of the target value
// if the target is not present in the array
public int searchInsert(int[] nums, int target) {
int start = 0;
int end = nums.length-1;
// if the target is greater then the last value of the array which mean the position
// of the give target will the length of the array.
if(target > nums[end]){
return nums.length;
}
while(start <= end){
int mid = start + (end - start)/2;
if(nums[mid] < target){
start = mid + 1;
}else if(nums[mid] > target){
end = mid - 1;
}else{
return mid;
}
}
// if we don't found the target then we will return the start index becuase the
// while loop will exit when the the target is not and start cross the end value
// where start will pointing the greater smallest value of target value;
return start;

}
}
61 changes: 61 additions & 0 deletions leetcode/BinarySearch/SerachInRotatedSortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package leetcode.BinarySearch;

class SerachInRotatedSortedArray{
public int search(int[] array, int target) {
// this will find the index point where the array is been rotated
int pivot = findPivot(array);
// if it return -1 means array is not rotated which means simple binary search
if(pivot == -1){
return binarySearch(array, 0, array.length-1,target);
}

// if the target is == to pivot(the gretest value) then return pivot
if(array[pivot] == target){
return pivot;
}

// if first value of array is less then target then we have to find the target
// on the left side of array as pivot will be the end point
if(array[0] <= target){
return binarySearch(array, 0, pivot-1,target);
}

// else target is greater then first then serach on right side of the array
// with pivot as the starting point
return binarySearch(array, pivot+1, array.length-1,target);
}

public int findPivot(int[] array){
int start = 0;
int end = array.length -1;
while(start <= end){
int mid = start + (end - start)/2;
if(mid < end && array[mid] > array[mid+1]){
return mid;
}else if(mid > start && array[mid] < array[mid - 1]){
return mid - 1;
}else if(array[start] >= array[mid]){
end = mid - 1;
}else if(array[start] <= array[mid]){
start = mid + 1;
}else{
return mid;
}
}
return -1;
}

public int binarySearch(int[] array,int start,int end,int target){
while(start <= end){
int mid = start +(end - start)/2;
if(array[mid] < target){
start = mid + 1;
}else if(array[mid] > target){
end = mid - 1;
}else{
return mid;
}
}
return -1;
}
}
Loading