FastPrepMaximum Sum Desirable Subarray
Problems/Maximum Sum Desirable Subarray

Maximum Sum Desirable Subarray

Groww logoGroww● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The judged task matches the visible source at about 99%.

Given an integer array values and a non-negative integer k, call a non-empty contiguous subarray desirable when the absolute difference between its first and last values is exactly k.

Return the maximum sum of a desirable subarray as a decimal string. Return IMPOSSIBLE when no desirable subarray exists.

Function

maximumDesirableSubarraySum(values: long[], k: long) → String

Examples

Example 1

values = [10,20,30,40]k = 20return = "90"

The subarray [20,30,40] has endpoint difference 20 and sum 90, which is maximal.

Example 2

values = [-5,10,-2]k = 0return = "10"

Every one-element subarray has equal endpoints. The best one contains 10.

Example 3

values = [1,2,3,4]k = 5return = "IMPOSSIBLE"

No two endpoints differ by 5, and a one-element subarray differs by 0.

Constraints

  • The array contains between 1 and 200000 values.
  • Every array value is between -10^9 and 10^9.
  • k is between 0 and 10^9.
  • The maximum possible subarray sum fits in a signed 64-bit integer.

Source note: Author-written prompt excerpt retained for source fidelity.

More Groww problems

  • Sliding Window MaximumPHONE SCREEN · Seen Sep 2026
  • Best Time to Buy and Sell Stock IIONSITE INTERVIEW · Seen Sep 2026
  • Minimum Candies by RatingONSITE INTERVIEW · Seen Sep 2026
See Groww hiring insights
Saved
class Solution {
    public String maximumDesirableSubarraySum(long[] values, long k) {
        // Write your code here.
    }

}
Checking account…