Some subarray counting problems become much easier when we transform them into “at most” problems. 🚀 Day 101/365 — DSA Challenge Solved: Binary Subarrays With Sum Problem idea: We need to count the number of subarrays whose sum equals a given goal in a binary array. Efficient approach: Instead of directly counting subarrays with exact sum, we use a trick: subarrays with sum = goal = subarrays with sum ≤ goal − subarrays with sum ≤ (goal − 1) Steps: 1. Create a helper function to count subarrays with sum at most k 2. Use a sliding window to maintain a valid window where sum ≤ k 3. Add the number of valid subarrays ending at each index 4. Subtract results to get the exact count for the goal This converts the problem into an efficient sliding window solution. ⏱ Time: O(n) 📦 Space: O(1) Day 101/365 complete. 💻 264 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
Solving Binary Subarrays With Sum Using Sliding Window
More Relevant Posts
-
📘 DSA Journey — Day 28 Today’s focus: Binary Search for minimum in rotated arrays. Problem solved: • Find Minimum in Rotated Sorted Array (LeetCode 153) Concepts used: • Binary Search • Identifying unsorted half • Search space reduction Key takeaway: The goal is to find the minimum element in a rotated sorted array. Using binary search, we compare the mid element with the rightmost element: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) This works because the rotation creates one unsorted region, and the minimum always lies in that region. By narrowing the search space each time, we achieve O(log n) time complexity. This problem highlights how slight modifications in array structure still allow binary search to work efficiently with the right observations. Continuing to strengthen binary search patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Many subarray problems follow the same hidden pattern — once you recognize it, they become much easier. 🚀 Day 102/365 — DSA Challenge Solved: Count Number of Nice Subarrays Problem idea: We need to count the number of subarrays that contain exactly k odd numbers. Efficient approach: Use the same trick as previous problem: subarrays with exactly k odds = subarrays with ≤ k odds − subarrays with ≤ (k − 1) odds Steps: 1. Use a sliding window to count subarrays with at most k odd numbers 2. Expand window by moving right pointer 3. If odd count exceeds k, shrink window from left 4. Add valid subarrays ending at each index 5. Subtract results to get exact count This pattern works beautifully for binary-like conditions. ⏱ Time: O(n) 📦 Space: O(1) Day 102/365 complete. 💻 263 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟵/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟰𝟰. 𝗙𝗶𝗻𝗱 𝗦𝗺𝗮𝗹𝗹𝗲𝘀𝘁 𝗟𝗲𝘁𝘁𝗲𝗿 𝗚𝗿𝗲𝗮𝘁𝗲𝗿 𝗧𝗵𝗮𝗻 𝗧𝗮𝗿𝗴𝗲𝘁 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Looks easy — but the circular wrap-around is the trap most people miss. 𝗧𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: Given a sorted circular array of letters, find the smallest letter strictly greater than the target. If none exists, wrap around to the first letter. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵: ✅ Standard binary search for the first letter > target ✅ If letters[mid] > target → go left (end = mid - 1) ✅ Else → go right (start = mid + 1) ✅ After the loop, start points to the answer ✅ Use start % letters.length to handle the circular wrap-around 𝗧𝗵𝗲 𝗰𝗹𝗲𝘃𝗲𝗿 𝗯𝗶𝘁: If target is greater than or equal to all letters, start lands at letters.length — modulo wraps it back to index 0 automatically. No special case needed. 🔄 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(log n) 📦 Space: O(1) One modulo operation handles the entire circular edge case cleanly. This is binary search with a twist — and the wrap-around trick is worth remembering! 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gvK_p44b 11 more days. So close! 💪 #LeetCode #Day89of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
A pattern I keep seeing in Binary Tree problems... Most tree problems are not asking for tree knowledge. They are asking where the answer needs to be built. If the answer depends on child nodes first Think: Bottom-Up / Postorder Examples: - Height of tree - Diameter - Balanced tree - Max path sum - Delete/Clean Up The parent cannot make a decision until both child nodes report back. This is: Left -> Right -> Root Let the child nodes bring their answers first. If the answer needs parent context to flow downwards Think: Top-Down / Preorder Examples: - Root to leaf paths - Path sum - Pass level/depth - Carry prefix sum This is: Root -> Left -> Right The parent needs to send information downwards. The pattern I now ask myself is: Does this node need information from below, or does it need to send information downwards? This pattern alone seems to give away which order to traverse. #DSA #BinaryTree #ProblemSolving #BuildInPublic #Java #learning
To view or add a comment, sign in
-
Some problems break the standard sliding window pattern — especially when negative numbers are involved. 🚀 Day 104/365 — DSA Challenge Solved: Shortest Subarray with Sum at Least K Problem idea: We need to find the length of the shortest subarray whose sum is at least k. The challenge is that the array can contain negative numbers, so normal sliding window won't work. Efficient approach: Use Prefix Sum + Monotonic Deque. Steps: 1. Build a prefix sum array 2. Use a deque to store indices of useful prefix sums 3. While current sum − smallest prefix ≥ k → update answer 4. Maintain increasing order in deque by removing larger prefix sums from the back 5. This ensures we always get the shortest valid subarray This approach efficiently handles negative numbers while keeping optimal time complexity. ⏱ Time: O(n) 📦 Space: O(n) Day 104/365 complete. 💻 261 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #Deque #PrefixSum #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
“Most people try to overcomplicate this one… but the simplest approach wins.” Day 69 — LeetCode Progress Problem: Height Checker Required: Given an array of student heights, return the number of indices where the heights are not in the expected non-decreasing order. Idea: If we sort the array, we get the expected order. Now just compare the original array with the sorted version — mismatches are the answer. Approach: Create a copy of the original array Sort the copied array Traverse both arrays: Compare elements at each index If they differ → increment count Return the count Time Complexity: O(n log n) Space Complexity: O(n) #LeetCode #DSA #Java #Arrays #Sorting #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟵𝟯/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟲𝟬𝟴. 𝗦𝗽𝗲𝗰𝗶𝗮𝗹 𝗔𝗿𝗿𝗮𝘆 𝗪𝗶𝘁𝗵 𝗫 𝗘𝗹𝗲𝗺𝗲𝗻𝘁𝘀 𝗚𝗿𝗲𝗮𝘁𝗲𝗿 𝗧𝗵𝗮𝗻 𝗼𝗿 𝗘𝗾𝘂𝗮𝗹 𝗫 | 🟢 Easy | Java A self-referential condition — x elements must be ≥ x. Elegant problem, elegant solution. 🎯 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Find x such that exactly x elements in the array are ≥ x. Return -1 if no such x exists. ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗙𝗿𝗲𝗾𝘂𝗲𝗻𝗰𝘆 𝗖𝗼𝘂𝗻𝘁 + 𝗦𝘂𝗳𝗳𝗶𝘅 𝗦𝘂𝗺 ✅ Cap all values at n (array length) — anything larger contributes the same way ✅ Build a frequency count array of size n+1 ✅ Traverse from right to left, accumulating a running suffix sum ✅ When suffix sum == current index i → x = i is the answer! 💡 𝗪𝗵𝘆 𝗰𝗮𝗽 𝗮𝘁 𝗻? x can never exceed n (can't have more elements than the array size). So values above n are equivalent — capping them avoids index overflow and keeps the logic clean. 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(n) — two passes 📦 Space: O(n) — frequency array No sorting. No binary search. Just a clever frequency count + suffix accumulation. Sometimes the cleanest approach is right under your nose. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gm2c4-6x 𝟳 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗦𝗼 𝗰𝗹𝗼𝘀𝗲 𝘁𝗼 𝟭𝟬𝟬! 💪 #LeetCode #Day93of100 #100DaysOfCode #Java #DSA #Arrays #FrequencyCount #CodingChallenge #Programming
To view or add a comment, sign in
-
Day 74 of #100DaysOfLeetCode 💻✅ Solved #162. Find Peak Element problem in Java. Approach: • Used Binary Search technique to efficiently find the peak element • Set two pointers, left at start and right at end of the array • Calculated mid index using safe mid formula • Compared nums[mid] with nums[mid + 1] to determine direction • If mid element is smaller, moved search space to right half • Otherwise, moved search space to left half including mid • Continued until left and right pointers converged • Final position (left == right) represents the peak index Performance: ✓ Runtime: 0 ms (Beats 100.00% submissions) 🚀 ✓ Memory: 44.32 MB (Beats 25.49% submissions) Key Learning: ✓ Strengthened understanding of Binary Search on unsorted arrays ✓ Learned how to apply divide-and-conquer beyond traditional searching ✓ Improved intuition for peak finding using neighbor comparison ✓ Practiced optimizing search space instead of linear scanning Learning one problem every single day 🚀 #Java #LeetCode #DSA #BinarySearch #Arrays #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Some of the hardest problems become manageable once you recognize a repeating pattern. 🚀 Day 105/365 — DSA Challenge Solved: Subarrays with K Different Integers Problem idea: We need to count subarrays that contain exactly k distinct integers. Efficient approach: Use the powerful trick: subarrays with exactly k distinct = subarrays with ≤ k distinct − subarrays with ≤ (k − 1) distinct Steps: 1. Use a sliding window with a hashmap to track frequency of elements 2. Expand window by moving right pointer 3. If distinct count exceeds k, shrink window from the left 4. Count valid subarrays ending at each index 5. Subtract results to get exact count This pattern converts a hard problem into a manageable one. ⏱ Time: O(n) 📦 Space: O(n) Day 105/365 complete. 💻 260 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #HashMap #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟒 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding the minimum element in a rotated sorted array using binary search. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Find Minimum in Rotated Sorted Array 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Used binary search to reduce the search space • Compared the middle element with the rightmost element Logic: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) • Continued until left meets right 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Rotated arrays require a modified binary search • Comparing with boundary elements helps identify the sorted half • Binary search is not only for exact matches • Reducing the search space is the core idea 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(log n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Binary search is about asking the right question — not just finding the middle. 54 days consistent 🚀 On to Day 55. #DSA #Arrays #BinarySearch #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
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