Maximum Absolute Sum of Any Subarray
Maximum Absolute Sum of Any Subarray is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 1D Array (Kadane's Algorithm for Max/Min Subarray) subpattern.
Problem statement
You are given an integer array nums. The absolute sum of a subarray [numsl, numsl+1, ..., numsr-1, numsr] is abs(numsl + numsl+1 + ... + numsr-1 + numsr).
Return the maximum absolute sum of any (possibly empty) subarray of nums.
Note that abs(x) is defined as follows: If x is a negative integer, then abs(x) = -x.
If x is a non-negative integer, then abs(x) = x.
Example 1: Input: nums = [1,-3,2,3,-4] Output: 5 Explanation: The subarray [2,3] has absolute sum = abs(2+3) = abs(5) = 5.
Example 2: Input: nums = [2,-5,1,-4,3,-2] Output: 8 Explanation: The subarray [-5,1,-4] has absolute sum = abs(-5+1-4) = abs(-8) = 8.
Constraints: 1 <= nums.length <= 105 -104 <= nums[i] <= 104
Topics and companies
Topics: nan.
Reported in interviews at nan.
How to practise Maximum Absolute Sum of Any Subarray on Thita.ai
Starter code is provided in C, C#, C++, dart, elixir, erlang, Go, Java, JavaScript, Kotlin, PHP, Python, python3, racket, Ruby, Rust, Scala, Swift and TypeScript. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Maximum Absolute Sum of Any Subarray in the code editor, or read the Maximum Absolute Sum of Any Subarray editorial for a worked solution with its approach and complexity analysis.
Where Maximum Absolute Sum of Any Subarray sits in the DSA pattern sheet
- Dynamic Programming (DP) Patterns pattern guide
- 1D Array (Kadane's Algorithm for Max/Min Subarray) subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Maximum Absolute Sum of Any Subarray
Other problems that use the same 1D Array (Kadane's Algorithm for Max/Min Subarray) and Dynamic Programming (DP) Patterns techniques:
- Maximum Product Subarray — Medium, same subpattern
- Maximum Subarray — Medium, same subpattern
- Maximum Sum Circular Subarray — Medium, same subpattern
- Best Time to Buy and Sell Stock — Easy, same pattern
- Best Time to Buy and Sell Stock II — Medium, same pattern
- Best Time to Buy and Sell Stock III — Hard, same pattern
Opening the problem
Progress is saved as you study.