Problem · Array
HardRubrik logoRubrikOA

Problem statement

The beauty of an integer array is the maximum sum of a contiguous subarray. Choosing no elements is allowed, so beauty is never negative.

You may choose one contiguous segment and multiply every value in that segment by z. This amplification may be used at most once.

Return the maximum beauty obtainable after the optional amplification.

Function

enhanceLuminescence(arr: int[], z: int) → long

Examples

Example 1

arr = [3, 2]z = 2return = 10

Amplifying the entire array produces [6, 4], whose sum is 10.

Example 2

arr = [-5, 9, -2, 1, -6]z = -3return = 30

Amplify [-2, 1, -6] to obtain [6, -3, 18]. The best subarray is then [9, 6, -3, 18], with sum 30.

Example 3

arr = [1, 2, 4, 5]z = -2return = 12

Skipping amplification keeps the full-array sum 12, which is optimal.

Constraints

  • 1 <= arr.length <= 2 * 10^5.
  • -10^9 <= arr[i] <= 10^9.
  • -100 <= z <= 100.

More Rubrik problems

drafts saved locally
public long enhanceLuminescence(int[] arr, int z) {
  // write your code here
}
arr[3, 2]
z2
expected10
checking account