🚀 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
Abhijeet Sinha’s Post
More Relevant Posts
-
🚀 Day 406 of #500DaysOfCode 🎯 Problem: 3318. Find X-Sum of All K-Long Subarrays I 🧩 Difficulty: Easy Today’s challenge focused on computing the X-Sum for every subarray of length k. The X-Sum involves identifying the top X most frequent elements in each subarray — and if frequencies tie, we prioritize larger values. Then, we sum up the occurrences of only those selected elements 💡 💻 Approach: For each subarray of length k, count the frequency of elements. Sort elements by frequency (descending) and value (descending). Pick the top x elements and sum their occurrences in the subarray. Store this result for each subarray. 🧠 Key Concepts Used: Frequency counting using HashMap Custom sorting with comparator logic Sliding subarray analysis 📊 Complexity: O(n × k × log k) 🔍 Topics: Arrays | HashMap | Sorting Every problem solved adds a new pattern to how we think about optimization and logical structuring in code 💪 #Java #LeetCode #ProblemSolving #CodingChallenge #DataStructures #Algorithms #100DaysOfCode #500DaysOfCode
To view or add a comment, sign in
-
-
🔥 Day 109 of My DSA Challenge – Intersection of Two Arrays 🔷 Problem : 349. Intersection of Two Arrays 🔷 Goal : Return the unique intersection of two integer arrays. Order doesn’t matter, but duplicates should not appear in the result. 🔷 Key Insight : This is a set / hashmap based lookup problem. We store elements from the first array, then check if elements from the second array exist in it. To ensure uniqueness, we remove the element once counted, so duplicates don't appear. 🔷 Approach : 1️⃣ Add all elements of nums1 to a HashMap 2️⃣ Traverse nums2 3️⃣ If the element exists in the map → add to result & remove from map 4️⃣ Convert list to array and return Time Complexity: O(n + m) Space Complexity: O(n) Sometimes the simplest tools — like HashMaps/Sets — give the cleanest solutions. Learning to choose the right data structure = faster logic + cleaner code 💪 #Day109 #100DaysOfCode #LeetCode #DSA #Java #HashMap #DataStructures #CodingChallenge #Algorithm #ProblemSolving #Programming #SoftwareEngineering #TechJourney #DeveloperJourney #CodeEveryday #LearnDSA #LogicBuilding #GrowthMindset #EngineerMindset
To view or add a comment, sign in
-
💻 Day 62 of #LeetCode100DaysChallenge Solved LeetCode 142: Linked List Cycle II — a problem that deepens understanding of linked list traversal and cycle detection using pointers. 🧩 Problem: Given the head of a linked list, return the node where the cycle begins. If there’s no cycle, return null. A cycle exists if a node can be revisited by continuously following the next pointer. The goal is to identify the exact node where the cycle starts — without modifying the list. 💡 Approach — Floyd’s Cycle Detection (Tortoise and Hare): 1️⃣ Use two pointers — slow and fast. 2️⃣ Move slow by 1 step and fast by 2 steps until they meet (if a cycle exists). 3️⃣ Once they meet, reset one pointer to the head. 4️⃣ Move both one step at a time; the node where they meet again is the start of the cycle. 5️⃣ If no meeting point (i.e., fast or fast.next becomes null), return null. ⚙️ Complexity: Time: O(N) — each pointer traverses the list at most twice. Space: O(1) — no extra memory used. ✨ Key Takeaways: ✅ Strengthened Floyd’s algorithm understanding for detecting and locating cycles. ✅ Learned how mathematical reasoning helps pinpoint the start node of a loop. ✅ Reinforced efficient use of pointer manipulation in linked list problems. #LeetCode #100DaysOfCode #Java #LinkedList #CycleDetection #FloydsAlgorithm #DSA #CodingJourney #WomenInTech #ProblemSolving
To view or add a comment, sign in
-
-
✅Day 46 : Leetcode 69 - Sqrt(x) #60DayOfLeetcodeChallenge 🧩 Problem Statement Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer y should satisfy the condition: y * y <= x < (y + 1) * (y + 1) You must not use any built-in exponent functions such as pow(x, 0.5) or Math.sqrt(x). 💡 My Approach I used a binary search approach to efficiently find the integer square root. The search space is between 1 and x. At each step, I calculate mid and compute val = (long) mid * mid to avoid overflow. If val <= x, I move to the right half (low = mid + 1) and update the answer as mid. If val > x, I move to the left half (high = mid - 1). When the loop ends, ans contains the largest integer whose square is less than or equal to x. ⏱ Time Complexity O(log x) — Binary Search reduces the range by half each time. 💾 Space Complexity O(1) — Constant extra space. #LeetCode69 #BinarySearch #Math #SquareRoot #IntegerSquareRoot #Java #DataStructures #Algorithms #DSA #LeetCodeEasy #ProblemSolving #EfficientCode #OverflowHandling
To view or add a comment, sign in
-
-
🔍 Day 30 | Shift Happens (and That’s Beautiful) Today’s LeetCode challenge was #848 – Shifting Letters 🌀 At first glance, it looks like a simple “shift characters” task — but under the hood, it’s a neat example of prefix-sum logic meets modular arithmetic. 💡 The Problem: For each index i, shift the first i+1 letters by shifts[i] times — wrapping around from 'z' to 'a'. 🧠 The Trick: If we start from the end of the string and move backward, we can keep a cumulative shift sum that already accounts for all later shifts. This avoids repeatedly looping over the prefix and keeps our time complexity O(n) instead of O(n²). ⚙️ Approach: Traverse from right to left Keep adding each shift to a running total (totalShift) Apply totalShift % 26 to shift the current character efficiently Update the string using a StringBuilder 💨 Why It’s Optimal: ✅ Single pass — linear time ✅ Constant extra space ✅ Clean and scalable even for large inputs (up to 10⁵ length! Sometimes the cleanest solutions come from flipping your perspective — literally shifting your direction Shishir chaurasiya and PrepInsta #LeetCode #CodingChallenge #Java #Algorithms #ProblemSolving #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 28 of #100DaysOfCode Today’s challenge was all about understanding frequency patterns in arrays — a simple yet powerful concept in hashing 🔍 LeetCode 1207 – Unique Number of Occurrences 🔢 📌 Problem Summary: Given an integer array, determine whether each value appears a unique number of times. In other words, 👉 No two numbers should share the same frequency. 💡 Example: Input: [1,2,2,1,1,3] Output: true Because frequencies are: 1 → 3 times 2 → 2 times 3 → 1 time All unique ✔️ 🧠 My Approach: I solved it using a combination of HashMap + HashSet: 1️⃣ Count the occurrences of each number using a HashMap. 2️⃣ Insert all frequency values into a HashSet. 3️⃣ If the size of the HashSet equals the size of the map → all frequencies are unique ✔️ 4️⃣ Otherwise → duplicates exist ❌ A clean and efficient hashing technique. ⚙️ Complexity: Time: O(n) ⏱️ Space: O(n) 💾 (storing frequencies) 💡 Key Learning: HashMaps aren’t just for storing counts—they can reveal patterns, validations, and uniqueness constraints when combined with sets. This problem reinforced how frequency-based logic solves many real-world scenarios like: ✔ Detecting duplicates ✔ Comparing patterns ✔ Character/string analysis ✔ Data validation 🔥 Result: Code ran successfully with 0 ms Runtime, accepted on the first attempt! Small challenge, clean logic, satisfying finish 💪 On to the next one! 🚀 #100DaysOfCode #LeetCode #HashTable #Java #ProblemSolving #DSA #CodingJourney
To view or add a comment, sign in
-
-
🔥 Day 35/100 of #100DaysOfCode - Finding Longest Sequence! Today's Problem: Longest Consecutive Sequence Task: Find the length of the longest consecutive elements sequence in an unsorted array. Solution: Used a HashSet for O(1) lookups! First added all elements to the set, then for each number, checked if it's the start of a sequence (no num-1 in set). If yes, counted consecutive numbers ahead. Key Insights: O(n) time complexity by only checking sequence starters HashSet eliminates duplicates and provides fast lookups Avoids O(n log n) sorting approach Smart Optimization: Only begins counting from sequence starting points Each number is processed at most twice (visited in set iteration + sequence counting) Handles duplicates and empty arrays gracefully Elegant solution that transforms an O(n²) brute force into O(n) using smart data structure choice! 💡 #100DaysOfCode #LeetCode #Java #Algorithms #HashSet #Arrays #CodingInterview
To view or add a comment, sign in
-
-
🚀 Day-77 of #100DaysOfCodeChallenge Today’s problem: Delete Nodes From Linked List Present in Array (LeetCode 3217) 💻 Working with linked lists always reminds me that sometimes in code — and in life — you have to remove what’s not needed to keep things moving efficiently ✂️ This challenge was about filtering out nodes whose values existed in a given array — a perfect mix of logic, pointer manipulation, and careful iteration 🔁 ✨ What I learned today: Efficiently handling deletions in a linked list without breaking the chain Using hash sets to make lookups faster on LeetCode. The importance of clean and minimal traversal logic Some problems aren’t just about syntax — they’re about clarity of thought 🧠 Another green check ✅ Another step closer to consistency 💪 #100DaysOfCode #LeetCode #ProblemSolving #CodingJourney #DataStructures #Algorithms #LinkedList #DeveloperLife #CodeEveryday #JavaDeveloper #TechJourney
To view or add a comment, sign in
-
-
🌟 Day 118 of 150 – Search in Rotated Sorted Array [LeetCode 33] 🌟 🔧 Problem Brief: You are given a sorted integer array nums[] that has been rotated at an unknown pivot index. Your task → Find the index of a target element in nums using O(log n) runtime. If the target does not exist, return -1. 🎯 Goal: Efficiently locate the target element in a rotated sorted array using Modified Binary Search. 🧠 How We Solve It? → Binary Search with Rotation Logic At any point, one half of the array is always sorted. We can use this property to decide which side to continue searching on. 1️⃣ Initialize pointers: left = 0, right = nums.length - 1 2️⃣ While left <= right: Find middle → mid = left + (right - left) / 2 If nums[mid] == target → return mid ✅ If left half is sorted (nums[left] <= nums[mid]): If target is in this half → move right = mid - 1 Else → move left = mid + 1 Else (right half is sorted): If target is in this half → move left = mid + 1 Else → move right = mid - 1 3️⃣ If the loop ends → return -1 (target not found) 🧩 Example Walkthrough 📥 Input: nums = [4,5,6,7,0,1,2], target = 0 📘 Steps: mid = 3 → nums[mid] = 7 → left sorted → 0 not in [4,7] → move right mid = 5 → nums[mid] = 1 → left sorted → 0 in [0,1] → move left mid = 4 → nums[mid] = 0 → ✅ Found target at index 4 ✅ Output: 4 🧮 Complexity Analysis ⏱ Time Complexity: O(log n) 💾 Space Complexity: O(1) 📎 GitHub Link: https://lnkd.in/eqvwqs8N #Day118 #LeetCode #SearchInRotatedArray #BinarySearch #JavaDSA #ProblemSolving #CodingChallenge #InterviewPrep #TechWithSaravana #150DaysOfCode #DSA #LearningJourney #CodeEveryday #Algorithm
To view or add a comment, sign in
-
🔥 Day 115 of My DSA Challenge – Intersection of Two Arrays II 🔷 Problem : 350. Intersection of Two Arrays II 🔷 Goal : Return the intersection of two arrays, where each element in the result must appear as many times as it shows in both arrays. Order doesn’t matter. 🔷 Key Insight : This is an extension of the previous problem (Intersection of Two Arrays). The twist? Here we need to consider duplicate occurrences too. For example : nums1 = [1,2,2,1], nums2 = [2,2] → result should be [2,2] (because 2 appears twice in both). 🔷 Approach : 1️⃣ Use a HashMap to count occurrences of each element in nums1. 2️⃣ Traverse nums2 and check if an element exists in the map. 3️⃣ If yes → add it to the result and decrease its count in the map. 4️⃣ When the count becomes 0, remove it from the map to keep things clean. Time Complexity: O(n + m) Space Complexity: O(n) This problem strengthens hash-based frequency counting and element tracking. Use a map to record frequency — then match, decrease, and clean up. This logic is key for : ✅ Counting duplicates ✅ Array frequency comparison ✅ Inventory & matching systems Every small concept compounds into bigger problem-solving clarity. Step by step, I’m becoming stronger at thinking like a coder 💪💻 #Day115 #DSA #100DaysOfCode #HashMap #LeetCode #ProblemSolving #Java #CodingChallenge #Algorithms #DataStructures #DeveloperJourney #LogicBuilding #EngineerMindset #GrowEveryday
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