Solved LeetCode challenge 3318 using sliding window and HashMap in Java.

🚀 Day 145 of #150DaysOfCode on LeetCode 🔹 Problem: 3318. Find X-Sum of All K-Long Subarrays I 🔹 Topic: Sliding Window | HashMap | Sorting Today’s challenge was about computing the X-Sum for every subarray of length k in an array — where the X-Sum is defined as the sum of the top x most frequent elements (considering frequency first and then value). To solve this: I used a sliding window to iterate over all subarrays of size k. For each window, I maintained a frequency map of elements. Then sorted them by frequency (descending) and by value to select the top x contributing elements. Finally, calculated the cumulative X-Sum for each subarray. It was a great exercise in combining maps, sorting logic, and windowing concepts efficiently! 🧩 Key Learnings: How to balance frequency counting with sorting priorities. Importance of optimizing nested loops for small vs. large constraints. Clear understanding of how sorting custom objects impacts runtime. #LeetCode #CodingChallenge #Java #DSA #SlidingWindow #HashMap #ProblemSolving #CodingJourney #100DaysOfCode #WomenInTech #LearnByDoing

  • graphical user interface

To view or add a comment, sign in

Explore content categories