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
40 changes: 40 additions & 0 deletions FinaPeak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity : O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :
// I believe this algo works to move to highesr points as we are given both the end are -INFINITY
// that in turn mean going to the higher side will give you guranteed lower point->peak
// If the question is tweaked to have +INFINITY on both ends this algo of finding PEAK won't work, have to go for lienar

//A tweak may be find trough where both ends are +INFINITY, same algo

// Your code here along with comments explaining your approach in three sentences only
public class FinaPeak {
public int findPeakElement(int[] nums) {
int start = 0;
int end = nums.length -1;
while(start<=end)
{
int mid = start + (end-start)/2;
//check if mid is end nums[i] != nums[i + 1] and next -Infinity blindly return
//or for cases mid>0
//if mid==0, bring mid to end as in the last cond
if(mid==end || (mid>0 && (nums[mid]>nums[mid-1] && nums[mid]>nums[mid+1])))
{
return mid;
}
//nums[i] != nums[i + 1] for all valid i
//else can be same this algo won't work
else if(nums[mid]<nums[mid+1])
{
start = mid+1;
}
else
{
end = mid;
}

}
return -1;
}
}
40 changes: 40 additions & 0 deletions FindMin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity : O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this : same as RotatedArray


// Your code here along with comments explaining your approach in three sentences only
class FindMin {
public int findMin(int[] nums) {
int start = 0;
int end = nums.length -1;
//if sorted and not rorated return beginning
if(nums[start]<=nums[end])
{
return nums[start];
}

while(start<=end)
{
int mid = start + ((end-start)/2);
//found the pivot element
if (nums[mid]>nums[mid+1])
{
return nums[mid + 1];
}
//reject the other half,
// if nums[mid] to nums[end] is sorted, pivot lies first half
else if(nums[mid]<nums[end])
{
end = mid;
}
// if nums[start] to nums[mid] is sorted, pivot lies sec half
else if(nums[start]<nums[mid])
{
start = mid + 1;
}
}
return -1;
}
}
86 changes: 86 additions & 0 deletions FirstLastInArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Time Complexity : O(logn)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this : coming up with the logic, and check when to stop in both first and last index
// Time constarint

// Your code here along with comments explaining your approach in three sentences only
class FirstLastInArray {
public int[] searchRange(int[] nums, int target) {
//First edge case if array len is 0
if(nums.length==0)
{
return new int[]{-1,-1};
}
int start = 0;
int end = nums.length -1;
//edge case if target lies out of range
if(target<nums[start] || target>nums[end] )
{
return new int[]{-1,-1};
}
int firstIndex = findFirstIndex(nums,target,start,end);
int lastIndex = findLastIndex(nums,target,start,end);
return new int[]{firstIndex,lastIndex};
}

//Finds the first target element in sorted array
private int findFirstIndex(int[] nums,int target,int start,int end)
{
int index = -1;
while(start<=end)
{
int mid = start + (end-start)/2;
//can't move left further when mid is already start or mismatch
if(nums[mid]==target && (mid==start || nums[mid-1]!=nums[mid]))
{
index = mid;
break;
}
//can move left further
else if(nums[mid]==target)
{
end = mid -1;
}
//Same for finding element in sorted binary serach
else if(target<nums[mid])
{
end = mid - 1;
}else
{
start = mid + 1;
}
}
return index;

}

private int findLastIndex(int[] nums,int target,int start,int end)
{
int index = -1;
while(start<=end)
{
int mid = start + (end-start)/2;
//if mid is target and you can't move right further
if(nums[mid]==target && (mid==end || nums[mid]!=nums[mid+1]))
{
index = mid;
break;
}
//can move right further, start = mid +1
else if(nums[mid]==target)
{
start = mid+1;
}
//Same for finding element in sorted binary serach
else if(target<nums[mid])
{
end = mid - 1;
}else
{
start = mid + 1;
}
}
return index;
}
}