ProgrammingVery hard

Which approach finds the maximum sum rectangle in an n x n int matrix with n = 500 within a second in C?

  1. A. Try every rectangle with four nested loops, about 6 x 10^10 steps
  2. B. Fix the top and bottom rows and run Kadane's algorithm on column sums, about n^3 = 1.25 x 10^8 steps
  3. C. Try every rectangle with two loops and sum each in O(n), about n^4 steps
  4. D. Sort all rectangles by sum, which is far more than n^4 work

Correct answer: B. Fix the top and bottom rows and run Kadane's algorithm on column sums, about n^3 = 1.25 x 10^8 steps

Fixing the row pair and applying Kadane to column sums gives O(n^3). Four nested loops cost about n^4 / 4, which is about 1.6 x 10^10 for n = 500.

More C Programming questions

Exams that ask this topic