DEV Community

He Codes IT
He Codes IT

Posted on

Two Sum LeetCode C#: Coding Interview Solutions for Success

Welcome to our comprehensive guide on tackling the popular coding problem, "Two Sum," in the context of LeetCode using the C# programming language. Whether you are a coding enthusiast, a student preparing for technical interviews, or a seasoned developer looking to enhance your problem-solving skills, this resource is tailored to meet your needs. Within this guide, we provide you with expert insights, efficient algorithms, and optimal C# solutions to conquer the Two Sum problem on LeetCode. Prepare to elevate your coding proficiency and ace those challenging coding interviews with confidence. The Two Sum LeetCode Problem's link is Here.


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]
Explanation: 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]
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.

Solution 1 (Brute Force)

It is the easiest Solution Someone can think of:
Steps are as follow:
Firstly Create an Array of size 2 to store the Return Result of Solution.
Run Outer Loop For example i from 0 to length of array-1;
Run inner Loop For example j from i+1 to length of array.
if number at index i + number at index j equals the target return i and j.


The time Complexity of this algorithm is O(n²) and the space complexity is 0(1)

Solution 2 (Hashmap Two Loops)

In this solution we are going to use a Data Structure Hashmap.
Steps for this solution are:
Create a Hashmap.
Run a loop in HashMap to from 0 to length of array storing all the values of array in Hashmap.
Run another loop from 0 to length of array where you are going to find if compliment (target - number at index i) exists in the hashmap or not.
if the compliment exists return the current index and the compliment.

Two Sum LeetCode C#:
The time complexity of this algorithm is O(n) and the Space complexity is also O(n) because the number of items stored in hashmap = n.

Solution 3 (HashMap Single Loop)

This is the Best solution for the Two Sum LeetCode Problem I have found in C# so far.
Create a Hashmap.
Run only a single Loop From i =0 to Length of Array.
If Compliment is found in HashMap return the Index of HashMap along with Array current index.
If not found put the current Index into the HashMap.

The Time Complexity of this Algorithms is still O(n) and its space complexity at worst case still O(n). The space complexity could be reduces if the two elments which we are finding are in start of the array.

Longest Palindromic Substring Solution - Python LeetCode
Longest Valid Parentheses - Leetcode Python
Divide Two integers Leetcode Python

4sum Problem Leetcode - Python Solutions
Find Median Of Two Sorted Arrays - LeetCode Solutions
String to Integer (atoi) - LeetCode Solutions

Top comments (0)