π Day 4β Teacher's Day Challenge | Two Sum (Brute Force)


ποΈ Date: July 28, 2025
π’ Problems Solved Today: 1
π― Focus Topic: Arrays, Brute Force
π» Platform: Leetcode
π Table of Contents
Daily Summary
π§© Problem 1 β Two Sum
π Difficulty: Easy
π§ Concepts: Array Traversal, Brute Force
π Problem Statement (Summary):
Find two indices in an array nums
such that the elements at those indices sum up to a given target
.
You must return the indices in any order. Each input has exactly one solution.
Examples:
Input:
nums = [2,7,11,15]
,target = 9
β Output:[0,1]
Input:
nums = [3,2,4]
,target = 6
β Output:[1,2]
Input:
nums = [3,3]
,target = 6
β Output:[0,1]
π‘ Approach:
Use nested loops to try every possible pair.
Check if the sum of two elements equals the target.
If so, return their indices immediately.
π§ͺ Code (C++):
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int sum = 0;
vector<int> sum_arr;
for (int i = 0; i < nums.size(); i++) {
for (int j = i + 1; j < nums.size(); j++) {
sum = nums[j] + nums[i];
if (sum == target) {
sum_arr.push_back(i);
sum_arr.push_back(j);
return sum_arr;
}
}
}
return sum_arr;
}
};
πΈ Submission Screenshot:
β Key Takeaways:
Brute force is simple but inefficient with O(nΒ²) time.
Will explore optimized solutions using HashMaps (O(n)) later.
Ensure you donβt reuse the same index twice.
π Daily Summary
Metric | Value |
Problems Solved | 1 |
Topics Covered | Arrays, Brute Force |
Tools Used | C++ |
Day | 4 / 30 |
π·οΈ Tags:#leetcode
#43DaysChallenge
#arrays
#bruteforce
#cpp
#dsa
#twosum
#codingjourney
Subscribe to my newsletter
Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
