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
64 changes: 64 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,64 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

/*
Approach:
We perform binary search twice — first to find the first occurrence and then to find the last occurrence of the target.
In the first search, once we find the target, we continue searching on the left to ensure it is the first index, and similarly search right for the last occurrence.
If the target is not found, we return [-1, -1]; otherwise, we return the indices of first and last occurrence.
*/

class Solution {

private int binarySearchFirst(int[] nums, int target, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;

if (target == nums[mid]) {
if (mid == 0 || nums[mid - 1] != target) {
return mid;
} else {
high = mid - 1;
}
} else if (target < nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}

private int binarySearchLast(int[] nums, int target, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;

if (target == nums[mid]) {
if (mid == nums.length - 1 || nums[mid + 1] != target) {
return mid;
} else {
low = mid + 1;
}
} else if (target < nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}

public int[] searchRange(int[] nums, int target) {
int first = binarySearchFirst(nums, target, 0, nums.length - 1);

if (first == -1) {
return new int[]{-1, -1};
}

int last = binarySearchLast(nums, target, first, nums.length - 1);

return new int[]{first, last};
}
}
45 changes: 45 additions & 0 deletions find-minimum-in-rotated-sorted-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes (for no-duplicate version)
// Any problem you faced while coding this : No

/*
Approach:
We use binary search to find the minimum element (pivot) in a rotated sorted array by identifying the unsorted half.
If the current range is already sorted, we directly return nums[low], otherwise we check if mid is the pivot by comparing neighbors.
If left half is sorted, we move right; otherwise, we move left to continue searching for the minimum.
*/

class Solution {
public int findMin(int[] nums) {
int low = 0;
int high = nums.length - 1;

while (low <= high) {
// If already sorted, the smallest element is at low
if (nums[low] <= nums[high]) {
return nums[low];
}

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

// Check if mid is the minimum (pivot)
if ((mid == 0 || nums[mid] < nums[mid - 1]) &&
(mid == nums.length - 1 || nums[mid] < nums[mid + 1])) {
return nums[mid];
}

// Left half is sorted → pivot must be in right half
else if (nums[low] <= nums[mid]) {
low = mid + 1;
}

// Right half is sorted → pivot must be in left half
else {
high = mid - 1;
}
}

return -1;
}
}
39 changes: 39 additions & 0 deletions find-peak-element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time Complexity : O(log n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

/*
Approach (3 sentences):
We use binary search to find a peak element by checking if the current mid is greater than its neighbors.
If the right neighbor is greater, it means we are on an increasing slope, so a peak must exist on the right side; otherwise, it lies on the left.
This works because a peak always exists, and we reduce the search space based on the slope direction.
*/

class Solution {
public int findPeakElement(int[] nums) {
int low = 0;
int high = nums.length - 1;

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

// Check if mid is a peak
if ((mid == 0 || nums[mid] > nums[mid - 1]) &&
(mid == nums.length - 1 || nums[mid] > nums[mid + 1])) {
return mid;
}

// Increasing slope → peak on right
else if (nums[mid] < nums[mid + 1]) {
low = mid + 1;
}

// Decreasing slope → peak on left
else {
high = mid - 1;
}
}
return -1;
}
}