Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed Problem1.cpp
Empty file.
Empty file removed Problem1.java
Empty file.
Empty file removed Problem2.cpp
Empty file.
Empty file removed Problem2.java
Empty file.
28 changes: 28 additions & 0 deletions TwoSum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//Time Complexity - O(n)
//Space Complexity - O(n)

// Iterate over an array calculate sum= element - target, check if dictionary contains sum , if not then add element and its index in dictionary.

// If the sum is in the dictionary then return index stored at the sum and current index.

public class Solution
{
public int[] TwoSum(int[] nums, int target)
{
//Dictionaries O(n)
var pairs = new Dictionary<int, int>();
int sum = 0;
for (int i = 0; i < nums.Length; i++)
{
sum = target - nums[i];
if (pairs.ContainsKey(sum))
return new int[] { pairs[sum], i };
else
pairs.TryAdd(nums[i], i);
}
return default;
}
}