ProgrammingHard
For each of Q = 10^5 queries, a program sums an array prefix of length up to N = 10^5 by a loop. What is the total cost?
- A. About 10^5 additions, which is fast enough
- B. About 10^10 additions, but in a 2-second limit it passes
- C. About 5 x 10^9 additions, which is too slow for a 1-second limit
- D. About 10^3 additions, since the prefix is short
Correct answer: C. About 5 x 10^9 additions, which is too slow for a 1-second limit
Each query costs up to N additions, so Q x N = 10^10 in the worst case and about 5 x 10^9 on average. Prefix sums reduce each query to O(1).
More C Programming questions
- A recurrence T(n) = T(n - 1) + n is solved by a program with a loop over the recursion. What is the total cost in terms of n?
- What is the total number of elements in a triangle of rows 1 to 10, where row k has k elements?
- A program runs a loop body 10^8 times and each iteration does one multiply and one add on ints. Roughly how long does this take on typical…
- A bit-packed bitset of 10^9 bits needs how much memory?
- Which loop is correct and fast enough to find the largest element of an array of 10^7 ints, reading each value once?
- A program must answer 10^6 queries 'is x present in the set S?' where S has 10^6 values fixed in advance. Which preprocessing and query…