ProgrammingModerate
A counting pass over an array of 10^7 values, each in [0, 9], uses 10 counters. Which statement is correct?
- A. It needs 10^7 counters, one per value
- B. One pass with 10 counters is O(n), which is fast enough in C
- C. It must sort the values first, which is O(n^2)
- D. It is impossible because the counters are not enough
Correct answer: B. One pass with 10 counters is O(n), which is fast enough in C
Each value increments one of the 10 counters, so a single pass gives the answer in O(n) time and O(1) memory.
More C Programming questions
- Which output method is fast enough for 10^6 lines of output in C?
- Which 2D DP table declaration is correct for a 1000 x 1000 table of ints in a contest C program?
- A modular sum over 10^6 terms uses sum = (sum + a[i]) % MOD with a[i] < MOD and sum an int. What is wrong?
- A counting loop uses the expression i i <= n with int i and n up to 2^31 - 1. Is the loop correct?
- To read a whole line of unknown length in standard C without overflow, which approach is correct?
- A program sums the digits of 10^5 numbers with one loop per number that divides by 10. What is the total cost, about?