Solved LeetCode problem 2090 with Sliding Window technique

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

  • text

To view or add a comment, sign in

Explore content categories