Home | Projects | Notes > Problem Solving > LC - E - 1. Two Sum

LC - E - 1. Two Sum

 

Solutions in C++

Solution 1

Brute-force approach - Checking all possible combinations with two nested loops.

Complexity Analysis:

Solution:

 

Solution 2

Instead of checking all possible combinations with two nested loops (O(n²)), this solution uses a hash table (unordered_map) to reduce the time complexity to O(n).

Complexity Analysis:

Solution: Two-pass

Solution: One-pass