๐ Day 1 โ Teacher's Day Challenge | Even Digit Count + Two Sum


๐๏ธ 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 numbersBitwise
& 1
helps quickly check for odd/evenEdge 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
Metric | Value |
Problems Solved | 2 |
Topics Covered | Arrays, Math, Brute Force |
Tools Used | C++ |
Next Focus | HashMap, Two Pointers |
Day | 1 / 30 |
๐ท๏ธ Tags:
#leetcode
#43DaysChallenge
#arrays
#c++
#dsa
#twosum
#evenDigits
Subscribe to my newsletter
Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
