Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done BS-2 #2030

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 P1_Better.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Solution {

private int lowerBound(int[] nums, int target) {
int low = 0, high = nums.length - 1;
int ans = nums.length;

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

if(nums[mid] >= target) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
return ans;
}

private int upperBound(int[] nums, int target) {
int low = 0, high = nums.length - 1;
int ans = nums.length;

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

if(nums[mid] > target) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
return ans;
}

public int[] searchRange(int[] nums, int target) {
int firstOcc = lowerBound(nums, target);

// Check if the target is present in the array or not
if(firstOcc == nums.length || nums[firstOcc] != target) return new int[]{-1, -1};

// Function call to find the last occurrence (upper bound)
int lastOcc = upperBound(nums, target) - 1;

return new int[]{firstOcc, lastOcc};
}
}

// Time Complexity : 2*O(logn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : None

/* Your code here along with comments explaining your approach in three sentences only -

We use lower bound to find the first occurence of the element and the upper bound - 1 to get the last occurence of the element

*/
27 changes: 27 additions & 0 deletions P1_Brute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public int[] searchRange(int[] nums, int target) {
int first = -1, last = -1;
int[] ans = new int[2];
int n = nums.length;

for (int i = 0; i < n; i ++) {
if (nums[i] == target) {
if (first == -1) first = i;
last = i;
}
}

ans[0] = first;
ans[1] = last;
return ans;
}
}

// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : None

/* Your code here along with comments explaining your approach in three sentences only - Will initise first and last variable to -1 and traverse the array to check if we have our target.
Once the target is met we store its index in both first and last variable and keep searching and updating the last variable with indexes.
*/
82 changes: 82 additions & 0 deletions P1_Optimal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class Solution {
// Function to find the first occurrence of the target
private int firstOccurrence(int[] nums, int target) {
int low = 0, high = nums.length - 1;
int first = -1;

// Applying Binary Search Algorithm
while(low <= high) {
int mid = low + (high - low) / 2;

/* If the target element is found, we
update the first variable to mid and
eliminate the right half to look for
more smaller index where target is present */
if(nums[mid] == target) {
first = mid;
high = mid - 1;
}

/* If middle element is smaller,
we eliminate the left half */
else if(nums[mid] < target) {
low = mid + 1;
}

/* If middle element is greater,
we eliminate the right half */
else {
high = mid - 1;
}
}
return first;
}

// Function to find the last occurrence of the target
private int lastOccurrence(int[] nums, int target) {
int low = 0, high = nums.length - 1;
int last = -1;

// Applying Binary Search Algorithm
while(low <= high) {
int mid = low + (high - low) / 2;

/* If the target element is found, we
update the first variable to mid and
eliminate the right half to look for
more greater index where target is present */
if(nums[mid] == target) {
last = mid;
low = mid + 1;
}

/* If middle element is smaller,
we eliminate the left half */
else if(nums[mid] < target) {
low = mid + 1;
}

/* If middle element is greater,
we eliminate the right half */
else {
high = mid - 1;
}
}
return last;
}

// Function to find the first and last occurrences of the target
public int[] searchRange(int[] nums, int target) {

// Function call to find the first occurence of target
int first = firstOccurrence(nums, target);

// If the target is not present in the array
if(first == -1) return new int[]{-1, -1};

// Function call to find the last occurence of target
int last = lastOccurrence(nums, target);

return new int[]{first, last};
}
}
21 changes: 21 additions & 0 deletions P2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int findMin(int[] nums) {
int n = nums.length;
int low = 0;
int high = n - 1;

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

// If the middle element is greater than the last element,
// the minimum is in the right part
if (nums[mid] > nums[high]) {
low = mid + 1;
} else {
// Otherwise, the minimum is in the left part (including mid)
high = mid;
}
}
return nums[low]; // At the end of the loop, `low == high`, pointing to the minimum
}
}
20 changes: 20 additions & 0 deletions P3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int low = 0;
int high = n - 1;

while (low <= high) {
int mid = low + (high - low)/2;
if ((mid == 0 || nums[mid] > nums[mid - 1])
&& (mid == n - 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 1;
}
}