Skip to main content

1. Two Sum

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.

 

Example 1:

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

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

 

Constraints:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • Only one valid answer exists.
here was my Engrish explanation of how i solved it

its for each number in the array we index it then we start with the first value (firstvalue_x) then subtract that from the target value(target_val), if the result (secondvalue_y) is in the index and isnt the index number(firstvalue_x) we subtracted from the target value(target_val) that is the answer(answer) otherwise continue with this loop by increasing the (firstvalue_x). we dont have to look at values we have tested already


public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        Dictionary<int, int> dict = new Dictionary<int, int>();
 // We create an index (dictionary) to store each number and its position in the array.
        for (int n = 0; n < nums.Length; n++) { 
// We start a loop to go through each number in the array. The variable 'i' is the position of the current number (firstvalue_x).
            int complement = target - nums[n]; 
// We subtract the current number (firstvalue_x) from the target value (target_val), the result is the complement (secondvalue_y).
            if (dict.ContainsKey(complement) && dict[complement] != n) { 
// We check if the complement (secondvalue_y) is in the index (dictionary) and its position is not the same as the current position (firstvalue_x).
                return new int[] { dict[complement], n };
 // If the above condition is true, we've found the answer (answer). We return the positions of the two numbers that add up to the target.
            }
            dict[nums[n]] = n;
// If the complement is not in the index, we add the current number and its position to the index (dictionary) and continue with the loop by increasing the current position (firstvalue_x).
        }
                  throw new Exception("There's a problem with your code or the input array is bad"); 
// If we've gone through all the numbers and haven't found two numbers that add up to the target, we throw an exception.
// we need this to allow compiler to compile because the compiler requires that all code paths in a function must return a value if the function is declared to return a value
// we *have* to have this or it just fails at compiling
    }
}

cases for question

case 1 [2,7,11,15] target = 9
case 2 [3,2,4] target = 6
case 3 [3,3] target = 6

The code works for these test questions
Follow-up: Can you come up with an algorithm that is less than O(n2time complexity?
i asked chat gpt coz idk if it is off the top of my head coz i cant think lol

"Yes, the provided solution fits the follow-up question. The time complexity of this solution is O(n), which is less than O(n^2).

Here's why:

The solution iterates over the nums array only once. For each number, it performs operations that take constant time: calculating the complement, checking if the complement is in the dictionary, and adding a number to the dictionary. Therefore, the time complexity is proportional to the size of the nums array, which is O(n)."

what does leetcode say to my answer?
well it likes it :D i guess its about as fast as it can be, especially if you remove the comments

image.png