Abhijeet Sinha’s Post

🚀 Day 407 of #365DaysOfCode 🎯 Problem: 3321. Find X-Sum of All K-Long Subarrays II 🧩 Difficulty: Hard Today’s challenge took the concept of frequency-based subarray analysis to the next level 🔥 Given an array and two integers k and x, the goal is to compute the X-Sum for every subarray of length k. The X-Sum is calculated by taking the sum of occurrences of the top X most frequent elements in the subarray (with ties broken by larger element value). 💡 Key Idea: Since n can go up to 10⁵, we can’t recompute everything for each subarray. We use a sliding window approach with a HashMap for frequency tracking — efficiently adding and removing elements as the window moves — and compute the X-Sum dynamically. 💻 Approach: 1️⃣ Maintain frequency counts using a HashMap. 2️⃣ Sort by frequency (and value when tied). 3️⃣ Use a sliding window to efficiently update counts instead of recalculating each time. 4️⃣ Compute and store the X-Sum for each window. 📊 Complexity: O(n × log(k)) 🧠 Concepts Used: Sliding Window | HashMap | Greedy Sorting Every hard problem is a reminder that optimization isn’t just about speed — it’s about thinking smart and coding cleaner. 💪 #LeetCode #Java #ProblemSolving #CodingChallenge #Algorithms #DataStructures #100DaysOfCode #365DaysOfCode #SlidingWindow #HashMap

  • text

To view or add a comment, sign in

Explore content categories