ProgrammingHard
Which 2D DP table declaration is correct for a 1000 x 1000 table of ints in a contest C program?
- A. int dp[1001][1001]; declared inside main as a local
- B. int dp[1000][1000]; with indices running to 1000
- C. int dp[1000001]; indexed as dp[i][j] with i, j in range
- D. static int dp[1001][1001];
Correct answer: D. static int dp[1001][1001];
A static array of 1001 x 1001 ints is about 4 MB and lives outside the stack. The local version can overflow the stack, and the 1000 size lacks index 1000.
More C Programming questions
- 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?
- A queue for BFS on a grid of 10^6 cells uses a fixed array of size 10^6 indexed by head and tail. Is this enough?
- To find the maximum over 10^5 queries of ranges in a static array, which approach is fast enough?