ProgrammingVery hard
To find the maximum over 10^5 queries of ranges in a static array, which approach is fast enough?
- A. Scan each range, about 10^10 operations
- B. A sparse table built in O(n log n), then each query in O(1)
- C. Sort the whole array before each query, about 10^10 operations
- D. Recompute the maximum by recursion over all ranges, about 10^15 operations
Correct answer: B. A sparse table built in O(n log n), then each query in O(1)
A sparse table makes each query O(1) after an O(n log n) build, which is about 2 x 10^6 steps. Scanning costs up to 10^10.
More C Programming questions
- A program multiplies two n x n matrices with n = 500 in int, and the product entries reach about 10^12. What is the error?
- To answer 'how many times does x appear in a sorted array' in O(log n), which pair of searches is correct?
- A bit array for flags of 10^9 items is held as one byte per item. What is the memory use, and is it fast enough in typical limits?
- struct P { char a; int b; char c; }; printf("%zu", sizeof(struct P)); What is printed with 4-byte int and natural alignment?
- struct P { char a; int b; }; printf("%zu", offsetof(struct P, b)); What is printed with 4-byte int and natural alignment (stddef.h…
- struct R { int v; struct R next; }; struct R c = {3, NULL}, b = {2, &c}, a = {1, &b}; printf("%d", a.next->next->v); What is printed?