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 BS-SortedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
public class Solution {

public int[] searchRange(int[] nums, int target) {
int[] result = new int[]{-1, -1};

// Find the first occurrence of the target
int left = findLeft(nums, target);

// If target is not found
if (left >= nums.length || nums[left] != target) {
return result;
}

// Find the last occurrence of the target
int right = findRight(nums, target);

result[0] = left;
result[1] = right;

return result;
}

// Function to find the leftmost (first) index of the target
private int findLeft(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}

// Function to find the rightmost (last) index of the target
private int findRight(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] <= target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return right;
}

// Example usage
public static void main(String[] args) {
Solution solution = new Solution();

int[] result1 = solution.searchRange(new int[]{5, 7, 7, 8, 8, 10}, 8);
System.out.println("Result: [" + result1[0] + ", " + result1[1] + "]"); // Output: [3, 4]

int[] result2 = solution.searchRange(new int[]{5, 7, 7, 8, 8, 10}, 6);
System.out.println("Result: [" + result2[0] + ", " + result2[1] + "]"); // Output: [-1, -1]

int[] result3 = solution.searchRange(new int[]{}, 0);
System.out.println("Result: [" + result3[0] + ", " + result3[1] + "]"); // Output: [-1, -1]
}
}
39 changes: 39 additions & 0 deletions FindMinElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public class Solution {
public int findMin(int[] nums) {
int left = 0, right = nums.length - 1;

// Binary search loop
while (left < right) {
int mid = left + (right - left) / 2; // Calculate mid index

// If middle element is greater than rightmost element, the minimum is on the right side
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid; // The minimum could be at mid itself, so we include mid
}
}

// At the end, left == right, and we return the element at that position
return nums[left];
}

public static void main(String[] args) {
Solution solution = new Solution();

// Example 1
int[] nums1 = {3, 4, 5, 1, 2};
System.out.println(solution.findMin(nums1)); // Output: 1

// Example 2
int[] nums2 = {4, 5, 6, 7, 0, 1, 2};
System.out.println(solution.findMin(nums2)); // Output: 0

// Example 3
int[] nums3 = {11, 13, 15, 17};
System.out.println(solution.findMin(nums3)); // Output: 11
}
}

//Time Complexity : O(log n)
//Space Comlexity : O(1)
29 changes: 29 additions & 0 deletions FindPeekElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class Solution {
public int findPeakElement(int[] nums) {
int left = 0, right = nums.length - 1;

while (left <= right) {
int mid = left + (right - left) / 2; // To avoid overflow

// Check if mid is a peak element
if ((mid == 0 || nums[mid] > nums[mid - 1]) &&
(mid == nums.length - 1 || nums[mid] > nums[mid + 1])) {
return mid;
}
// If the right neighbor is greater, peak is on the right half
else if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
}
// If the left neighbor is greater, peak is on the left half
else {
right = mid - 1;
}
}

return -1; // This line should never be reached because a peak will always be found.
}
}


//Time Complexity : O(log n)
//Space Complexity : O(1)