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
69 changes: 69 additions & 0 deletions find_first_and_last_position_of_element_in_sorted_array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Time Complexity : log(n) + log(m) ( log(m) becuase on the right part will wont be doing the binary search from 0 to N, but we can ignore log m becasue only higher order term matter)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : yes i bit, how we are reaching in first index and the last index is a bit compusing to me still but i'll know how we do it in terms of theory code is were i am stipping a bit

/*
* Approach
* in order to find the first and the last index we are basically doing the binary serach 2 times, one is to find the starts index of the number
* and then second time to find the index where the element occurs for the last time.
*
* we reduce the search space for the last element by using the first occurance index as low
*
*/

class Solution {
public int rightbinarysearch(int[] nums, int target, int low, int high) {
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;
} else {
low = mid + 1;
}
} else if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}

public int leftbinarysearch(int[] nums, int target, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target) {
if (mid == 0 || nums[mid] > nums[mid - 1]) {
return mid;
} else {
high = mid - 1;
}
} else if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}

public int[] searchRange(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
if (nums == null || nums.length == 0) {
return new int[] { -1, -1 };
}
if (target > nums[high]) {
return new int[] { -1, -1 };
}
if (target < nums[0]) {
return new int[] { -1, -1 };
}
int inital = leftbinarysearch(nums, target, low, high);
int last = rightbinarysearch(nums, target, inital, high);
return new int[] { inital, last };

}
}
33 changes: 33 additions & 0 deletions find_minimum_in_rotated_sorted_array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time Complexity : log(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no

/*
* Approach
* if the low value is smaller then high, that means our element is at low but if that id not the case
* we check is mids might me the high lowest by comparing mid -1 and mid + 1 to the mids if that also not the case then
* we need to eleminate one half of the side how we do that is before we
* we check if low it smaller then the mid if that is true that side is sorted and we eleminate that
*
*/

class Solution {
public int findMin(int[] nums) {
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[low] <= nums[high]) {
return nums[low];
} else if ((mid > 0 && nums[mid - 1] > nums[mid]) && (mid < nums.length - 1 && nums[mid] < nums[mid + 1])) {
return nums[mid];
} else if (nums[low] <= nums[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
}
29 changes: 29 additions & 0 deletions find_peak_element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Time Complexity : log(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : no

/*
* Approach
* idea here is to find the number that a peak meaning the number before and after that number are smaller then that number
* even though it is a sorted array we can apply binary search becasue of the conditiion that nums[i] != nums[i+1]
* we move the search space where we find the greater element when comparing elements
*/

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 - 1] > nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return 999;
}
}