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
61 changes: 61 additions & 0 deletions Leetcode_153.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//Way 1:
//Perform binary search. Either of the side will be sorted. If both sides are sorted, then that part of array is sorted and min element will be starting positon. So, we will return element at 0th index.
//If only one of the array is sorted, then right side portion will not be sorted. Means min element will present at right side. So, shift low to mid+1
//TC: O(log n); SC: O(1)
class Solution {
public int findMin(int[] nums) {
int n=nums.length;
if(n==1) return nums[0];

int low=0,high=n-1;

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

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


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;
}
}

return nums[low];
}
}

//Way 2:

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

int low=0,high=n-1;

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

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


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;
}
}
51 changes: 51 additions & 0 deletions Leetcode_162.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//Way 1
//Applying binary search on the given array. If the mid element is bigger than it's adjacent elements, returning the mid. Else, moving to the side which is having bigger element than middle element. And performing binary search on it.

//TC:O(log n); SC: O(1)
class Solution {
public int findPeakElement(int[] nums) {
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(mid==0 || nums[mid]>nums[mid-1]){
low=mid+1;
}else{
high=mid-1;
}
}

return -1;

}
}

//Way 2
//Applying binary search on the given array. If the mid element is bigger than it's adjacent elements, returning the mid. Else, moving to the side which is having bigger element than middle element. And performing binary search on it.

//TC:O(log n); SC: O(1)
class Solution {
public int findPeakElement(int[] nums) {
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(mid==0 || nums[mid]>nums[mid-1]){
low=mid+1;
}else{
high=mid;
}
}

return low;

}
}
87 changes: 87 additions & 0 deletions Leetcode_34.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
///Way 1
//Linear search
//TC: O(n); SC: O(1)
class Solution {
public int[] searchRange(int[] nums, int target) {
int start=-1,end=-1;

int j=0;

while(j<nums.length && nums[j]!=target){
j+=1;
}

if(j==nums.length){
return new int[]{-1,-1};
}else if(j==nums.length-1){
return new int[]{nums.length-1,nums.length-1};
}else{
start=j;

end=j+1;

while(end<nums.length && nums[end]==target){
end+=1;
}
end=end-1;
}

return new int[]{start,end};

}
}

//Way 2:
//Perform binary search on the given nums to find target. From low to the target index, perform binary saerch to find start index. From the same target index to high, perform another binary search to find end index.
//TC: log n; SC: O(1)
class Solution {
public int[] searchRange(int[] nums, int target) {
int n=nums.length;

int low=0,high=n-1;
int start=-1,end=-1;

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

if(nums[mid]==target){
start=findFirst(nums,target,low,mid);
end=findLast(nums,target,mid,high);
break;
}else if(nums[mid]<target){
low=mid+1;
}else{
high=mid-1;
}
}

return new int[]{start,end};

}

private int findFirst(int[] nums, int target, int low, int high){
while(low<=high){
int mid=low+(high-low)/2;
if(nums[mid]>=target){
high=mid-1;
}else{
low=mid+1;
}
}

return low;
}

private int findLast(int[] nums,int target, int low, int high){
while(low<=high){
int mid=low+(high-low)/2;
if(nums[mid]<=target){
low=mid+1;
}else{
high=mid-1;
}
}

return low-1;
}
}