Episode 7 — DSA with JavaScript / 7.10 — Binary Search

7.10 — Exercise Questions: Binary Search

<< Overview


E1. Basic Binary Search

Solution (JS)
function bs(a,t){let l=0,r=a.length-1;while(l<=r){const m=l+((r-l)>>1);if(a[m]===t)return m;a[m]<t?l=m+1:r=m-1;}return -1;}

E2. First and Last Position

Solution (JS)
function range(a,t){return[lower(a,t),upper(a,t)];}

E3. Search Insert Position

Solution (JS)
function sip(a,t){let l=0,r=a.length;while(l<r){const m=(l+r)>>1;a[m]<t?l=m+1:r=m;}return l;}

E4. Search Rotated Array

Solution (JS)
function sr(a,t){let l=0,r=a.length-1;while(l<=r){const m=(l+r)>>1;if(a[m]===t)return m;a[l]<=a[m]?a[l]<=t&&t<a[m]?r=m-1:l=m+1:a[m]<t&&t<=a[r]?l=m+1:r=m-1;}return -1;}

E5. Find Peak Element

Solution (JS)
function fp(a){let l=0,r=a.length-1;while(l<r){const m=(l+r)>>1;a[m]<a[m+1]?l=m+1:r=m;}return l;}

E6. Sqrt(x)

Solution (JS)
function sqrt(x){let l=0,r=x;while(l<=r){const m=(l+r)>>1;m*m===x?l=m+1:m*m<x?(l=m+1):r=m-1;}return r;}

E7. Find Minimum Rotated

Solution (JS)
function fm(a){let l=0,r=a.length-1;while(l<r){const m=(l+r)>>1;a[m]>a[r]?l=m+1:r=m;}return a[l];}

E8. Koko Eating Bananas

Solution (JS)
function koko(p,h){let l=1,r=Math.max(...p);while(l<r){const m=(l+r)>>1;p.reduce((s,x)=>s+Math.ceil(x/m),0)<=h?r=m:l=m+1;}return l;}

E9. Capacity to Ship

Solution (JS)
// Binary search on capacity, check feasibility

E10. Median Two Sorted

Solution (JS)
// Binary search on shorter array partition

E11. Count Occurrences

Solution (JS)
function count(a,t){return upper(a,t)-lower(a,t)+1;}

E12. Search 2D Matrix

Solution (JS)
function s2d(m,t){const r=m.length,c=m[0].length;let l=0,h=r*c-1;while(l<=h){const mid=(l+h)>>1,v=m[mid/c|0][mid%c];if(v===t)return true;v<t?l=mid+1:h=mid-1;}return false;}

E13. Find First Bad Version

Solution (JS)
function fbv(n){let l=1,r=n;while(l<r){const m=(l+r)>>1;isBadVersion(m)?r=m:l=m+1;}return l;}

Additional exercise bank

EX-014

  • Problem: Exercise #14.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-015

  • Problem: Exercise #15.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-016

  • Problem: Exercise #16.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-017

  • Problem: Exercise #17.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-018

  • Problem: Exercise #18.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-019

  • Problem: Exercise #19.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-020

  • Problem: Exercise #20.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-021

  • Problem: Exercise #21.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-022

  • Problem: Exercise #22.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-023

  • Problem: Exercise #23.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-024

  • Problem: Exercise #24.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-025

  • Problem: Exercise #25.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-026

  • Problem: Exercise #26.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-027

  • Problem: Exercise #27.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-028

  • Problem: Exercise #28.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-029

  • Problem: Exercise #29.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-030

  • Problem: Exercise #30.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-031

  • Problem: Exercise #31.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-032

  • Problem: Exercise #32.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-033

  • Problem: Exercise #33.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-034

  • Problem: Exercise #34.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-035

  • Problem: Exercise #35.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-036

  • Problem: Exercise #36.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-037

  • Problem: Exercise #37.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-038

  • Problem: Exercise #38.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-039

  • Problem: Exercise #39.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-040

  • Problem: Exercise #40.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-041

  • Problem: Exercise #41.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-042

  • Problem: Exercise #42.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-043

  • Problem: Exercise #43.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-044

  • Problem: Exercise #44.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-045

  • Problem: Exercise #45.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-046

  • Problem: Exercise #46.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-047

  • Problem: Exercise #47.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-048

  • Problem: Exercise #48.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-049

  • Problem: Exercise #49.
  • Difficulty: Easy
  • Implementation: Both JS and C++.

EX-050

  • Problem: Exercise #50.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-051

  • Problem: Exercise #51.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-052

  • Problem: Exercise #52.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-053

  • Problem: Exercise #53.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-054

  • Problem: Exercise #54.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-055

  • Problem: Exercise #55.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-056

  • Problem: Exercise #56.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-057

  • Problem: Exercise #57.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-058

  • Problem: Exercise #58.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-059

  • Problem: Exercise #59.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-060

  • Problem: Exercise #60.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-061

  • Problem: Exercise #61.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-062

  • Problem: Exercise #62.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-063

  • Problem: Exercise #63.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-064

  • Problem: Exercise #64.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-065

  • Problem: Exercise #65.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-066

  • Problem: Exercise #66.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-067

  • Problem: Exercise #67.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-068

  • Problem: Exercise #68.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-069

  • Problem: Exercise #69.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-070

  • Problem: Exercise #70.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-071

  • Problem: Exercise #71.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-072

  • Problem: Exercise #72.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-073

  • Problem: Exercise #73.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-074

  • Problem: Exercise #74.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-075

  • Problem: Exercise #75.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-076

  • Problem: Exercise #76.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-077

  • Problem: Exercise #77.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-078

  • Problem: Exercise #78.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-079

  • Problem: Exercise #79.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-080

  • Problem: Exercise #80.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-081

  • Problem: Exercise #81.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-082

  • Problem: Exercise #82.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-083

  • Problem: Exercise #83.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-084

  • Problem: Exercise #84.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-085

  • Problem: Exercise #85.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-086

  • Problem: Exercise #86.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-087

  • Problem: Exercise #87.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-088

  • Problem: Exercise #88.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-089

  • Problem: Exercise #89.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-090

  • Problem: Exercise #90.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-091

  • Problem: Exercise #91.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-092

  • Problem: Exercise #92.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-093

  • Problem: Exercise #93.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-094

  • Problem: Exercise #94.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-095

  • Problem: Exercise #95.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-096

  • Problem: Exercise #96.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-097

  • Problem: Exercise #97.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-098

  • Problem: Exercise #98.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-099

  • Problem: Exercise #99.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-100

  • Problem: Exercise #100.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-101

  • Problem: Exercise #101.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-102

  • Problem: Exercise #102.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-103

  • Problem: Exercise #103.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-104

  • Problem: Exercise #104.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-105

  • Problem: Exercise #105.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-106

  • Problem: Exercise #106.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-107

  • Problem: Exercise #107.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-108

  • Problem: Exercise #108.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-109

  • Problem: Exercise #109.
  • Difficulty: Medium
  • Implementation: Both JS and C++.

EX-110

  • Problem: Exercise #110.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-111

  • Problem: Exercise #111.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-112

  • Problem: Exercise #112.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-113

  • Problem: Exercise #113.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-114

  • Problem: Exercise #114.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-115

  • Problem: Exercise #115.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-116

  • Problem: Exercise #116.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-117

  • Problem: Exercise #117.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-118

  • Problem: Exercise #118.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-119

  • Problem: Exercise #119.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-120

  • Problem: Exercise #120.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-121

  • Problem: Exercise #121.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-122

  • Problem: Exercise #122.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-123

  • Problem: Exercise #123.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-124

  • Problem: Exercise #124.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-125

  • Problem: Exercise #125.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-126

  • Problem: Exercise #126.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-127

  • Problem: Exercise #127.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-128

  • Problem: Exercise #128.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-129

  • Problem: Exercise #129.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-130

  • Problem: Exercise #130.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-131

  • Problem: Exercise #131.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-132

  • Problem: Exercise #132.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-133

  • Problem: Exercise #133.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-134

  • Problem: Exercise #134.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-135

  • Problem: Exercise #135.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-136

  • Problem: Exercise #136.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-137

  • Problem: Exercise #137.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-138

  • Problem: Exercise #138.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-139

  • Problem: Exercise #139.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-140

  • Problem: Exercise #140.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-141

  • Problem: Exercise #141.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-142

  • Problem: Exercise #142.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-143

  • Problem: Exercise #143.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-144

  • Problem: Exercise #144.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-145

  • Problem: Exercise #145.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-146

  • Problem: Exercise #146.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-147

  • Problem: Exercise #147.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-148

  • Problem: Exercise #148.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-149

  • Problem: Exercise #149.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-150

  • Problem: Exercise #150.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-151

  • Problem: Exercise #151.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-152

  • Problem: Exercise #152.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-153

  • Problem: Exercise #153.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-154

  • Problem: Exercise #154.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-155

  • Problem: Exercise #155.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-156

  • Problem: Exercise #156.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-157

  • Problem: Exercise #157.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-158

  • Problem: Exercise #158.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-159

  • Problem: Exercise #159.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-160

  • Problem: Exercise #160.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-161

  • Problem: Exercise #161.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-162

  • Problem: Exercise #162.
  • Difficulty: Hard
  • Implementation: Both JS and C++.

EX-163

  • Problem: Exercise #163.
  • Difficulty: Hard
  • Implementation: Both JS and C++.