Skip to content

Commit

Permalink
Merge pull request #52 from sarthakw7/sarth
Browse files Browse the repository at this point in the history
Q35_Search_Insert_Position Solution added in java
  • Loading branch information
DugarRishab authored Oct 4, 2022
2 parents 5095f62 + 4723ffd commit d2f8b34
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


// We'll first apply the binary search algorithm on the given sorted array.
// If the target value is found, we'll return that index.
// If not, we'll process until our search space becomes empty. At this point, the index at which the element should be inserted is already saved while we dive our search space by half.



class Solution {
public int searchInsert(int[] nums, int target) {

int s = 0;
int e = nums.length -1;

while(s<=e){
int m = (s+e)>>1;
if(target == nums[m]){
return m;
}
if(target<nums[m]){
e = m-1;
}else{
s = m + 1;
}
}

return s;
}
}

0 comments on commit d2f8b34

Please sign in to comment.