๐Ÿ“˜ Day 1 โ€“ Teacher's Day Challenge | Even Digit Count + Two Sum

TanishiTanishi
3 min read

๐Ÿ—“๏ธ Date: July 25, 2025
๐Ÿ”ข Problems Solved Today: 2
๐ŸŽฏ Focus Topic: Arrays, Brute Force, Digit Operations

๐Ÿ’ป Platform: Leetcode


๐Ÿ”— Table of Contents


๐Ÿงฉ Problem 1 โ€“ Count Numbers with Even Number of Digits

๐Ÿ“š Difficulty: Easy
๐Ÿง  Concepts: Math, Digit Count


๐Ÿ“„ Problem Statement (Summary):

Given an array nums, return how many numbers have an even number of digits.

Example 1:
Input: [12,345,2,6,7896] โ†’ Output: 2

Example 2:
Input: [555,901,482,1771] โ†’ Output: 1


๐Ÿ’ก Approach:

  • Count the digits in each number using logarithm

  • If the count is even, increment result counter

  • Edge case: 0 has 1 digit


๐Ÿงช Code (C++):

class Solution {
public:
    int countDigits(int n) {
        if (n == 0)
            return 1; // Handle 0 separately
        return (int)(log10(abs(n))) + 1;
    }

    int findNumbers(vector<int>& nums) {
        int c = 0;
        for (int num : nums)
            if (!(countDigits(num) & 1))  // Check if digit count is even
                c++;
        return c;
    }
};

๐Ÿ“ธ Submission Screenshot:


โœ… Key Takeaways:

  • log10(n) + 1 gives digit count for positive numbers

  • Bitwise & 1 helps quickly check for odd/even

  • Edge case handled for 0 separately


๐Ÿงฉ Problem 2 โ€“ Two Sum

๐Ÿ“š Difficulty: Easy
๐Ÿง  Concepts: Brute Force, Array Traversal


๐Ÿ“„ Problem Statement (Summary):

Find two indices in an array such that their values add up to a given target.
Return the indices in any order.

Example:
Input: nums = [2,7,11,15], target = 9 โ†’ Output: [0,1]


๐Ÿ’ก Approach:

  • Brute force using nested loops to try every pair

  • Return the first valid pair that sums to the target


๐Ÿงช 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 O(nยฒ) in time

  • Better version uses HashMap (O(n)), will explore later

  • Ensure not using same element twice


๐Ÿ“ˆ Daily Summary

MetricValue
Problems Solved2
Topics CoveredArrays, Math, Brute Force
Tools UsedC++
Next FocusHashMap, Two Pointers
Day1 / 30

๐Ÿท๏ธ Tags:

#leetcode #43DaysChallenge #arrays #c++ #dsa #twosum #evenDigits

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