Welcome to Episode 6 of our Competitive Programming for Beginners Challenge! Today, we are taking on the undisputed king of coding interviews, a true rite of passage for every programmer: LeetCode Number 1: Two Sum.
When you step into a technical interview, your interviewer wants to see how your brain works. The best way to start tackling a problem like Two Sum is to establish the baseline solution—the Brute Force method. Today, we will learn how to use nested loops to guarantee we find the correct answer.
(Placeholder: Insert the Episode 06 thumbnail image here)
The Problem Statement
Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to the 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.
Example:
- Input:
nums = [2, 7, 11, 15],target = 9 - Output:
[0, 1] - Explanation: Because nums[0] + nums[1] == 9, we return 0 and 1.
The Logic & Theory Breakdown: Brute Force
“Brute Force” means trying every single possible combination, one by one, until you stumble upon the correct answer. It relies on sheer computing power. To do this, we use Nested Loops (a loop inside a loop).
Think of this as the “Two-Finger Method”:
- The Left Finger (Outer Loop
i): We point our left finger at the very first number in the array. This finger stays completely still while we search for its matching partner. - The Right Finger (Inner Loop
j): We point our right finger at the next number in the array (j = i + 1). We check: do these two numbers add up to our target?- If yes, we win! Return the indices.
- If no, we move the right finger one spot over and check again.
- Resetting: If the right finger reaches the end of the array and we haven’t found a match, we move the left finger over by one, reset the right finger to be right next to it, and start checking again.
(Note: The inner loop j must always start at i + 1. If we started at 0, we might accidentally use the exact same number twice, which breaks the rules of the problem!)
C++ Code Implementation
Here is the clean, C++ Brute Force solution for Two Sum. Notice the very clean return {i, j}; syntax at the end—this is a modern C++ trick (Uniform Initialization) that creates and returns our vector in a single line!
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
vector<int> answer; // 1. Create an empty vector
// Outer loop (The Left Finger)
for(int i = 0; i < n; i++)
{
// Inner loop (The Right Finger) - always starts right after 'i'
for(int j = i+1; j< n; j++)
{
// If the two numbers add up to our target
if(nums[i] + nums[j] == target)
{
answer.push_back(i); // 2. Push the first index
answer.push_back(j); // 3. Push the second index
return answer; // Return their indices instantly
}
}
}
return answer;
}
};Complexity Analysis
- Time Complexity: O(n^2). Because we have a loop nested inside another loop, in the worst-case scenario (the answer is at the very end of the array), we have to check nearly every single combination.
- Space Complexity: O(1). We are not creating any new arrays or data structures to solve the problem, we are simply returning two integer indices. The space memory footprint is constant.
Conclusion
Congratulations on solving the most famous interview question on the internet!
While O(n^2) is considered a “slow” time complexity, explaining the Brute Force method first shows an interviewer that you understand the core mechanics of the problem. Later in your coding journey, you will learn how to optimize this exact problem to O(n) time using Hash Maps!If you want to see the “Two-Finger” visual breakdown on the whiteboard, watch the full video tutorial below!
Hit the “Join” button on our YouTube channel to become a Snippet Supporter! You’ll get instant access to our downloadable ad-free Master Notes, featuring the complete code and step-by-step logic breakdown so you can confidently review before your placement drives. Link to join our Youtube Channel Simple Snippets : https://www.youtube.com/channel/UCRIWTSgd7hGtZhx4RYoASEg/join