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 2 #1919

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
126 changes: 126 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,70 @@ Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

Approach: Perform binary search twice to find the starting and ending position of an element in an array

Time Complexity: O(logn)
Space Complexity: O(1)

class Solution {
public int[] searchRange(int[] nums, int target) {

int n = nums.length;
int[] result = new int[]{-1, -1};
if(n == 0) return result;

int first = binarySearchFirst(nums, target, 0, n - 1);

if(first == - 1) return result;

int last = binarySearchLast(nums, target, first, n - 1);

return new int[]{first, last};
}

private int binarySearchFirst(int[] nums, int target, int low, int high) {

while(low <= high) {
int mid = low + (high - low)/2;

if(nums[mid] == target) {

if(mid == 0 || nums[mid] != nums[mid - 1]) {
return mid;
} else {
high = mid - 1;
}
} else if(nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}

private int binarySearchLast(int[] nums, int target, int low, int high) {

while(low <= high) {
int mid = low + (high - low)/2;

if(nums[mid] == target) {

if(mid == high || nums[mid] != nums[mid + 1]) {
return mid;
} else {
low = mid + 1;
}
} else if(nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
}

## Problem 2: (https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
Expand All @@ -37,6 +101,43 @@ Example 2:
Input: [4,5,6,7,0,1,2]
Output: 0

Approach: Perform binary search on the unsorted side of the array to find the minimum element

Time Complexity: O(logn)
Space Complexity: O(1)

class Solution {
public int findMin(int[] nums) {

int n = nums.length;
if(n == 0) return -1;

int low = 0, high = n - 1;

while(low <= high) {

// check for sorted array
if(nums[low] <= nums[high]) {
return nums[low];
}

int mid = low + (high - low)/2;

if((mid == 0 || nums[mid - 1] > nums[mid])
&& (mid == n - 1 || nums[mid + 1] > nums[mid])) {
return nums[mid];
} else if(nums[low] <= nums[mid]) {
// minimum element will be present on the unsorted side always
low = mid + 1;
} else {
high = mid - 1;
}
}

return -1;
}
}

## Problem 3: (https://leetcode.com/problems/find-peak-element/)
A peak element is an element that is greater than its neighbors.

Expand All @@ -62,4 +163,29 @@ Note:

Your solution should be in logarithmic complexity.

Approach: Perform binary search on the greater element side to find the peak.

Time Complexity: O(logn)
Space Complexity: O(1)

class Solution {
public int findPeakElement(int[] nums) {
if(nums.length == 0) return -1;

int low = 0, high = nums.length - 1;

while(low <= high) {
int mid = low + (high - low)/2;

if((mid == 0 || nums[mid] > nums[mid - 1]) && (mid == nums.length - 1 || nums[mid] > nums[mid + 1])) {
return mid;
} else if(mid > 0 && nums[mid] < nums[mid - 1]) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return -1;
}
}