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

Completed Binary-Search-2 #1912

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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/Binary-Search-2.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//Problem 1: (https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
//Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
//
//Your algorithm's runtime complexity must be in the order of O(log n).
//
//If the target is not found in the array, return [-1, -1].
//
//Example 1:
//
//Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] Example 2:
//
//Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1]


public class problem1 {
public int[] searchRange(int[] nums, int target) {
int[] res={-1,-1};
int low=0, high=nums.length-1;
while (low<=high) {
int mid=(low+high)/2;
if (nums[mid]==target) {
res[0]= mid;
high=mid-1;
}
else if (nums[mid]>target) {
high=mid-1;
}
else {low=mid+1;}
}
while (low<=high) {
int mid=(low+high)/2;
if (nums[mid]==target) {
res[1]= mid;
low=mid+1;
}
else if (nums[mid]>target) {
high=mid-1;
}
else {low=mid+1;}
}
return res;
}
}
33 changes: 33 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//Problem 2: (https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)
//Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
//
// (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
//
//Find the minimum element.
//
//You may assume no duplicate exists in the array.
//
// Example 1: Input: [3,4,5,1,2] Output: 1
//
//Example 2: Input: [4,5,6,7,0,1,2] Output: 0


public class problem2 {
public int findMin(int[] nums) {

int low=0, high=nums.length-1;

while (low<=high) {
int mid = (low+high)/2;
if(nums[mid]> nums[high])
{
low=mid+1;
}
else if(nums[mid]< nums[high]) {
high=mid;
}
}
return nums[low];

}
}
37 changes: 37 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//Problem 3: (https://leetcode.com/problems/find-peak-element/)
//A peak element is an element that is greater than its neighbors.
//
//Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.
//
//The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
//
//You may imagine that nums[-1] = nums[n] = -∞.
//
//Example 1:
//
//Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2:
//
//Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2,

public class problem3 {
public int findPeakElement(int[] nums) {

int low=0, high= nums.length-1;
// int res=-1;

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

}
}