This repository contains a complete solution to the Two Sum problem from LeetCode, implemented in C programming language with detailed documentation, explanations, and test cases. This is a beginner-friendly resource for learning problem-solving and coding interviews.
Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to target.
- You may assume that each input would have exactly one solution
- You may not use the same element twice
- You can return the answer in any order
2 <= nums.length <= 10⁴-10⁹ <= nums[i] <= 10⁹-10⁹ <= target <= 10⁹- Only one valid answer exists
Author's Note: This solution is implemented in C language because it is the primary language I'm currently learning and practicing. I'm proficient in C, while Python and Java are still being learned. This project serves as a practical application of my C programming knowledge for solving real LeetCode problems.
File: twoSum.c
The solution uses a straightforward brute force approach:
- Iterate through each element in the array using an outer loop
- For each element at index
i, iterate through all elements after it using an inner loop starting fromi+1 - Check if the sum of the current pair equals the target
- If a match is found, return the indices
- If no pair found, return NULL
- Time Complexity: O(n²) - Two nested loops iterating through the array
- Space Complexity: O(1) - Only using a constant amount of extra space (excluding the output array)
- Status: ✅ Accepted
- Runtime: 103 ms
- Runtime Percentile: Beats 32.38% of submissions
- Memory Usage: 8.58 MB
- Memory Percentile: Beats 93.07% of submissions
- Test Cases Passed: 63/63
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: nums[0] + nums[1] == 9, we return [0, 1]
Input: nums = [3,2,4], target = 6
Output: [1,2]
Explanation: nums[1] + nums[2] == 6, we return [1, 2]
Input: nums = [3,3], target = 6
Output: [0,1]
Explanation: nums[0] + nums[1] == 6, we return [0, 1]
Time Complexity: O(n) Space Complexity: O(n)
Using a hash map/hash table to store value-to-index mappings:
- Create a hash map to store values and their indices
- For each number, check if (target - current_number) exists in the hash map
- If found, return the indices
- Otherwise, add the current number to the hash map
This approach reduces time complexity to O(n) with a single pass through the array.
- Arrays and Indexing
- Nested Loops
- Hash Tables/Hash Maps
- Two Pointer Technique
- Time and Space Complexity Optimization
- C Memory Management (malloc)
- Dynamic Arrays in C
- Problem Analysis and Breakdown
- Algorithm Design Patterns
- Optimization Techniques
- Memory Management in C
- Dynamic Array Allocation
- Code Efficiency
- PROBLEM_DESCRIPTION.md - Detailed problem explanation and approaches
- INPUT_OUTPUT_EXAMPLES.md - Test cases and expected outputs
- SOLUTION_EXPLANATION.md - Step-by-step code walkthrough
- twoSum.c - Complete C source code with comments
- Two Sum II - Input Array Is Sorted
- Two Sum III - Data Structure Design
- 3Sum
- 4Sum
- Two Sum IV - Input is a BST
- Two Sum Less Than K
- Read the problem carefully and understand all constraints
- Identify the problem pattern and think of multiple approaches
- Start with a brute force solution first
- Analyze time and space complexity
- Optimize step by step
- Test with edge cases
- Practice regularly with different problems
- Read the problem statement in
PROBLEM_DESCRIPTION.md - Study the test cases in
INPUT_OUTPUT_EXAMPLES.md - Review the C implementation in
twoSum.c - Try implementing it yourself first
- Compare your solution with the provided approach
- Explore the optimization techniques
- Practice solving similar problems
For deeper learning:
- LeetCode: Official problem and discussions
- GeeksforGeeks: DSA tutorials
- HackerRank: Practice problems
- YouTube: Code with Harry, Striver's DSA course
This repository is a learning project created while preparing for technical interviews and building strong problem-solving skills in C programming. Each file contains detailed explanations to help beginners understand not just the solution, but the thought process behind it.
Happy Learning! 🚀 Keep practicing, keep improving!