Solved Longest Subarray with At Most 2 Distinct Elements using Sliding Window.
Two pointers + unordered_map.
Even though thereβs a while loop inside a for loop, it still runs in O(n) because both pointers move only forward.
Simple idea, powerful result π₯
#DSA#Coding
Solved LeetCode 1404 β Number of Steps to Reduce a Number in Binary Representation to One π»π₯
Binary simulation + carry handling = clean O(n) solution.
String traversal from right to left makes it elegant.
#LeetCode#DSA#Binary#Coding#100DaysOfCode
Cracked 148. Sort List π₯
Sorted a linked list in ascending order using Merge Sort (fast & slow pointer to split + merge step).
β± Time: O(n log n)
π¦ Space: O(1) (constant extra space)
Clean pointer manipulation. No array conversion.
#LeetCode#DataStructures#LinkedList
LeetCode 21 β Merge Two Sorted Lists β
Used dummy node + iterative pointer approach to merge in O(n + m) time.
Clean pointer manipulation makes it simple & efficient.
Linked Lists getting stronger πͺ
#LeetCode21#DSA#LinkedList#Cpp
Solved #42 β Trapping Rain Water π§π₯
Turned bars into boundaries and gaps into gallons.
Calculated trapped water using left & right max heights β clean O(n) solution.
When elevation rises, logic must rise higher π
#LeetCode#DSA#CPP#ProblemSolving
Solved LeetCode 868 β Binary Gap β
Used pure bit manipulation instead of converting to string π₯
Tracked positions of consecutive 1s using:
n & 1
Right shift n >>= 1
Stored last seen index
Time: O(log n)
Space: O(1)
Bit tricks > string conversion πͺ
#LeetCode#DSA
Solved LeetCode 762 β Prime Number of Set Bits β
Optimized by:
β’ Using n & (n-1) to count set bits
β’ Precomputing primes (since max set bits β€ 20)
β’ Avoiding repeated prime checks
Key insight: We check if the count of 1s is prime, not the number itself π₯
#LeetCode#DSA
Solved LeetCode 442 β Find All Duplicates in an Array π
Used index marking trick (value β index mapping).
If nums[abs(x)-1] is negative β duplicate found.
β± O(n) time
π¦ O(1) space
In-place hashing is powerful π₯
#LeetCode#DSA#CodingInterview#Cpp
Solved Reverse Bits (LeetCode 190) π₯
Used pure bit manipulation (no strings π«).
Shift left β extract last bit β build reversed number in O(1) time & O(1) space.
Clean, optimal & interview-friendly approach πͺ
#LeetCode#DSA#BitManipulation#100DaysOfCode
Solved LeetCode 67 β Add Binary β
Binary addition from right to left with carry handling.
Simple logic, clean implementation.
Time Complexity: O(n)
Space Complexity: O(1)
#LeetCode#DSA#Coding#CPP#100DaysOfCode