Skip to content
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
57 changes: 57 additions & 0 deletions Problem-1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Time Complexity : O(logn)
// Space Complexity : O(1)

class Solution {
// Perform binary search to find the first position of the target
// Perform a second binary search from the range of first occurance to the last index of the array to find the last position of the target
public int[] searchRange(int[] nums, int target) {
int n = nums.length;
if(n == 0)
return new int[] {-1,-1};
if(target < nums[0] || target > nums[n-1]) //target does not exist
return new int[]{-1,-1};
int first = binarySearchFirst(nums, 0, n-1, target);
if(first == -1) //target does not exist
return new int[]{-1,-1};
int second = binarySearchLast(nums, first, n-1, target);
return new int[]{first, second};
}

// Function to find the first position of target element
private int binarySearchFirst(int[] nums, int low, int high, int target) {
while(low <= high) {
int mid = low + (high - low) / 2;
if(nums[mid] == target) {
if(mid == 0 || nums[mid] > nums[mid-1])
return mid;
high = mid - 1;
}
else if(nums[mid] > target) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return -1;
}

// Function to find the last position of target element
private int binarySearchLast(int[] nums, int low, int high, int target) {
while(low <= high) {
int mid = low + (high - low) / 2;
if(nums[mid] == target) {
if(mid == nums.length - 1 || nums[mid] < nums[mid+1])
return mid;
low = mid + 1;
}
else if(nums[mid] > target) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return -1;
}
}
34 changes: 34 additions & 0 deletions Problem-2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Time Complexity : O(logn)
// Space Complexity : O(1)

class Solution {
// Perform binary search to find the minimum element in the array
// If array is already sorted return the first element
// Otherwise the minimum element will always be on the unsorted half
public int findMin(int[] nums) {
int n = nums.length;
int low = 0;
int high = n-1;

while(low <= high) {
// check if complete array is sorted
if(nums[low] <= nums[high])
return nums[low];

int mid = low + (high - low) / 2;
// if the mid element is the minimum element
if((mid == 0 || nums[mid] < nums[mid-1]) && (mid == n-1 || nums[mid] < nums[mid+1])) {
return nums[mid];
}
// if the left half is sorted reject left half
else if(nums[mid] >= nums[low]) {
low = mid + 1;
}
// Reject sorted right half
else {
high = mid - 1;
}
}
return 0;
}
}
29 changes: 29 additions & 0 deletions Problem-3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Time Complexity : O(logn)
// Space Complexity : O(1)

class Solution {
// Perform binary search and check if the current mid is the peak element
// If the left element of peak is greater than mid then move to left half otherwise move to right half
// Repeat till peak is found
public int findPeakElement(int[] nums) {
int n = nums.length;
int low = 0;
int high = n-1;

while(low <= high) {
int mid = low + (high-low) / 2;
// check if it is peak element
if((mid == 0 || nums[mid] > nums[mid-1]) && (mid == n-1 || nums[mid] > nums[mid+1]))
return mid;
// if left element of mid is greater than itself
else if(nums[mid+1] > nums[mid]) {
low = mid + 1;
}
// right element of mid is greater
else {
high = mid - 1;
}
}
return -1;
}
}