Interactive guide to computer science

Each lesson teaches concepts through visuals and interactive widgets, helping you understand complex algorithms and concepts

ABCDEFG
Logic gatesGraphsK-mapsCoding practice

51 lessons · 4 folders

Learn step by step

A short stack of lessons for everything computer science, fully interactive and fun.

Browse all tutorials

Try them out

Tools & visualizers

9 interactive tools that each do one job, free in the browser, and every one of them embeddable.

Browse all tools

Coding problems

Try a coding problem

Implement one function in Python, JavaScript or C++, run it against the worked examples, then submit it against the hidden cases.

Browse all problems

Two Sum

Easyarraystwo pointers

Find the two numbers in a sorted array that add up to a target.

You are given an array numbers sorted in ascending order, and an integer target. Return the indices of the two values that add up to the target, smallest index first.

Constraints

  • 2 ≤ numbers.length ≤ 10,000
  • -10⁹ ≤ numbers[i] ≤ 10⁹
  • numbers is sorted in ascending order
  • assume exactly one pair works

Examples

Input
numbers = [2,7,11,15], target = 9
Output
[0,1]
Why
numbers[0] + numbers[1] = 2 + 7 = 9.
Input
numbers = [2,3,4], target = 6
Output
[0,2]
Why
2 + 4 = 6. Note that 3 + 3 is not allowed - that would reuse one element.
Input
numbers = [-3,3,4,90], target = 0
Output
[0,1]
Why
Negative numbers behave no differently.

Submitting also runs 6 hidden tests.

Open the full problem
Loading the editor

Logic problems

Try a logic problem

Pick one, wire up the inside of the chip, and run the tests in the real editor below.

Browse all problems

Writing

Read articles about CS

Longer pieces on the ideas the tutorials only have room to use.

Read the blog

Nothing published yet, check back soon.