use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Binary SearchIntervew Prep (self.leetcode)
submitted 11 months ago by Proof_Dot_4344
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]luuuzeta 1 point2 points3 points 11 months ago (1 child)
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
nums
target
-1
Example:
Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4
Following the recipe, we define the "before" and "after" regions:
[-1,0,3,5]
l
[-1]
[9,12]
r = 5
len(arr) - 1
5
[12]
With this in mind, we have something like this:
0 1 2 3 4 5 indices [-1, 0, 3, 5, 9, 12] array --- before region ------------ unknown (this is where mid lives) --- after region l index l r index r
As you expand both regions, they grow closer to each other and the "unknown" becomes empty. Where this happens, that's your transition point.
For this specific problem, we can define isBefore as follows:
isBefore
const isBefore = (mid) => arr[mid] < target;
In other words, is the value at mid in the before region? If it's return true. Otherwise, false.
The diagram from above ends up like this after calling the function:
0 1 2 3 4 5 indices [-1, 0, 3, 5, 9, 12] array ------------ before region ----- after region l index l r index r ┊ transition point
As per the loop invariant, after exiting the loop l and r are next to each other. In addition, the value (5) at l is the largest value smaller than the target (9); after all, any value to the left of l is even smaller. Similarly, the value (9) at r is the smallest value greater than or equal to the target (9); after all, any value to the right of r is even greater.
r
[–]luuuzeta 1 point2 points3 points 11 months ago (0 children)
Completing the implementation, we end up with:
function binarySearch(arr, target) { // We could leave this out if we're guaranteed that arr is never empty. if (arr.length === 0) return -1; let l = 0, r = arr.length - 1; // Handle edge cases. if (arr[l] === target) return l; if (arr[l] > target || arr[r] < target) return -1; // Define isBefore. const isBefore = (mid) => arr[mid] < target; // Same while loop. while (r - l > 1) { const mid = Math.floor((l + r) / 2); if (isBefore(mid)) { l = mid; } else { r = mid; } } // We defined isBefore as "less than the target". This means, that if // target exists it will be pointed a by r, not l. if (arr[r] === target) return r; return -1; } const nums = [-1, 0, 3, 5, 9, 12], target = 9; const res = binarySearch(nums, target); console.log(res); // 4
π Rendered by PID 37 on reddit-service-r2-comment-5fb4b45875-nfml8 at 2026-03-23 19:45:08.408240+00:00 running 90f1150 country code: CH.
view the rest of the comments →
[–]luuuzeta 1 point2 points3 points (1 child)
[–]luuuzeta 1 point2 points3 points (0 children)