Leetcode two sum solution python

LeetCode #1 — Two Sum

Hello happy people πŸ‘‹! Today we are going to discuss the very first problem on the LeetCode.

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.

So, we have to find two numbers among all the numbers in an array such that when they are added, they give us a third number equals to target .

If nums = [2,7,11,15] and target = 9, then we should return 2 and 7 because 2 + 7 = 9

In simpler words, we need to find —

c = a + b where, c => target a and b => elements from the given array

Also, the given constraints suggest that there is only ONE valid answer along with the ranges of the elements in the array and the target.

After we have analyzed our problem, now it’s time to think about the solution.

The first solution that comes to mind is —

  • Take one element
  • Add this element with every other element
  • After adding, compare the sum with the given target
  • If the sum is equal to the target, return the indices of these two elements
  • If the sum is not equal to the target, we check for the next pair

Pretty simple, eh πŸ˜‰? Let’s analyze the time and space complexities.

Let’s say the array has n elements then —

For 1st element - we will check for (n - 1) elements For 2nd element - we will check for (n - 2) elements For 3rd element - we will check for (n - 3) elements and so on. Thus, the total iterations would be - [(n - 1) + (n - 2) + (n - 3) + . + 2 + 1]

If we simplify the above expression we will get —

Thus, our time complexity would be — O(n 2 ).

Since we are not using any extra data structure hence our space complexity would be O(1).

This approach is simple and intuitive but it takes quadratic time which is bad for large inputs. Let’s see if we have any efficient approach to solve this problem. As a matter of fact, we do πŸ˜„!

In the earlier approach, we did what naturally came to us. But let’s try to really think through this problem. Hmm… πŸ€”

When we were analyzing the problem, we came across the following equation

but can we write the above equation as —

Of course we can. We are mathematicians after all 😁.

The benefit of writing this equation is that now we can iterate through the array only once and calculate the difference of target (c) and the current element (a) and find the other element (b) . The idea is as follows —

  • Loop through the array
  • Check if we have encountered the current element before
  • If we have, we will return the index of this element and the index of the difference of the target and the current element . We will only encounter this element before only if we have saved the difference of target and the element which when added to the current element gives the target itself. This is confusing I know but once it clicks, it’s very obvious.
  • If we haven’t, the we will save the difference of the target and current element .
  • Repeat the process

Since we need to store our difference , we need a data structure which can store the difference and its corresponding index. So what do you think, which data structure can help us? Of course, we need a Map where we will store the difference as key and the corresponding index as value .

Since we are iterating the array only once, the time complexity would be O(n).

Since we need a Map of the size of the array, the space complexity would be O(n).

Now we have an approach to solve this problem, let’s write some code —

package org.redquark.tutorials.leetcode; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class TwoSum  public int[] twoSum(int[] nums, int target)  // Array to store result int[] result = new int[2]; // This map will store the difference and the corresponding index MapInteger, Integer> map = new HashMap>(); // Loop through the entire array for (int i = 0; i  nums.length; i++)  // If we have seen the current element before // It means we have already encountered the other number of the pair if (map.containsKey(nums[i]))  // Index of the current element result[0] = i; // Index of the other element of the pair result[1] = map.get(nums[i]); > // If we have not seen the current before // It means we have not yet encountered any number of the pair else  // Save the difference of the target and the current element // with the index of the current element map.put(target - nums[i], i); > > return result; > >
from typing import List def twoSum(nums: List[int], target: int) -> List[int]: # List to store results result = [] # Dictionary to store the difference and its index index_map = > # Loop for each element for i, n in enumerate(nums): # Difference which needs to be checked difference = target - n if difference in index_map: result.append(i) result.append(index_map[difference]) break else: index_map[n] = i return result
var twoSum = function (nums, target)  // Array to store the result result = []; // Map to store the difference and its index index_map = new Map(); // Loop for each element in the array for (let i = 0; i  nums.length; i++)  let difference = target - nums[i]; if (index_map.has(difference))  result[0] = i; result[1] = index_map.get(difference); break; > else  index_map.set(nums[i], i); > > return result; >;
package org.redquark.tutorials.leetcode fun twoSum(nums: IntArray, target: Int): IntArray  // Array to store result val result = IntArray(2) // This map will store the difference and the corresponding index val map: MutableMapInt, Int> = HashMap() // Loop through the entire array for (i in nums.indices)  // If we have seen the current element before // It means we have already encountered the other number of the pair if (map.containsKey(nums[i]))  // Index of the current element result[0] = i // Index of the other element of the pair result[1] = map[nums[i]]!! break > else  // Save the difference of the target and the current element // with the index of the current element map[target - nums[i]] = i > > return result >

I hope you liked this post. Here, we solved the very first problem on LeetCode in O(n) time and O(n) space.

You can find the complete source code on GitHub. If you find it useful, consider giving it a star ⭐.

  • Java
  • Python
  • JavaScript
  • Kotlin Feel free to share your thoughts about this post in comments’ section. I’d love to hear your feedback.

Happy coding πŸ˜„ and Namaste πŸ™!

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

LeetCode Problem 1 Solution in Python

Discussing the approach for an optimal solution to Two Sum problem in LeetCode

Introduction

LeetCode is a platform that gives access to thousands of programming problems and helps users enhance their skills and get prepared for technical interviews that are usually part of the recruitment process for Engineering and ML positions.

In today’s short guide we will explore the first problem called Two Sum and attempt to solve it in an optimal way. In technical interviews, it’s not only important to derive a solution for a particular problem but the time complexity is also something you will usually be questioned about.

The 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.

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Input: nums = [3,2,4], target = 6
Output: [1,2]
Input: nums = [3,3], target = 6
Output: [0,1]

Constraints:

A not so..optimal solution

The most simple approach would require two nested loops where the outer loop iterates over all the elements of the list and the inner loop iterates from the current index of the outer loop up to the end of the list.

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

Π Π°Π·Π±ΠΎΡ€ Π·Π°Π΄Π°Ρ‡ΠΈ 1 Two Sum LeetCode.com

Учитывая массив Ρ†Π΅Π»Ρ‹Ρ… чисСл nums ΠΈ Ρ†Π΅Π»ΠΎΠ΅ число target , Π²Π΅Ρ€Π½ΠΈΡ‚Π΅ индСксы Π΄Π²ΡƒΡ… чисСл Ρ‚Π°ΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΠ½ΠΈ составляли Π² суммС target .

Π’Ρ‹ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ ΠΏΡ€Π΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚ΡŒ, Ρ‡Ρ‚ΠΎ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ Π²Ρ…ΠΎΠ΄ Π±ΡƒΠ΄Π΅Ρ‚ ΠΈΠΌΠ΅Ρ‚ΡŒ Ρ€ΠΎΠ²Π½ΠΎ ΠΎΠ΄Π½ΠΎ Ρ€Π΅ΡˆΠ΅Π½ΠΈΠ΅ , ΠΈ Π²Ρ‹ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΠΎΠ΄ΠΈΠ½ ΠΈ Ρ‚ΠΎΡ‚ ΠΆΠ΅ элСмСнт Π΄Π²Π°ΠΆΠ΄Ρ‹.

Π’Ρ‹ ΠΌΠΎΠΆΠ΅Ρ‚Π΅ Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ ΠΎΡ‚Π²Π΅Ρ‚ Π² любом порядкС.

ΠžΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΡ:
2 -10 9 -10 9 БущСствуСт Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΎΠ΄ΠΈΠ½ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹ΠΉ ΠΎΡ‚Π²Π΅Ρ‚.

Для Ρ€Π΅ΡˆΠ΅Π½ΠΈΡ этой Π·Π°Π΄Π°Ρ‡ΠΈ ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Ρ…Ρ€Π°Π½ΠΈΡ‚ΡŒ ΡƒΠΆΠ΅ просмотрСнныС числа. ΠœΡ‹ ΠΏΡ€ΠΎΡ…ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ массиву чисСл ΠΈ для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ числа провСряСм, сущСствуСт Π»ΠΈ число, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅, Π² суммС с Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΌ числом, даст Π½Π°ΠΌ Ρ†Π΅Π»Π΅Π²ΠΎΠ΅ число. Если Ρ‚Π°ΠΊΠΎΠ΅ число сущСствуСт, ΠΌΡ‹ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ индСксы Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ числа ΠΈ числа ΠΈΠ· Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹. Если Ρ‚Π°ΠΊΠΎΠ΅ число Π½Π΅ сущСствуСт, ΠΌΡ‹ добавляСм Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ число ΠΈ Π΅Π³ΠΎ индСкс Π² Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ ΠΈ ΠΏΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠ°Π΅ΠΌ ΠΈΡ‚Π΅Ρ€Π°Ρ†ΠΈΡŽ ΠΏΠΎ массиву.

Π­Ρ‚ΠΎΡ‚ Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌ ΠΈΠΌΠ΅Π΅Ρ‚ Π²Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ ΡΠ»ΠΎΠΆΠ½ΠΎΡΡ‚ΡŒ O(n), Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ ΠΌΡ‹ ΠΏΡ€ΠΎΡ…ΠΎΠ΄ΠΈΠΌ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΎΠ΄ΠΈΠ½ Ρ€Π°Π· ΠΏΠΎ массиву чисСл, ΠΈ для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ числа выполняСм константноС количСство ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ (ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ°, сущСствуСт Π»ΠΈ число Π² Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Π΅, Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ числа ΠΈ Π΅Π³ΠΎ индСкса Π² Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ). Π­Ρ‚ΠΎ Π·Π½Π°Ρ‡ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ быстрСС, Ρ‡Π΅ΠΌ O(n2), которая Π±Ρ‹Π»Π° Π±Ρ‹ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠ°, Ссли Π±Ρ‹ ΠΌΡ‹ провСряли всС Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Π΅ ΠΏΠ°Ρ€Ρ‹ чисСл Π² массивС.

РСшСниС Two Sum LeetCode ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΊΠΎΠ΄Π° Π½Π° Python

class Solution: # ΠΌΠ΅Ρ‚ΠΎΠ΄ twoSum ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ Π½Π° Π²Ρ…ΠΎΠ΄ список nums ΠΈ Ρ†Π΅Π»Π΅Π²ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ target # ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ список ΠΈΠ· Π΄Π²ΡƒΡ… индСксов чисСл Π² nums, сумма ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Ρ€Π°Π²Π½Π° target def twoSum(self, nums: List[int], target: int) -> List[int]: # ΡΠ»ΠΎΠ²Π°Ρ€ΡŒ для хранСния ΡƒΠΆΠ΅ просмотрСнных чисСл seen = <> # проходимся ΠΏΠΎ всСм числам Π² спискС nums с индСксами for i, num in enumerate(nums): # вычисляСм "ΠΊΠΎΠΌΠΏΠ»Π΅ΠΌΠ΅Π½Ρ‚" - Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π±Ρ‹Ρ‚ΡŒ Π² спискС, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ target complement = target - num # Ссли ΠΊΠΎΠΌΠΏΠ»Π΅ΠΌΠ΅Π½Ρ‚ ΡƒΠΆΠ΅ Π±Ρ‹Π» просмотрСн, Ρ‚ΠΎ нашли Π½ΡƒΠΆΠ½Ρ‹Π΅ индСксы чисСл if complement in seen: return [seen[complement], i] # сохраняСм Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ число ΠΈ Π΅Π³ΠΎ индСкс Π² ΡΠ»ΠΎΠ²Π°Ρ€ΡŒ seen seen[num] = i # Ссли Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ Π½ΡƒΠΆΠ½Ρ‹Ρ… индСксов, Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ None return None

РСшСниС Two Sum LeetCode ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΊΠΎΠ΄Π° Π½Π° Java

class Solution < public int[] twoSum(int[] nums, int target) < // созданиС Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ для хранСния чисСл ΠΈ ΠΈΡ… индСксов Mapmap = new HashMap<>(); // ΠΏΠ΅Ρ€Π΅Π±ΠΎΡ€ массива чисСл for (int i = 0; i < nums.length; i++) < // Π½Π°Ρ…ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ разности ΠΌΠ΅ΠΆΠ΄Ρƒ Ρ†Π΅Π»Π΅Π²Ρ‹ΠΌ числом ΠΈ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΌ числом int complement = target - nums[i]; // Ссли такая Ρ€Π°Π·Π½ΠΎΡΡ‚ΡŒ ΡƒΠΆΠ΅ Π΅ΡΡ‚ΡŒ Π² Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Π΅, Π·Π½Π°Ρ‡ΠΈΡ‚ ΠΌΡ‹ нашли ΠΏΠ°Ρ€Ρƒ чисСл if (map.containsKey(complement)) < // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ индСксы Π΄Π²ΡƒΡ… чисСл return new int[] < map.get(complement), i >; > // добавляСм Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ число ΠΈ Π΅Π³ΠΎ индСкс Π² Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ map.put(nums[i], i); > // Ссли Π½Π΅ нашли ΠΏΠ°Ρ€Ρƒ чисСл, выбрасываСм ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ throw new IllegalArgumentException("No two sum solution"); > >

РСшСниС Two Sum LeetCode ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΊΠΎΠ΄Π° Π½Π° C++

class Solution < public: vectortwoSum(vector& nums, int target) < // созданиС unordered_map для хранСния чисСл ΠΈ ΠΈΡ… индСксов unordered_mapmp; for(int i = 0; i < nums.size(); i++) < // Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ число-Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ int complement = target - nums[i]; // Ссли Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ Π² unordered_map if(mp.find(complement) != mp.end()) < // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ Π΅Π³ΠΎ индСкс ΠΈ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ индСкс return < mp[complement], i >; > // добавляСм Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ число ΠΈ Π΅Π³ΠΎ индСкс Π² unordered_map mp[nums[i]] = i; > // Ссли Π½Π΅Ρ‚ Ρ€Π΅ΡˆΠ΅Π½ΠΈΡ, выбрасываСм ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ throw invalid_argument("No two sum solution"); > >;

РСшСниС Two Sum LeetCode ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΊΠΎΠ΄Π° Π½Π° JavaScript

/** * Ѐункция twoSum ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ Π½Π° Π²Ρ…ΠΎΠ΄ массив чисСл nums ΠΈ Ρ†Π΅Π»Π΅Π²ΠΎΠ΅ число target. * Она Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ массив с двумя индСксами элСмСнтов, сумма ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Ρ€Π°Π²Π½Π° Ρ†Π΅Π»Π΅Π²ΠΎΠΌΡƒ числу. * @param nums - массив чисСл * @param target - Ρ†Π΅Π»Π΅Π²ΠΎΠ΅ число * @returns - массив с двумя индСксами элСмСнтов * @throws - Ссли Ρ€Π΅ΡˆΠ΅Π½ΠΈΠ΅ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ */ const twoSum = function(nums, target) < // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ для хранСния элСмСнтов ΠΈ ΠΈΡ… индСксов const map = new Map(); // Π˜Ρ‚Π΅Ρ€ΠΈΡ€ΡƒΠ΅ΠΌΡΡ ΠΏΠΎ массиву nums for (let i = 0; i < nums.length; i++) < // ВычисляСм Ρ€Π°Π·Π½ΠΎΡΡ‚ΡŒ Ρ†Π΅Π»Π΅Π²ΠΎΠ³ΠΎ числа ΠΈ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ элСмСнта const complement = target - nums[i]; // Если элСмСнт-Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ слагаСмоС ΡƒΠΆΠ΅ находится Π² Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Π΅, // Π·Π½Π°Ρ‡ΠΈΡ‚, ΠΌΡ‹ нашли ΠΏΠ°Ρ€Ρƒ, сумма ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Ρ€Π°Π²Π½Π° Ρ†Π΅Π»Π΅Π²ΠΎΠΌΡƒ числу if (map.has(complement)) < // Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ массив с индСксами элСмСнтов return [map.get(complement), i]; >// Π˜Π½Π°Ρ‡Π΅, добавляСм Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ элСмСнт ΠΈ Π΅Π³ΠΎ индСкс Π² Ρ…ΡΡˆ-Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ map.set(nums[i], i); > // Если Π½Π΅ Π±Ρ‹Π»ΠΎ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ ΠΏΠ°Ρ€Ρ‹ элСмСнтов, сумма ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Ρ€Π°Π²Π½Π° Ρ†Π΅Π»Π΅Π²ΠΎΠΌΡƒ числу, // выбрасываСм ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ throw new Error('No two sum solution'); >;

РСшСниС Two Sum LeetCode ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΊΠΎΠ΄Π° Π½Π° c#

public class Solution < public int[] TwoSum(int[] nums, int target) < Dictionarymap = new Dictionary(); // созданиС словаря for (int i = 0; i < nums.Length; i++) < // Ρ†ΠΈΠΊΠ» ΠΏΠΎ массиву int complement = target - nums[i]; // вычислСниС разности ΠΌΠ΅ΠΆΠ΄Ρƒ Ρ†Π΅Π»Π΅Π²Ρ‹ΠΌ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ΠΌ ΠΈ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΌ элСмСнтом массива if (map.ContainsKey(complement)) < // ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° наличия элСмСнта-дополнСния Π² словарС return new int[] < map[complement], i >; // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π΅Π½ΠΈΠ΅ ΠΏΠ°Ρ€Ρ‹ индСксов, Ссли элСмСнт-Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ Π½Π°ΠΉΠ΄Π΅Π½ > map[nums[i]] = i; // Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ элСмСнта Π² ΡΠ»ΠΎΠ²Π°Ρ€ΡŒ > throw new ArgumentException("No two sum solution"); // выброс ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ, Ссли Ρ€Π΅ΡˆΠ΅Π½ΠΈΠ΅ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ > >

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

Π§ΠΈΡ‚Π°ΠΉΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅:  Html button block or inline
ΠžΡ†Π΅Π½ΠΈΡ‚Π΅ ΡΡ‚Π°Ρ‚ΡŒΡŽ