My DSA Journey
π Recently Solved Problems
Single Number
Approach: Utilize the XOR property A ^ A = 0 and A ^ 0 = A. XORing all elements in the array will cancel out all paired numbers, leaving only the unique single number.
2025-11-12
Optimized
N/ABrute Force
N/ASpace
N/APower of Two
Approach: A positive integer N is a power of two if and only if N has exactly one bit set to 1. The bitwise trick N & (N - 1) clears the rightmost set bit.
2025-11-11
Optimized
N/ABrute Force
N/ASpace
N/APermutations (Backtracking)
Approach: Use a recursive function. At each step, iterate through the elements not yet included in the current permutation. This requires tracking the state of which numbers are used.
2025-11-10
Optimized
N/ABrute Force
N/ASpace
N/ASubsets (Backtracking)
Approach: Use a recursive function to explore all possibilities. At each element, make a choice: 1) Include the element and recurse. 2) Exclude the element and recurse. Backtrack by removing the element.
2025-11-09
Optimized
N/ABrute Force
N/ASpace
N/ACheck Palindrome and Armstrong Number
Approach: Both require reversing the digits. For Palindrome, check if the original number equals the reversed number. For Armstrong, check if the original number equals the sum of its digits raised to the power of the total digit count.
2025-11-08
Optimized
N/ABrute Force
N/ASpace
N/AGreatest Common Divisor (GCD/HCF)
Approach: Use the efficient Euclidean Algorithm, which relies on the property: GCD(a, b) = GCD(b, a % b).
2025-11-07
Optimized
N/ABrute Force
N/ASpace
N/ACount Digits and Reverse Number
Approach: Use a while loop that continues while N > 0. Inside the loop, extract the last digit (N % 10) and update N (N /= 10).
2025-11-06
Optimized
N/ABrute Force
N/ASpace
N/ASpiral Traversal of Matrix
Approach: Use four pointers (top, bottom, left, right) to define boundaries. Iterate through the four sides, shifting the boundaries inward after each pass.
2025-11-05
Optimized
N/ABrute Force
N/ASpace
N/ADiagonal Traversal of Matrix
Approach: Elements on the same anti-diagonal share the same sum of indices (i + j). Group elements by this sum and process the groups.
2025-11-04
Optimized
N/ABrute Force
N/ASpace
N/ARotation of Matrix (90 Degrees Clockwise)
Approach: To achieve a 90-degree clockwise rotation in-place, first transpose the matrix (swap A[i][j] with A[j][i]), and then reverse each row.
2025-11-03
Optimized
N/ABrute Force
N/ASpace
N/AValid Parentheses
Approach: Use a Stack to store opening brackets. Use a Hash Map for O(1) lookup to find the matching opener when a closing bracket is encountered.
2025-11-02
Optimized
N/ABrute Force
N/ASpace
N/AInteger to Roman
Approach: Use a lookup table of Roman values and symbols, sorted from largest to smallest. Greedily subtract the largest possible value until the integer reaches zero.
2025-11-01
Optimized
N/ABrute Force
N/ASpace
N/AFirst Unique Character in a String
Approach: Use a two-pass approach: 1) Count frequencies in a Hash Map. 2) Iterate through the string again and return the index of the first character with a count of 1.
2025-10-31
Optimized
N/ABrute Force
N/ASpace
N/ARoman to Integer
Approach: Iterate through the string from left to right. If the current symbol's value is less than the next symbol's value, subtract it; otherwise, add it.
2025-10-30
Optimized
N/ABrute Force
N/ASpace
N/AValid Palindrome (Alpha-Numeric Filter)
Approach: Use two pointers (one at start, one at end). Move pointers inward, skipping non-alphanumeric characters. Compare the characters after converting them to the same case.
2025-10-29
Optimized
N/ABrute Force
N/ASpace
N/AReverse Words in a String
Approach: Split the string into words, then process the list of words in reverse order, ignoring extra spaces. Or, use two pointers to reverse each word in-place, then reverse the entire string.
2025-10-28
Optimized
N/ABrute Force
N/ASpace
N/ACompress String (Run-Length Encoding)
Approach: Use a two-pointer approach (read pointer and write pointer) to count consecutive identical characters and replace the sequence with the character followed by the count.
2025-10-27
Optimized
N/ABrute Force
N/ASpace
N/AVowels and Consonants
Approach: Use a predefined set (HashSet in Java/C++, set in Python) of vowels for fast O(1) lookups during a single string traversal.
2025-10-26
Optimized
N/ABrute Force
N/ASpace
N/ALetter Coverage (Pangram Check)
Approach: Use a fixed-size boolean array (size 26) to mark the presence of each letter (a-z) encountered during a single pass.
2025-10-25
Optimized
N/ABrute Force
N/ASpace
N/ADigit String Check
Approach: Iterate through the string and use a built-in character method (e.g., isdigit() or Character.isDigit()) to check if every character is a numerical digit.
2025-10-24
Optimized
N/ABrute Force
N/ASpace
N/AOdd Even Index
Approach: Iterate through the string index and check the parity (i % 2) of the index to route the character to the odd or even result string.
2025-10-23
Optimized
N/ABrute Force
N/ASpace
N/ATriangle and Pyramid Patterns
Approach: Use the row index (i) to determine the number of characters or spaces to print in the inner loop.
2025-10-22
Optimized
O(NΒ²)Brute Force
O(NΒ²)Space
O(1)Half Diamond Pattern
Approach: Print the pattern in two halves: the increasing part (top) and the decreasing part (bottom).
2025-10-21
Optimized
O(NΒ²)Brute Force
O(NΒ²)Space
O(1)Filled and Hollow Rectangle
Approach: Use two nested loops (i for rows, j for columns). For Hollow, add a conditional check for boundaries.
2025-10-20
Optimized
O(N*M)Brute Force
O(N*M)Space
O(1)Submatrix Sum
Approach: Brute force sum for small N (O(N^2)) or 2D Prefix Sum for optimization (O(1) query).
2025-10-19
Optimized
O(NΒ²)Brute Force
O(NΒ²)Space
O(NΒ²)Lower Triangle Sum
Approach: Iterate through the matrix and only sum elements where the row index (i) is greater than or equal to the column index (j), i.e., i >= j.
2025-10-18
Optimized
N/ABrute Force
N/ASpace
N/AMatrix Row and Column Sums
Approach: Calculate Row Sums by streaming and Column Sums using an auxiliary 1D array.
2025-10-17
Optimized
O(N*M)Brute Force
O(N*M)Space
O(M)Natural Numbers Sum
Approach: Use Gauss's formula, $S = rac{N(N+1)}{2}$, to calculate the sum in constant time.
2025-10-16
Optimized
N/ABrute Force
N/ASpace
N/AFactorial
Approach: Use an iterative loop starting from 1 up to N, multiplying the result at each step. Must handle data type overflow for large N.
2025-10-15
Optimized
N/ABrute Force
N/ASpace
N/AArithmetic Progression Check
Approach: Calculate the difference between the first two elements. Then, iterate through the rest of the array, checking if the difference between all consecutive pairs is constant.
2025-10-14
Optimized
N/ABrute Force
N/ASpace
N/AArithmetic Operators
Approach: Read two numbers and a character operator. Use conditional logic (if/else or switch) to perform the correct operation.
2025-10-13
Optimized
N/ABrute Force
N/ASpace
N/ATriangle Validator
Approach: Check the triangle inequality theorem: the sum of the lengths of any two sides of a triangle must be greater than the length of the third side.
2025-10-12
Optimized
N/ABrute Force
N/ASpace
N/ANumber Distribution (Frequency Count)
Approach: Use a hash map to count the occurrences of each number. Then, iterate through the map to find the counts of required numbers.
2025-10-11
Optimized
N/ABrute Force
N/ASpace
N/AFind Duplicate Number in Array
Approach: Treat the array as a linked list where the index i points to the value nums[i]. A duplicate creates a cycle.
2025-10-10
Optimized
N/ABrute Force
N/ASpace
N/AOdd and Even Sum
Approach: Use a single linear pass to check each element's parity and accumulate separate sums for odd and even numbers.
2025-10-09
Optimized
N/ABrute Force
N/ASpace
N/AThe Missing Number
Approach: Use the arithmetic series formula (N*(N+1)/2) to find the expected sum, and subtract the actual sum of the array elements to find the missing number.
2025-10-08
Optimized
N/ABrute Force
N/ASpace
N/AMax Element in Array
Approach: Initialize a variable to the smallest possible value and iterate through the array, updating the maximum value encountered.
2025-10-07
Optimized
N/ABrute Force
N/ASpace
N/ASparse Arrays
Approach: Use a Hash Map to count input string frequencies in O(N), then lookup queries in O(1).
2025-10-06
Optimized
O(N + M)Brute Force
O(N * M)Space
O(N)Duplicate Integer
Approach: Use a HashSet/Unordered Set to track seen numbers.
2025-10-05
Optimized
O(n)Brute Force
O(nΒ²)Space
O(n)Two Sum
Approach: Use hash map to store complements for O(n) lookup
2025-10-04
Optimized
O(n)Brute Force
O(nΒ²)Space
O(n)