🔹 Day 45: Move Zeroes (LeetCode #283) 📌 Problem Statement: Given an integer array nums, move all 0s to the end of it while maintaining the relative order of the non-zero elements. The operation must be done in-place, without making a copy of the array. ✅ My Approach: I used a two-pointer technique — one pointer tracks the position to place the next non-zero element, and the other iterates through the array. Whenever a non-zero element is found, it is swapped with the element at the tracking pointer position. This efficiently pushes all zeroes to the end while keeping the order intact. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 3 ms (Beats 20.93%) Memory: 45.96 MB (Beats 86.42%) 💡 Reflection: A clean and efficient in-place array manipulation problem that reinforced the importance of pointer management for space optimization. 🚀 #LeetCode #Java #Array #TwoPointers #100DaysOfCode #Day45
Ananth B’s Post
More Relevant Posts
-
🚀 Day 87/100 of #100DaysOfCode 💡 Problem: 3321. Find X-Sum of All K-Long Subarrays II (Hard) 🔗 Platform: LeetCode Today’s challenge was all about optimizing sliding window logic with frequency-based ordering! The task — find the “x-sum” of every k-sized subarray, keeping only the top x most frequent elements (and if tied, prefer larger values). 🧠 Key Learnings: How to maintain frequency counts efficiently in a sliding window. Managing top-x elements dynamically using TreeSet and custom comparators. Overcoming TLE issues by switching from naive sorting (O(n*k)) to a balanced TreeSet approach (O(n log n)). ⚙️ Concepts Used: → Sliding Window → Frequency Map (HashMap) → Ordered Sets (TreeSet) → Custom Comparator Logic 🔥 This one really tested both logic & optimization — but once it clicked, it felt amazing! #LeetCode #100DaysOfCode #Java #CodingChallenge #ProblemSolving #DSA #SlidingWindow #Optimization
To view or add a comment, sign in
-
-
🔹 Day 50: Maximum Average Subarray I (LeetCode #643) 📌 Problem Statement: Given an integer array nums and an integer k, find the contiguous subarray of length k that has the maximum average value and return this value. ✅ My Approach: I used the sliding window technique to efficiently calculate the sum of subarrays of length k. First, I computed the sum of the first k elements. Then, as the window slid forward, I subtracted the element going out and added the new element entering the window. I continuously updated the maximum sum encountered and finally returned the average by dividing it by k. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 99.90%) Memory: 56.27 MB (Beats 66.43%) 💡 Reflection: This problem highlighted how the sliding window technique can drastically improve performance over recalculating sums for each subarray, making the approach both elegant and efficient. 🚀 #LeetCode #Java #SlidingWindow #Optimization #100DaysOfCode #Day50
To view or add a comment, sign in
-
-
🚀 #SridharSolves100 – Day 97 | Smallest Number With All Set Bits 💡 Today’s challenge was all about bit manipulation brilliance 🧠💻 🧩 Problem: Given a number n, find the smallest number x ≥ n such that the binary representation of x contains only set bits (1s). Approach: 1. Find the highest bit length of n. 2. Generate a number of the same bit-length with all bits set → (1 << bits) - 1. 3. If this number < n, increase bit length by 1 and repeat. 4. Return the resulting all-1s number. 5. Bitwise shifts make this constant-time for small n. ⏱️ Complexity: Time – O(1) | Space – O(1) 💡 Real-world analogy: 1. Imagine lighting a sequence of bulbs 🔆— we keep extending the row until all bulbs stay ON continuously from the start. 🔗 My Solution: https://shorturl.at/jyMqs #100DaysOfCode #SridharSolves100 #BitManipulation #Java #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
📌 Day 12/100 – 100 Days of Code Challenge 🚀 Today’s LeetCode challenge: “Trapping Rain Water” (LeetCode #42) – one of the most popular Hard-level problems that strengthens logic building and array manipulation skills. 🔹 Approach: Used precomputed leftMax and rightMax arrays to determine how much water each bar can trap. For every index, the trapped water = min(leftMax[i], rightMax[i]) - height[i]. 🔹 Key Learnings: Improved understanding of prefix and suffix computations. Reinforced problem-solving using array-based preprocessing. Time Complexity: O(n) Space Complexity: O(n) #100DaysOfCode #LeetCode #Java #DSA #Arrays #TwoPointer #ProblemSolving #CodingJourney #DynamicProgramming #DailyChallenge
To view or add a comment, sign in
-
-
Problem: "73. Set Matrix Zeroes" The core of this problem is simple: if a cell is zero, its entire row and column must become zero. But the execution is tricky—you can't modify the matrix while you're still scanning for original zeros! My solution uses a clean, two-pass strategy with auxiliary storage: Pass 1: Use two HashSets to record the indices of rows and columns that contain an initial zero. Pass 2: Iterate again and set any cell to zero if its row or column is present in the HashSets. This approach is highly readable and ensures optimal O(M . N) time complexity. While this solution uses O(M+N) extra space, it's a fantastic baseline. The next step is tackling the O(1) space constraint by cleverly using the matrix's first row and column as the marker sets. That's the real optimization puzzle! #Algorithms #DataStructures #LeetCode #CodingChallenge #Java #SoftwareEngineering #100daysofcode
To view or add a comment, sign in
-
-
🔥 Day 33/100 of #100DaysOfCode - Alternating Positive & Negative! Today's Problem: Rearrange Array Elements by Sign Task: Given an array with equal number of positive and negative integers, rearrange them in alternating positive/negative order (starting with positive). Solution: Used a two-pointer approach with separate indices for positive and negative positions! Created a temp array and placed positives at even indices (0, 2, 4...) and negatives at odd indices (1, 3, 5...). Key Insights: Since the array has equal positives and negatives, we can pre-determine positions O(n) time complexity with O(n) space for the temp array Clean and intuitive - no complex swapping needed Smart Pointer Management: Positive pointer moves by +2 each time Negative pointer moves by +2 each time Single pass through the original array Elegant solution for maintaining relative order while achieving the required alternation! ⚡ #100DaysOfCode #LeetCode #Java #Algorithms #Arrays #TwoPointers #CodingInterview
To view or add a comment, sign in
-
-
🔹 Day 48: Container With Most Water (LeetCode #11) 📌 Problem Statement: Given an array height[], where each element represents the height of a vertical line, find two lines that together form a container holding the maximum amount of water. ✅ My Approach: I used the two-pointer technique — starting one pointer at the beginning and another at the end of the array. At each step, I calculated the area using width * min(height[left], height[right]) and kept track of the maximum area. Then, I moved the pointer pointing to the shorter line, since that’s the limiting factor for water storage. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 5 ms (Beats 76.45%) Memory: 58.32 MB (Beats 14.37%) 💡 Reflection: This problem reinforced how the two-pointer approach can transform a brute-force O(n²) problem into an efficient O(n) solution. 🚀 #LeetCode #Java #TwoPointers #Greedy #100DaysOfCode #Day48
To view or add a comment, sign in
-
-
💻 Day 48 of #LeetCode100DaysChallenge Solved LeetCode 151: Reverse Words in a String — a simple yet elegant string manipulation problem that tests precision and edge-case handling. 🧩 Problem: Given a string s, reverse the order of words while removing extra spaces. Each word is a sequence of non-space characters, and the result should contain exactly one space between words with no leading or trailing spaces. 💡 Approach Used — String Splitting & Reversal: 1️⃣ Trim leading and trailing spaces. 2️⃣ Split the string by spaces to get individual words. 3️⃣ Filter out empty strings caused by multiple spaces. 4️⃣ Reverse the list of words and join them with a single space. ⚙️ Complexity: Time: O(N) Space: O(N) ✨ Key Takeaways: ✅ Strengthened understanding of string manipulation and trimming. ✅ Learned to handle irregular spacing efficiently. ✅ Practiced building clean, readable string-processing logic. #LeetCode #100DaysOfCode #Java #StringManipulation #ProblemSolving #DSA #CodingJourney #WomenInTech
To view or add a comment, sign in
-
-
🔹 Day 36: Merge Sorted Array (LeetCode #88) 📌 Problem Statement: You are given two sorted arrays, nums1 and nums2, and two integers m and n representing the number of initialized elements in each. Merge nums2 into nums1 as one sorted array in non-decreasing order. ✅ My Approach: I appended all elements of nums2 into the extra space of nums1 and then sorted the combined array. While this is a simple approach, an optimized method could merge from the end to avoid extra sorting. 📊 Complexity: Time Complexity: O((m + n) log(m + n)) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 1 ms (Beats 27.92%) Memory: 42.32 MB 💡 Reflection: This problem reinforces the importance of understanding in-place array manipulation. A clean and straightforward implementation that still gets the job done! 💪 #LeetCode #Java #Arrays #Sorting #100DaysOfCode #Day36
To view or add a comment, sign in
-
-
🔥 LeetCode Daily Challenge – Day 14 🧩 Problem: 3312. Number of Substrings With One’s Count at Least Square of Zero’s Count 🚀 Approach: Zero-Positions + Prefix Optimization Today’s problem was an interesting blend of math and string analysis. The key insight was recognizing that zero-count grows slowly, and feasible substrings can be efficiently tracked using prefix logic and previous zero positions. ⭐ My approach: Precompute previous zero indices for fast block calculations Track zeros and compute required ones using ones ≥ zeros² Use a mathematical bound (sqrt(n)) to safely prune infeasible ranges Achieved near-linear behavior on large inputs Clean, optimized, and significantly faster than brute force. 💡 This method avoids triple-nested loops and leverages structure in binary strings. 📈 Result: ✔️ Accepted ⚡ Runtime: 115ms (Beats 83.33%) 📦 Memory: 47.07MB (Beats 26.85%) Day 14 streak still going strong! 🔥 #LeetCode #DailyChallenge #Java #DSA #ProblemSolving #DailyCodingChallenge #CodingStreak
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development