Tight Band Maximum Sum Problem
Tight Band Maximum Sum Problem — ExecCode Medium DSA Practice
Solve the Tight Band Maximum Sum problem on ExecCode. Free online medium DSA practice in Two Pointers. Write and run code in Java, C++, Python — no signup required to run.
Problem description
You are given a non-negative integer array a. A tight band is a contiguous subarray whose largest value and smallest value differ by exactly 1. Return the maximum sum of any tight band. If no tight band exists, return 0. The sum can exceed 32-bit range, so use a 64-bit integer. You are given: a — non-negative integers Return the maximum tight-band sum as a 64-bit integer, or 0 when none exists. A subarray must be contiguous and non-empty. Values that are equal differ by 0, so they are not a tight band. An O(n) solution is required.
Examples
Input {'a': [1, 2, 1, 2]}; Output 6. Input {'a': [1, 2, 4]}; Output 3. Input {'a': [5, 5, 5]}; Output 0
Constraints
1 <= a.length <= 100000 0 <= a[i] <= 1000000000 The answer fits in a signed 64-bit integer
Practice Tight Band Maximum Sum free on ExecCode. Browse DSA problems, topic map, and placement guides.