Why should Kadane initialize with nums[0] instead of 0?

Practice this question from Maximum Subarray - Kadane Pattern Practice Quiz, with options and an explanation.

Question

Why should Kadane initialize with nums[0] instead of 0?

Option A

Because the answer may be all elements

Option B — Correct answer

Because an all-negative array should return the largest element

Option C

Because arrays are always sorted

Option D

Because zero is always part of the input

Explanation

The problem requires a non-empty subarray, so all-negative arrays must still return the largest element.

Maximum Subarray - Kadane Pattern Practice Quiz

All questions · Previous question · Next question

Open on QuizMaker