Two Sum Leetcode Solution

Two Sum Leetcode 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.return [0, 1]..
Two Sum Leetcode Solution

Two Sum Leetcode Solution :

Constraints :

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <=10^9
  • -10^9 <= target <= 10^9
  • Only one valid answer exists.

Example 1:

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

Output: [1,2]

Example 2:

Input: nums = [3,3], target = 6

Output: [0,1]

In the “Two Sum” problem, you are given an array of integers nums and an integer target. The goal is to find two numbers in the array such that their sum equals the target. Here are the key points for the given constraints and examples:

  • Constraints:

    • The length of the nums array is between 2 and 10,000.
    • The values in the nums array can range from -1,000,000,000 to 1,000,000,000.
    • The target value can also range from -1,000,000,000 to 1,000,000,000.
    • There is exactly one valid solution.

Approach :

For Solving two sum Leetcode Problem we can use following procedure :

  • A hashmap which contain the element of the array along with its indices.
  • Then we can traverse the array such that target – particular element will find the hashmap which return the indices respectively.
Two sum leetcode

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription