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

Search insert position problem #1

Merged
merged 1 commit into from
Oct 30, 2022
Merged
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
Search insert position
  • Loading branch information
Raj04 committed Oct 30, 2022
commit 368ab52765bcd2f1f184fcc99df5472c51d865cc
21 changes: 21 additions & 0 deletions DSA/code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//Problem link: https://leetcode.com/problems/search-insert-position/
class Solution {
public int searchInsert(int[] nums, int target) {

//try to think of mountain array concept
//where mid-1<target && mid>target so mid is the index to be inserted
int low=0,high=nums.length-1, mid=(low+high)/2;
while(low<=high){
mid=(low+high)/2;

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