From 11e1bf1fd4277e83a1e8c04b3b83e7fcf29d5bb4 Mon Sep 17 00:00:00 2001 From: sneha-tambade Date: Tue, 13 Jan 2026 18:36:28 -0800 Subject: [PATCH] Competitive-coding-2 problems solved --- Problem1.cpp | 0 Problem1.java | 0 Problem2.cpp | 0 Problem2.java | 0 TwoSum.cs | 28 ++++++++++++++++++++++++++++ 5 files changed, 28 insertions(+) delete mode 100644 Problem1.cpp delete mode 100644 Problem1.java delete mode 100644 Problem2.cpp delete mode 100644 Problem2.java create mode 100644 TwoSum.cs diff --git a/Problem1.cpp b/Problem1.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/Problem1.java b/Problem1.java deleted file mode 100644 index e69de29b..00000000 diff --git a/Problem2.cpp b/Problem2.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/Problem2.java b/Problem2.java deleted file mode 100644 index e69de29b..00000000 diff --git a/TwoSum.cs b/TwoSum.cs new file mode 100644 index 00000000..e89789f0 --- /dev/null +++ b/TwoSum.cs @@ -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 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; + } +} + + +