2

I read the question in Exercise 4.1-4 in Introduction To Algorithms:

Suppose we change the definition of the maximum-subarray problem to allow the result to be an empty subarray, where the sum of the values of an empty subarray is 0. How would you change any of the algorithms that do not allow empty subarrays to permit an empty subarray to be the result?

I cannot get what's an empty sub-array.

I came across the point that a single number can be returned if the array consists of negative elements only.

Please can anyone explain the concept of an empty sub-array? And how can we have an empty sub-array?

Even if a single element is returned it still means that sub-array is not empty. Please clear the doubt.

Edit:

To make it more clear as a question if I take an array of elements:

[-3,-4,-1,-8]

The answer would be -1 or 0? Please explain if why it should be 0 and how can we conclude an empty sub-array.

Thank you.

Sachin Bahukhandi
  • 247
  • 1
  • 3
  • 13

2 Answers2

2

A subarray of length zero is empty.

Given an array $A[1],\ldots,A[n]$, a subarray is specified by a pair of indices $i \leq j$. These correspond to the subarray $A[i],\ldots,A[j]$ of length $j-i+1$. If we also allow $j = i-1$ then we get an empty subarray of length $j-i+1 = 0$, whose sum is zero.


In the maximum subarray problem, we are given an array $A[1],\ldots,A[n]$, and want to find a subarray whose sum is maximal. If we don't allow empty subarrays, this means that we are looking for the maximum value of $$ A[i] + \cdots + A[j], $$ where $1 \leq i \leq j \leq n$. If we are allowing empty subarrays, then we take the maximum of that with $0$, which is the sum of the empty subarray.

This only makes a difference if all entries of the array are negative. The maximum sum of a non-empty subarray is in this case the maximal element $A[i]$, which is the sum of the subarray $A[i]$ of length $1$. The empty subarray, however, has a larger sum: $0$. Therefore if the empty subarray is not allowed, the answer should be $\max_i A[i]$, and if it is allowed, the answer should be $0$.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
2

If you have this array: $[-2,-10,-5]$, and the problem specifies that you should return to it the sum of the maximum subarray whose length is at least $1$, you will return the sum of the subarray $[-2]$, which is $-2$. So far so good?

Now, focus here because this is where you are most probably having trouble:

The problem is now tweaked. The problem now allows you to return to it an empty subarray, which means, you can return to it a subarray that is empty - a subarray that has no elements. Bear with me:

In mathematics, an "empty sum" is a summation where the number of terms is zero. Verify.

Similarly, in computer science, an "empty subarray" is a subarray in which the number of terms is zero. This is just the definition. It's just a subarray whose sum evaluates to zero.

Now, concerning the tweaked version of the problem, what would be better, returning to it $[-2]$ whose sum evaluates to $-2$, or returning the empty subarray $( [ \ \ \ ] )$ whose sum evaluates to $0$?

einstein
  • 36
  • 3