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?
- A. O(n^2) double loop
- B. O(n^3) triple loop
- C. Kadane's O(n) scan
- 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
- char s = "hello"; What is strlen(s + 2)?
- int x = 1, y = 2, z = 3; int ptrs[3] = {&x, &y, &z}; What is ptrs[1] + ptrs[2]?
- printf("%d", 0x7FFFFFFF == 2147483647); What is printed?
- unsigned char c = 0xFF; unsigned int u = c << 8 | c; printf("%X", u); What is printed?
- static int total = 0; int add(int n) { static int t = 0; t += n; return t; } Calls add(3), add(4) and add(5) in that order. What does the…
- int r = 0; switch (2) { case 1: r += 1; case 2: r += 10; case 3: r += 100; break; default: r += 1000; } What is r?