πŸ“˜ Day 4– Teacher's Day Challenge | Two Sum (Brute Force)

TanishiTanishi
2 min read

πŸ—“οΈ Date: July 28, 2025
πŸ”’ Problems Solved Today: 1
🎯 Focus Topic: Arrays, Brute Force

πŸ’» Platform: Leetcode


πŸ”— Table of Contents


🧩 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

MetricValue
Problems Solved1
Topics CoveredArrays, Brute Force
Tools UsedC++
Day4 / 30

🏷️ Tags:
#leetcode #43DaysChallenge #arrays #bruteforce #cpp #dsa #twosum #codingjourney

0
Subscribe to my newsletter

Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tanishi
Tanishi