Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with 1s for LIS calculation.
DSA C++
std::vector<int> dp(n, [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp with 0 causes incorrect LIS length calculation.
Using n or -1 as initial values is incorrect for LIS.
✗ Incorrect
Each element is at least an increasing subsequence of length 1 by itself, so dp is initialized with 1.
2fill in blank
mediumComplete the code to check if current element can extend the subsequence ending at j.
DSA C++
if (arr[i] [1] arr[j]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using >= or <= allows non-increasing sequences.
Using < instead of > reverses the logic.
✗ Incorrect
For LIS, we check if current element is greater than previous to extend the subsequence.
3fill in blank
hardFix the error in updating dp[i] when a longer subsequence is found.
DSA C++
dp[i] = std::max(dp[i], dp[[1]] + 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp[i] + 1 causes no change or infinite loop.
Using dp[n] or dp[0] is out of context.
✗ Incorrect
We update dp[i] based on dp[j] where j < i and arr[i] > arr[j].
4fill in blank
hardFill both blanks to complete the nested loops for LIS calculation.
DSA C++
for (int [1] = 1; [1] < n; [1]++) { for (int [2] = 0; [2] < [1]; [2]++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using same variable name for both loops causes errors.
Using indices out of range breaks the logic.
✗ Incorrect
Outer loop uses i from 1 to n-1, inner loop uses j from 0 to i-1 to compare elements.
5fill in blank
hardFill all three blanks to find the maximum LIS length after dp is computed.
DSA C++
int max_len = 0; for (int [1] = 0; [1] < n; [1]++) { max_len = std::max(max_len, dp[[2]]); } return [3];
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop and dp access causes errors.
Returning dp[i] instead of max_len returns partial result.
✗ Incorrect
We iterate i from 0 to n-1, update max_len with dp[i], and return max_len.
