Many array problems that involve maximizing a window can be solved using the sliding window technique. 🚀 Day 98/365 — DSA Challenge Solved: Max Consecutive Ones III Problem idea: We need to find the maximum number of consecutive 1's in a binary array if we are allowed to flip at most k zeros. Efficient approach: Use a sliding window that allows at most k zeros inside the window. Steps: 1. Expand the window by moving the right pointer 2. Count how many zeros are inside the window 3. If zero count becomes greater than k, move the left pointer until zeros ≤ k 4. Track the maximum window length This keeps the window valid while maximizing the number of 1's. ⏱ Time: O(n) 📦 Space: O(1) Day 98/365 complete. 💻 267 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
Maximizing Consecutive Ones with Sliding Window Technique
More Relevant Posts
-
Not all sliding window problems are about sums — sometimes the constraint is on the product, but the pattern still applies. 🚀 Day 103/365 — DSA Challenge Solved: Subarray Product Less Than K Problem idea: We need to count the number of subarrays where the product of all elements is strictly less than k. Efficient approach: Use a sliding window that maintains a valid product. Steps: 1. Expand the window by multiplying the current element 2. If product becomes ≥ k, shrink the window from the left 3. Keep dividing until product < k 4. Count all valid subarrays ending at each index This works because all numbers are positive, so the window can be adjusted greedily. ⏱ Time: O(n) 📦 Space: O(1) Day 103/365 complete. 💻 262 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
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
-
-
Linked list problems often test how well you can manipulate pointers without losing track. 🚀 Day 114/365 — DSA Challenge Solved: Swap Nodes in Pairs Problem idea: We need to swap every two adjacent nodes in a linked list without changing values, only pointers. Efficient approach: Use a dummy node and carefully adjust pointers in pairs. Steps: 1. Use a dummy node pointing to head 2. Maintain a pointer prev before the current pair 3. Identify two nodes: first and second 4. Swap them by updating pointers 5. Move prev forward to the next pair This ensures all pairs are swapped correctly. ⏱ Time: O(n) 📦 Space: O(1) Day 114/365 complete. 💻 251 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LinkedList #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
🎯 Number of 1 Bits 👍 MY APPROACH 1️⃣ Here, first of all I create a counter variable where we store the number of ones in the binary number of given n value. 2️⃣ Then, I use while loop with a condition n!=0. It means if n!=0 then loop continues, the loop will stop if n==0. 3️⃣ During execution of loop, I start deleting rightmost ones bit one by one by using (n & (n-1)) expression. 4️⃣ When we delete rightmost ones bit then side by side we also increase the counter variable so that we can count number of ones bit in the binary of n number. 5️⃣ Atlast, we return the counter variable to see the number of ones bit in the binary of n number. #LeetCode #LeetcodeSolution #cp_sheet #BitManipulation #Consistency #Java
To view or add a comment, sign in
-
-
Some problems become much easier when you reframe the question. 🚀 Day 107/365 — DSA Challenge Solved: Minimum Operations to Reduce X to Zero Problem idea: We need to remove elements from the left or right so that their sum equals x, with minimum operations. Efficient approach: Instead of removing elements, think in reverse: 👉 Find the longest subarray with sum = totalSum − x Steps: 1. Calculate total sum of the array 2. Set target = totalSum − x 3. Find the longest subarray with sum = target using sliding window 4. Result = total length − longest subarray length This converts the problem into a familiar sliding window problem. ⏱ Time: O(n) 📦 Space: O(1) Day 107/365 complete. 💻 258 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Day 69/75 — Reverse String Simple and clean two-pointer problem. Approach: • Use two pointers (start & end) • Swap characters • Move pointers inward Key idea: while (i < j) { char temp = s[i]; s[i] = s[j]; s[j] = temp; i++; j--; } Time Complexity: O(n) Space Complexity: O(1) Basic problem, but strengthens in-place thinking and pointer control. 69/75 🔁 #Day69 #DSA #TwoPointers #Strings #Java #LeetCode
To view or add a comment, sign in
-
-
🖤 Day 44 of Solving DSA : Today, I implemented a solution to generate Pascal’s Triangle, a classic problem that helps strengthen problem-solving and array manipulation skills. 🔺 What is Pascal’s Triangle? It is a triangular array where: The first and last elements of each row are always 1 Every middle element is the sum of two elements from the previous row Example: [1] [1, 1] [1, 2, 1] [1, 3, 3, 1] 💡 Approach I Used: 1️⃣ Initialize Result List Create a list to store all rows of the triangle. 2️⃣ Iterate for Each Row Loop from 0 to numRows - 1 to build each row. 3️⃣ Set First and Last Elements Each row starts and ends with 1. 4️⃣ Calculate Middle Elements Use values from the previous row: 👉 current[j] = prev[j-1] + prev[j] 5️⃣ Store Each Row Add the constructed row to the result list. #DSA #java #100dayschallenge #leetcode #consistency
To view or add a comment, sign in
-
-
Day 64/75 — Reverse Bits Today’s problem was about reversing the bits of a 32-bit integer. Approach: • Iterate through all 32 bits • Shift result left to make space • Extract last bit using (n & 1) • Add it to result • Right shift n Core idea: result <<= 1; result |= (n & 1); n >>= 1; Time Complexity: O(1) (fixed 32 iterations) Space Complexity: O(1) Bit manipulation getting sharper ⚡ 64/75 #Day64 #DSA #BitManipulation #Java #LeetCode
To view or add a comment, sign in
-
-
Day 66/75 — Climbing Stairs Revisited a classic DP problem today. Approach: • Recognize it as Fibonacci pattern • Ways(n) = Ways(n-1) + Ways(n-2) • Use space-optimized DP with two variables Key idea: int prev2 = 1; int prev1 = 1; for (int i = 2; i <= n; i++) { int current = prev1 + prev2; prev2 = prev1; prev1 = current; } return prev1; Time Complexity: O(n) Space Complexity: O(1) Simple problem, but very important for building DP intuition. 66/75 📈 #Day66 #DSA #DynamicProgramming #Java #LeetCode
To view or add a comment, sign in
-
-
Most substring problems can be solved efficiently using the sliding window technique. 🚀 Day 97/365 — DSA Challenge Solved: Longest Substring Without Repeating Characters Problem idea: We need to find the length of the longest substring that contains no duplicate characters. Efficient approach: Use a sliding window with two pointers. Steps: 1. Keep track of the last seen index of each character 2. Expand the window by moving the right pointer 3. If a character repeats inside the window, move the left pointer to the position after its last occurrence 4. Update the maximum window length This ensures the window always contains unique characters. ⏱ Time: O(n) 📦 Space: O(1) Day 97/365 complete. 💻 268 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
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