on-internet
← Community

Given a list of integers and a target number, return the indices of the two numbers that add up to the target in O(n) time complexity [closed]

Stack Overflow ·

Two Sum Problem Problem Statement Given an array of integers nums and an integer target, write a Python function two_sum(nums, target) that returns the indices of the two numbers such that they add up to target. Rules & Constraints You may assume that each input has exactly one solution. You may not use the same element twice (i.e., the two indices must be distinct). You can return the answer in any order (e.g., [0, 1] or [1, 0]). Time Complexity Requirement: Your solution must run in O(n) time complexity (avoid nested loops). 2 <= len(nums) <= 10^4 -10^9 <= nums[i] <= 10^9 -10^9 <= target <=

Comments

Sign in or sign up to comment and upvote.

No comments yet.