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
53 changes: 53 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Time Complexity : O(log n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this : Confused about the situations where we have to return [-1, -1].
// Approach: Perform 2 binasry searches. 1 for finding the first index of the target element and 2 for the last index for the target element
// If the target is not found in the first binary search than return [-1, -1]
// Or else, find the last index of the target by erformong the second binary search and return the indexes.
class Problem1 {
private int bs1(int [] arr, int low, int high, int target){
while(low <= high){
int mid = low + (high - low)/2;
if(arr[mid] == target){
if(mid == 0 || arr[mid] > arr[mid -1]){
return mid;
} else{
high = mid -1;
}
} else if(arr[mid] > target){
high = mid - 1;
} else{
low = mid + 1;
}
}
return -1;
}
private int bs2(int [] arr, int low, int high, int target){
while(low <= high){
int mid = low + (high - low)/2;
if(arr[mid] == target){
if(mid == arr.length-1 || arr[mid+1] > arr[mid]){
return mid;
} else{
low = mid + 1;
}
} else if(arr[mid] > target){
high = mid - 1;
} else{
low = mid + 1;
}
}
return -1;
}
public int[] searchRange(int[] nums, int target) {
int n = nums.length;
if(nums == null || n == 0) return new int[]{-1, -1};
if(target < nums[0]) return new int[]{-1, -1};
if(target > nums[n-1]) return new int[]{-1, -1};
int first = bs1(nums, 0, n-1, target);
if(first == -1) return new int[]{-1, -1};
int second = bs2(nums, first, n-1, target);
return new int[]{first, second};
}
}
31 changes: 31 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//Time Complexity: O(logn)
// Space Complexity: O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Forgot to take care of edge cases
// Approach: Perform binary search to find the pivot
// Compare the elements at low and high to find out if the array is sorted or not
// Otherwise check which side of the array is sorted and adjust the values of low and accordingly till you find the minimum element
class Problem2 {
public int findMin(int[] nums) {
int n = nums.length;
int low = 0, high = n-1;

while(low <= high){
if(nums[low] <= nums[high]){
return nums[low];
}

int mid = low + (high - low)/2;
if((mid == 0 || nums[mid] < nums[mid - 1]) && (mid == n-1 || nums[mid] < nums[mid + 1])){
return nums[mid];
}
else if(nums[low] <= nums[mid]){
low = mid + 1;
}else{
high = mid - 1;
}
}

return -1;
}
}
27 changes: 27 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Was coinfused a bit in writing the if conditions and edge cases
// Approach: Perform a binary search then compare mid with the neighbors if it is greater then retirn mid which will be the peak
// If not then move to the side which has the greater number then mid if the number to right of the mid is greater than move right otherwise left
// The binary search continues till the peak element is found
class Problem3 {
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;
if((mid ==0 || nums[mid] > nums[mid -1])
&&
(mid == n-1 || nums[mid] > nums[mid + 1])){
return mid;
} else if(mid != n-1 && nums[mid + 1] > nums[mid]){
low = mid + 1;
}else{
high = mid - 1;
}
}
return 113;
}
}