Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Pair with target sum

    Pair with target sum

    Easyarrayhash map

    Given an array nums and a number target, exactly one pair of elements adds up to target. Return the indexes of the two, smaller index first.

    You may not use the same element twice.

    Example 1

    Input:  nums = [2,7,11,15], target = 9
    Output: [0,1]

    2 + 7 = 9.

    Example 2

    Input:  nums = [3,2,4], target = 6
    Output: [1,2]

    Example 3

    Input:  nums = [3,3], target = 6
    Output: [0,1]

    Constraints

    • 2 ≤ nums.length ≤ 10⁴
    • -10⁹ ≤ nums[i] ≤ 10⁹
    • Exactly one answer exists

    Submit checks your solution against 10 tests, including hidden ones.

    /**
     * @param {int[]} nums
     * @param {int} target
     * @return {int[]}
     */
    function twoSum(nums, target) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.