Two Sum

Problem

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly** one solution**, and you may not use the same element twice.

You can return the answer in any order.

Pseudocode

Solution

Time and Space Complexity

Time

  • Loops through the array once - O(N)

  • Map operations are constant time

  • Total - O(N)

Space

  • Stores complement of each element in a Map

    • Worst case scenario is that complement is in the last position of the array

  • Total - O(N)

Last updated