Kadane Algorithm with One Deletion for Maximum Subarray Sum

šŸš€ Day 82 — Kadane’s Algorithm (Maximum Subarray Sum with One Deletion) Pushing forward with a more advanced Kadane variation — today I solved a problem where you’re allowed to deleteĀ at most one elementĀ from the subarray to maximize the sum. šŸ“ŒĀ Problem Solved: LeetCode 1186 – Maximum Subarray Sum with One Deletion 🧠 The Twist on Standard Kadane: We now need to trackĀ two statesĀ at each index: ndĀ = maximum subarray sum ending atĀ iĀ withĀ no deletionĀ used so far. odĀ = maximum subarray sum ending atĀ iĀ withĀ exactly one deletionĀ used so far. Transition logic: nd = Math.max(arr[i], nd + arr[i]) → classic Kadane (no deletion). od = Math.max(nd_prev, od_prev + arr[i]) → either skip deleting this element (so use previous no‑deletion state) or extend a previously deleted subarray. Why track both? Because at any point, the best subarray ending here may have never deleted, or may have deleted a previous element. The answer is the max of allĀ ndĀ andĀ odĀ across the array. šŸ’”Ā Key Insight: Kadane’s pattern isn’t just one variable — it scales to multiple states. This ā€œstate‑based DPā€ approach is the bridge between Kadane and more complex subarray problems. No guilt about past breaks — just leveling up one variation at a time. #DSA #KadaneAlgorithm #MaximumSubarrayWithDeletion #LeetCode1186 #CodingJourney #Revision #Java #ProblemSolving #Consistency #GrowthMindset #TechCommunity #LearningInPublic

To view or add a comment, sign in

Explore content categories