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
28 changes: 28 additions & 0 deletions 154_rotated_sorted_array_min.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int findMin(int[] nums) {
int low =0;
int n = nums.length;
int 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[mid]>=nums[low]){
low = mid + 1;
}else{
high = mid -1 ;
}
}

return -1;
}
}

/*There are few things to consider to solve this question -
(1) The minimum is always on the other side of the sorted half - thats an observation
(2) Whenever you see that its a sorted array - think of Binary search (also it is O(logn))
(3) One mistake that i made was while submitting it was to write the following statement - realised when dry run the attempt - if (nums[low]<=nums[high]) return nums[low];
*/
16 changes: 16 additions & 0 deletions 162_find_peak_element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int findPeakElement(int[] nums) {
// concept of slope was introduced here
// where every there is slope - we use binary search
// till now our thought process was binary search == sorted array but it changes a bit here
int low =0 ;
int 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 (nums[mid]<nums[mid+1]) low = mid+1;
else high = mid - 1;
}
return -1;
}
}
39 changes: 39 additions & 0 deletions 34_find_first_and_last_element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public int[] searchRange(int[] nums, int target) {
int first = binarySearchFirst(nums,0,nums.length-1,target);
if (first==-1) return new int[]{-1,-1};
int last = binarySearchLast(nums, first, nums.length - 1, target);
return new int[]{first, last};
}
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-1] != target){
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 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+1] != target) return mid;
else low = mid +1;
}
else if (nums[mid]>target){
high = mid - 1;
}else low = mid+1;
}
return -1;
}
}

// We used two binary search indivually to find the values for the first and last
// also for values with just single value it should return the same postion twice - like if 5 appears at 0 -{0,0}