Day 52 of #100DaysOfCode 🧩 Problem: 3321. Find X-Sum of All K-Long Subarrays II Difficulty: Hard This version builds on the easier “Find X-Sum” problem, but the constraints are massive — meaning brute force dies instantly. The goal is to maintain the sum of top x most frequent elements for every subarray of size k, with efficient updates as the window slides. 💡 What I learned: Balancing efficiency and correctness in sliding window problems is tricky. Frequency tracking with ordered structures (like multiset or heap) keeps updates O(log n). Rebalancing the “top x” elements correctly after each update is crucial to avoid stale counts. 🧠 Key takeaway: Optimization isn’t about shaving milliseconds; it’s about understanding how to reuse work instead of starting over every time. Every “Hard” problem eventually boils down to patience, debugging, and a little bit of emotional damage. #LeetCode #C++ #DSA #ProblemSolving #100DaysOfCode #CodingJourney
MOHITKUMAR PRAJAPATI’s Post
More Relevant Posts
-
🔗 Day 63 of #100DaysOfCode 🔗 🔹 Problem: Valid Parentheses – LeetCode ✨ Approach: Implemented a stack-based validation to ensure every opening bracket has a matching closing one in correct order. Each character is checked systematically — pushing opens and popping closes — making it both clean and efficient! ⚡ 📊 Complexity Analysis: Time Complexity: O(n) — single traversal of the string Space Complexity: O(n) — stack usage for unmatched brackets ✅ Runtime: 2 ms (Beats 97.47%) ✅ Memory: 41.96 MB 🔑 Key Insight: Stacks are the unsung heroes of structured logic — from parentheses validation to syntax parsing, they keep the balance just right. 🧠 #LeetCode #100DaysOfCode #ProblemSolving #DSA #Stack #AlgorithmDesign #CodeJourney #ProgrammingChallenge #LogicBuilding #Efficiency #CodingDaily
To view or add a comment, sign in
-
-
🚀 Problem Solved: Magical String Count 1’s in first n terms Problem Link:https://lnkd.in/epSi-snY The Magical String is unique because it describes itself. It contains only '1' and '2', and the counts of consecutive characters form the same string again. To solve the problem efficiently: 🧠 Approach Start with the base magical string: "122". Use a pointer j to read how many times the next digit should repeat. Always append the opposite digit of the last character: 1. Last digit '1' → append '2' s[j] times 2. Last digit '2' → append '1' s[j] times 3. Continue until the string reaches required size. 4. Finally, count how many '1' exist in the first n characters. This works because the magical string naturally self-generates as we follow the count pattern. Time Complexity:O(10001) Space Complexity:O(10001) #cpp #dsa #leetcode #problemSolving #logicbuilding #stringprocessing #codingJourney #competitiveProgramming #100DaysOfCode #programmer
To view or add a comment, sign in
-
-
💻 #2DayOf150 — Remove Duplicates from Sorted Array II (LeetCode #80) Day 2 and still going strong 💪 Today was all about keeping things clean and minimal — both in code and logic. After a solid dry run session, the entire idea just clicked. The two-pointer trick felt like magic once I saw how it limits duplicates perfectly in place ✨ 🧩 Approach Use one pointer to place valid elements Allow only up to 2 occurrences of each number In one pass, the array gets compressed beautifully ✅ Time: O(n) ✅ Space: O(1) ✅ Elegant, minimal, and efficient Dry run tested. Logic locked. Consistency on track 🔥 #DSA #LeetCode #CodingJourney #ProblemSolving #Cplusplus #100DaysOfCode #2DayOf150
To view or add a comment, sign in
-
-
🔹 Day 69 of #100DaysOfLeetCodeChallenge 🔹 🚀 Problem: Subsets II 🔑 Topic: Backtracking 🧠 Approach: The task is to find all unique subsets of an array that may contain duplicates. Here’s the step-by-step logic 👇 Sort the array first to bring duplicates together. Use backtracking to explore every possible subset. Skip duplicates by checking if the current element equals the previous one at the same recursion depth. Add every subset (including empty) to the result list. ⏳ Time Complexity: O(2^n) 💾 Space Complexity: O(n) (recursion stack + temp list) 📌 Example: Input: nums = [1,2,2] Output: [[], [1], [1,2], [1,2,2], [2], [2,2]] ✅ 🎯 Takeaway: Sorting and skipping duplicates isn’t just for combinations — it’s the secret weapon for clean subset generation too! ⚡ #LeetCode #DSA #Backtracking #Subsets #ProblemSolving #CodingChallenge #100DaysOfLeetCodeChallenge 🚀
To view or add a comment, sign in
-
-
Day 22 of #100DaysOfCode Today I solved the “Permutation in String” problem on LeetCode — a tricky one that really sharpens your understanding of sliding window and frequency mapping in strings. 🧩 Problem in short: Given two strings s1 and s2, the task is to check if any permutation of s1 exists as a substring in s2. At first glance, it feels like a brute-force problem — generate all permutations of s1 and check each in s2. But that’s computational suicide for longer strings. So the challenge was to find a smarter, optimized approach. ⚙️ Intuitive Approach: Instead of generating permutations, I focused on character frequencies. Maintain two frequency arrays — one for s1 and one for the current window of s2. Slide the window across s2, adding and removing characters as you move. Whenever both frequency arrays match, it means that substring of s2 is a permutation of s1. This approach reduces the complexity drastically and relies on pattern recognition through frequency matching, not brute force. Every day in this challenge is a reminder that optimization isn’t about doing less work — it’s about doing the right work. #LeetCode #C++ #ProblemSolving #DSA #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐃𝐚𝐢𝐥𝐲 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: 𝟑𝟐𝟐𝟖-𝐌𝐚𝐱𝐢𝐦𝐮𝐦 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬 𝐭𝐨 𝐌𝐨𝐯𝐞 𝐎𝐧𝐞𝐬 𝐭𝐨 𝐭𝐡𝐞 𝐄𝐧𝐝 🧩 Problem Statement: You’re given a binary string s. You can repeatedly choose an index i where s[i] == '1' and s[i + 1] == '0', and move that '1' to the right until it reaches the end or another '1'. You need to return the maximum number of operations that can be performed. Example: Input: s = "1001101" Output: 4 🧠 Approach: The idea is to notice that every '1' before a block of '0' can move once for that block. So, as we traverse the string :- • Keep counting '1's as they appear. • When we encounter a block of '0's, add the total number of '1's seen so far to our answer. • This way, each zero block contributes operations equal to the number of ones before it. Time Complexity: O(n) Space Complexity: O(1) #LeetCode #ProblemSolving #DSA #CodingJourney #LogicBuilding #DailyChallenge #KeepLearning #100DaysOfCode
To view or add a comment, sign in
-
-
💡 Day 77 of #100DaysOfCode 💡 🔹 Problem: Majority Element – LeetCode ✨ Approach: Used a sorting-based strategy to quickly identify the majority element. By sorting the array, the middle element naturally becomes the majority — simple, clean, and surprisingly powerful! 🌟 📊 Complexity Analysis: ⏱ Time Complexity: O(n log n) — due to sorting 💾 Space Complexity: O(1) — constant auxiliary space ✅ Runtime: 7 ms (Beats 39.33%) ✅ Memory: 55.75 MB 🔑 Key Insight: Sometimes the smartest solution is also the simplest — a little ordering can reveal the dominant pattern hiding in plain sight. ✨ #LeetCode #100DaysOfCode #ProblemSolving #DSA #AlgorithmDesign #CodingDaily #ProgrammingChallenge #Sorting #LogicBuilding #CodeJourney
To view or add a comment, sign in
-
-
🔢 “Today was all about turning strings into numbers — one recursive call at a time.” 🚀 Day 54/150 – DSA Challenge ✅ Problem solved: String to Integer (atoi) – LeetCode 8 (Recursion Approach) Converting a string to an integer sounds simple… until you start handling edge cases. Steps I followed: 1. Skip leading spaces. 2. Check for sign (+ / -). 3. Parse digits recursively. 4. Handle overflow by bounding between INT_MIN and INT_MAX. The recursion gave it a clean logical flow — digit by digit, call by call. ⏳ Time: O(n) 📦 Space: O(n) (recursion stack) 💡 “Recursion isn’t just looping — it’s trusting the next call to finish what you started.” 🔥 From text to integers, one call at a time. #Day54 #DSA #150DaysChallenge #LeetCode #StriverSheet #Recursion #ProblemSolving #CodingJourney #Atoi
To view or add a comment, sign in
-
-
🎯 Day 81 of #100DaysOfCode 🎯 🔹 Problem: Reverse Only Letters – LeetCode ✨ Approach: Used a two-pointer strategy to reverse only the alphabetic characters while keeping all non-letter characters in their original positions. By moving pointers inward and swapping only when both characters are letters, the solution stays efficient and elegant! 🔄✨ 📊 Complexity Analysis: ⏱ Time Complexity: O(n) — each character visited at most once 💾 Space Complexity: O(n) — due to character array construction ✅ Runtime: 0 ms (Beats 100% 🚀) ✅ Memory: 42.96 MB 🔑 Key Insight: Sometimes, all you need is smart pointer movement — not everything needs to be reversed, only the right pieces. #LeetCode #100DaysOfCode #DSA #TwoPointers #StringManipulation #ProblemSolving #CleanCode #AlgorithmDesign #CodingChallenge
To view or add a comment, sign in
-
More from this author
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
Your detailed approach to hard problems reflects genuine growth mindset