
LeetCode 3689: Maximum Total Subarray Value I – Java Greedy Solution Explained
IntroductionLeetCode 3689, “Maximum Total Subarray Value I”, is an interesting medium-level array problem that focuses on maximizing the total value of selected subarrays. The challenge introduces an important observation-based greedy approach that simplifies the problem significantly.In this article, we will break down the problem statement, understand the intuition behind the solution, analyze the Java code step-by-step, and discuss the time and space complexity.Problem StatementTry this problem here :- Maximum Total Subarray Value IYou are given:An integer array numsAn integer kYou must choose exactly k non-empty subarrays from the array.The value of a subarray is defined as:Value = Maximum Element − Minimum ElementThe goal is to maximize the total value of all chosen subarrays.ExampleExample 1Input:nums = [1,3,2]k = 2Output:4Explanation:Subarray [1,3] → 3 - 1 = 2Subarray [1,3,2] → 3 - 1 = 2Total = 2 + 2 = 4Example 2Input:nums = [4,2,5,1]k = 3Output:12Explanation:The maximum possible subarray value is:5 - 1 = 4Choose this optimal subarray 3 times:4 + 4 + 4 = 12Key ObservationThe problem allows:Overlapping subarraysReusing the same exact subarray multiple timesThis changes everything.To maximize the total value:Find the maximum possible subarray value.Reuse that same subarray exactly k times.Now the question becomes:What is the maximum possible value of any subarray?Since a subarray’s value is:max(subarray) - min(subarray)The largest possible value is simply:global maximum element - global minimum elementSo the final answer becomes:(maxElement - minElement) * kJava Solutionclass Solution { public long maxTotalValue(int[] nums, int k) { long min = Long.MAX_VALUE; long max = Long.MIN_VALUE; long ans = 0; for(int i = 0; i < nums.length; i++) { min = Math.min(min, nums[i]); max = Math.max(max, nums[i]); } ans = max - min; return ans * k; }}Step-by-Step Dry RunInputnums = [4,2,5,1]k = 3Step 1: Find Minimum and MaximumTraverse the array:ElementCurrent MinCurrent Max444224525115Final:min = 1max = 5Step 2: Calculate Maximum Subarray Value5 - 1 = 4Step 3: Multiply by k4 * 3 = 12Final Answer:12Why This Greedy Approach WorksThe problem explicitly allows selecting:The same subarray multiple timesOverlapping subarraysSo once we identify the subarray with the highest possible value, there is no reason to choose anything else.The optimal strategy is:Choose the best subarray repeatedly k timesThis reduces the entire problem to finding:Maximum element - Minimum elementTime ComplexityTime ComplexityO(n)We traverse the array only once.Space ComplexityO(1)Only a few variables are used.Interview InsightsThis problem tests:Observation skillsGreedy thinkingAbility to simplify constraintsUnderstanding of problem flexibilityMany developers initially overcomplicate the problem using sliding window or dynamic programming approaches. However, the key insight is realizing that repeated subarrays are allowed.Final ThoughtsLeetCode 3689 is a great example of how carefully reading constraints can dramatically simplify a problem.Instead of generating all subarrays, the optimal solution comes from a simple mathematical observation:Maximum Total Value = (Global Max − Global Min) × kThis leads to an elegant and highly efficient O(n) solution.If you are preparing for coding interviews, this problem is an excellent exercise in greedy optimization and pattern recognition.














