Two Sum

RedAurtherRedAurther
2 min read

๐Ÿง  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.


๐Ÿท๏ธ Tags

[object Object],[object Object]


๐Ÿ“Š Difficulty

Easy

โœ… Success Rate: 48.5%
๐Ÿ“ฅ Submissions: 13,207,990
๐Ÿ“ˆ Accepted: 6,403,821


โค๏ธ Reactions

๐Ÿ‘ Likes: 31242
๐Ÿ‘Ž Dislikes: 988


๐Ÿ’ก Hints

A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions for just for completeness. It is from these brute force solutions that you can come up with optimizations. So, if we fix one of the numbers, say x, we have to scan the entire array to find the next number y which is value - x where value is the input parameter. Can we change our array somehow so that this search becomes faster? The second train of thought is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?


๐Ÿ” Similar Questions

  • 3Sum
  • 4Sum
  • Two Sum II - Input Array Is Sorted
  • Two Sum III - Data structure design
  • Subarray Sum Equals K
  • Two Sum IV - Input is a BST
  • Two Sum Less Than K
  • Max Number of K-Sum Pairs
  • Count Good Meals
  • Count Number of Pairs With Absolute Difference K
  • Number of Pairs of Strings With Concatenation Equal to Target
  • Find All K-Distant Indices in an Array

Leetcode Link : Two Sum

0
Subscribe to my newsletter

Read articles from RedAurther directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

RedAurther
RedAurther