Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Two pointers

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Two pointers

Two Sum II — Input Array Is Sorted

beginnerlayer B4 · Two pointers5 tests

You are given numbers, an array of integers sorted in non-decreasing order, and a target. Find the two numbers that add up to the target and return their positions as a two-element array.

  • The returned positions are 1-indexed: the first element is at position 1, not 0
  • Return them in increasing order, so [smaller, larger]
  • You may not use the same element twice
  • If no such pair exists, return an empty array
  • Aim for O(1) extra space — no hash map

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen