Day 41 of #100DaysOfCode 💻🔥🚀 Problem: 2090. K Radius Subarray Averages 📊 💡 My Intuition: For each index, we need to calculate the average of the subarray centered at that index with radius k. If the total window size 2*k + 1 exceeds the array length, the average doesn’t exist — so we mark it as -1. To make this efficient, I used a Sliding Window 🪟 — maintaining a running sum instead of recalculating everything again and again. Simple, clean, and fast ⚡ 🧠 Approach: Use two pointers i and j ➡️ to maintain a window of size 2*k + 1. Keep track of the sum using long (to handle big numbers 💪). Once the window size matches, compute the average 🎯 and slide ahead ➡️. ⚙️ Time Complexity: O(n) 💾 Space Complexity: O(n) ✅ Efficient ✅ Handles edge cases (k = 0, large k, etc.) 🚀 Key Takeaway: Even problems that look complicated often crumble when you use the right pattern — Sliding Window keeps things elegant and efficient 💯 #Java ☕ #DSA #LeetCode #ProblemSolving #CodingJourney #100DaysOfCodeChallenge #CodeEveryday
Solved LeetCode problem 2090 with Sliding Window technique
More Relevant Posts
-
📌 Day 22/100 – Sort Colors (LeetCode 75) 🔹 Problem: Given an array containing 0s, 1s, and 2s (representing red, white, and blue), sort them in-place so that all same colors stay together in the order: 0 → 1 → 2. No built-in sorting allowed. 🔹 Approach Used: Counting Sort Count occurrences of 0, 1, and 2. Overwrite the array based on those counts. Done in single pass + rewrite, no extra sorting logic needed. 🔹 Time & Space Complexity: ⏳ Time: O(n) — one pass to count, one pass to rewrite 🧠 Space: O(1) — constant extra space 🔹 Key Learnings: ✅ Problems become simpler when we leverage constraints (only 3 values here) ✅ Counting sort is perfect when input range is small & fixed ✅ Always look for in-place optimizations before thinking of extra space #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney
To view or add a comment, sign in
-
-
💡 Day 10 of 100 Days of LeetCode 📘 Problem: #26. Remove Duplicates from Sorted Array 💻 Difficulty: Easy 🔍 Concept: The task is to remove duplicates in-place from a sorted array so that each unique element appears only once. The function should return the count of unique elements k. ⚙️ Approach Used: Used two-pointer technique — one pointer (k) tracks the position of the last unique element, and the other (i) scans the array. Whenever a new unique element is found, it’s moved to the next position in the array. 🧠 Key Learnings: Mastered the in-place modification concept in arrays. Learned how to use two-pointer pattern for linear-time solutions. Refined logic building with minimal extra space usage (O(1) space). 💻 Code (Java): class Solution { public int removeDuplicates(int[] nums) { int k = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] != nums[k]) { k++; nums[k] = nums[i]; } } return k + 1; } } 💬 Reflection: A simple-looking problem — but it’s one that trains you to think in space-efficient ways. Sometimes, mastering basics is what builds strong problem-solving intuition! #100DaysOfLeetCode #Day10 #DSA #Java #ProblemSolving #CodingChallenge #LeetCode #LearningEveryday
To view or add a comment, sign in
-
-
Today’s problem: Merge Two Sorted Lists 🔗 Problem: Given two sorted linked lists, merge them into one sorted list and return it. Example: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Approach I used: ✅ Create a dummy node to simplify pointer operations. ✅ Use a pointer (tail) to build the merged list by comparing the heads of both lists. ✅ Append the smaller node each time and move forward. ✅ When one list ends, attach the remaining nodes of the other. A clean iterative solution that keeps the space usage minimal (O(1) extra space). ⚡
To view or add a comment, sign in
-
-
💡 Day 34/100 ✅ Remove Zeros in Decimal Representation Today’s challenge was about removing all the zeros from a given number’s decimal form. For example, 1020030 → 123. It might look simple, but it reinforced the importance of handling edge cases like n = 0 or n = 1000, which can lead to empty strings or parsing errors. 🧠 Key Features: Practiced both String-based and Math-based approaches. Explored the difference between using int and long for large numbers. Strengthened understanding of number-to-string conversions and parsing. ⚙️ Time Complexity: O(d) (where d = number of digits) 💾 Space Complexity: O(d) #Day33Of100 #DSA #Java #ProblemSolving #CodingJourney #100DaysOfCode #LearnEveryday #LogicBuilding
To view or add a comment, sign in
-
-
💡 LeetCode #977 — Squares of a Sorted Array Today I solved LeetCode Problem 977: Squares of a Sorted Array ⚡ Problem Summary: Given a sorted array of integers nums, return an array of the squares of each number, also sorted in non-decreasing order. Key Idea: Even though the input array is sorted, squaring can make negative numbers larger. So we use the two-pointer approach: One pointer at the start (left), one at the end (right). Compare absolute values, place the larger square at the end of the result array, and move pointers accordingly. #LeetCode #Java #DSA #TwoPointers #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🧩 Question: “Remove all duplicates such that each element appears only once.” (This is similar to your previous one but helps you understand pointer movement better.) 📝 Problem Statement (in simple words): You are given a sorted array, like nums = [1, 1, 2, 2, 3, 4, 4] You need to modify the array in place (without using extra space) so that each unique element appears only once. And finally, return the count of unique elements. After processing, your array should look like: [1, 2, 3, 4, ...] and your function should return 4. ⚙️ How to Think — Using Two Pointers: Let’s name the two pointers: i → the “slow” pointer (starts at index 0) j → the “fast” pointer (starts at index 1) Idea: We compare nums[i] and nums[j]. If both are same, it means duplicate — move j forward to skip it. If they are different, that means nums[j] is a new unique number. Move i forward by one (i++) Copy nums[j] to nums[i] Then move j forward again Keep doing this until j reaches the end of the array. #DSA #TwoPointer #Java #CodingJourney #LearningInPublic #ProblemSolving #ProgrammerLife #MERNDeveloper #LeetCode #CodeNewbie #TechLearning #SoftwareEngineering #100DaysOfCode
To view or add a comment, sign in
-
-
🔹 Day 46: Is Subsequence (LeetCode #392) 📌 Problem Statement: Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence is formed by deleting some (possibly none) characters from the original string without disturbing the relative order of the remaining characters. ✅ My Approach: I used a two-pointer technique — one pointer iterates through string t, and the other tracks progress through string s. Each time a matching character is found, the pointer for s moves forward. If we reach the end of s, it means all its characters appeared in sequence within t. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 68.53%) Memory: 41.32 MB (Beats 87.28%) 💡 Reflection: A simple yet elegant problem that highlights how pointer movement can efficiently handle string comparisons without extra memory usage. ✨ #LeetCode #Java #Strings #TwoPointers #100DaysOfCode #Day46
To view or add a comment, sign in
-
-
🚀 Day 50 of My LeetCode Journey Today, I solved Reshape the Matrix. Problem in short: Given a matrix, reshape it to a new size r x c while keeping the row-traversal order intact. If it’s impossible, return the original matrix. Approach I used: First, check if total elements match (originalRows * originalCols == r * c). Use a single counter to traverse the original matrix and place elements in the new matrix. Map the counter to new matrix indices: row = count / c col = count % c Why this is clean: Simple and intuitive. No need for multiple variables for row and column. Works for any size matrix. Example: Original: 1 2 3 4 Reshaped to 1×4 → 1 2 3 4 This problem taught me how matrix manipulation can be simplified using smart indexing. #100DaysOfLeetCode #CodingJourney #ProblemSolving #DSA #Java #Matrix
To view or add a comment, sign in
-
-
🚀 Another good one of the Day 43 of #100DaysOfCode 💻🔥 🔹 Problem: Find All Anagrams in a String 🎯 You’re given two strings s and p. The goal is to find all starting indices of p’s anagrams in s. 💡 My Intuition: Think of it like sliding a window over s while keeping track of character frequencies. If the frequency of characters inside the current window matches that of p, we’ve found an anagram. We maintain two arrays of size 26 (for each lowercase letter): arr → frequency of chars in p arr2 → frequency of chars in current window of s We keep sliding the window of size p.length() and compare both arrays at each step. 🧠 Key takeaway: Sliding window + frequency matching = powerful combo for string problems! It improves your intuition for optimizing brute-force solutions efficiently. 💪 #100DaysOfCode #LeetCode #DSA #Java #CodingJourney #ProblemSolving #KeepLearning
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
-
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
💯💣