ProgrammingVery hardGATE CS · Campus placements

Which algorithm finds the maximum subarray sum of an array of n = 10^6 ints in time within a typical 1 second contest limit?

  1. A. O(n^2) double loop
  2. B. O(n^3) triple loop
  3. C. Kadane's O(n) scan
  4. D. O(n^2) merge recursion

Correct answer: C. Kadane's O(n) scan

Kadane's algorithm scans once and keeps the best sum ending at each position, which is O(n). The quadratic and cubic options do about 10^12 or more operations on this input.

More C Programming questions

Exams that ask this topic