LeetCode 1493: Longest Subarray of 1's After Deleting One Element

🚀 Day 17 of #75DaysofLeetCode LeetCode Problem Solved: Longest Subarray of 1's After Deleting One Element (Sliding Window) Today I solved LeetCode 1493 – Longest Subarray of 1's After Deleting One Element. This problem is a great example of applying the Sliding Window / Two Pointer technique to optimize array problems. 🔹 Problem Statement: Given a binary array nums, we must delete exactly one element and then find the longest non-empty subarray containing only 1's. 💡 Approach: The key idea is to maintain a sliding window that allows at most one 0 in the window. If the number of zeros exceeds one, we move the left pointer to shrink the window until the condition becomes valid again. Since we must delete one element, the final answer becomes: window_size - 1 ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(1) 💻 Java Implementation: class Solution { public int longestSubarray(int[] nums) { int left = 0; int zeroCount = 0; int maxLen = 0; for (int right = 0; right < nums.length; right++) { if (nums[right] == 0) { zeroCount++; } while (zeroCount > 1) { if (nums[left] == 0) { zeroCount--; } left++; } maxLen = Math.max(maxLen, right - left + 1); } return maxLen - 1; } } 📚 Practicing problems like this helps strengthen understanding of Sliding Window patterns, which are frequently asked in coding interviews. #LeetCode #Java #DSA #SlidingWindow #ProblemSolving #CodingPractice #SoftwareDevelopment

  • text

To view or add a comment, sign in

Explore content categories