Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the length array for LIS calculation.
DSA C#
int[] lengths = new int[nums.Length]; for (int i = 0; i < nums.Length; i++) { lengths[i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing lengths with 0 instead of 1.
Using the element value instead of 1.
✗ Incorrect
Each element starts with a length of 1 because the smallest increasing subsequence including that element is the element itself.
2fill in blank
mediumComplete the code to check if the current element can extend the subsequence ending at j.
DSA C#
for (int i = 1; i < nums.Length; i++) { for (int j = 0; j < i; j++) { if (nums[i] [1] nums[j]) { lengths[i] = Math.Max(lengths[i], lengths[j] + 1); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= or >= which allows non-increasing sequences.
Using < which checks for decreasing sequences.
✗ Incorrect
We want to find increasing subsequences, so nums[i] must be greater than nums[j] to extend the subsequence.
3fill in blank
hardFix the error in the code to find the maximum length of LIS after filling lengths array.
DSA C#
int maxLength = 0; for (int i = 0; i < lengths.Length; i++) { if (lengths[i] [1] maxLength) { maxLength = lengths[i]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which finds minimum instead of maximum.
Using '==' which only updates if equal.
✗ Incorrect
We update maxLength only if lengths[i] is greater than the current maxLength to find the maximum value.
4fill in blank
hardFill both blanks to complete the code that returns the length of the longest increasing subsequence.
DSA C#
public int LengthOfLIS(int[] nums) {
int[] lengths = new int[nums.Length];
for (int i = 0; i < nums.Length; i++) {
lengths[i] = 1;
}
for (int i = 1; i < nums.Length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] [1] nums[j]) {
lengths[i] = Math.Max(lengths[i], lengths[j] + 1);
}
}
}
return lengths.[2]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Sum() instead of Max() to find the longest subsequence.
Using '<' instead of '>' in the condition.
✗ Incorrect
We check if nums[i] is greater than nums[j] to extend the subsequence, and return the maximum value in lengths array.
5fill in blank
hardFill all three blanks to complete the code that reconstructs the longest increasing subsequence.
DSA C#
public List<int> GetLIS(int[] nums) {
int n = nums.Length;
int[] lengths = new int[n];
int[] previous = new int[n];
for (int i = 0; i < n; i++) {
lengths[i] = 1;
previous[i] = -1;
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] [1] nums[j] && lengths[j] + 1 > lengths[i]) {
lengths[i] = lengths[j] + 1;
previous[i] = j;
}
}
}
int maxIndex = 0;
for (int i = 1; i < n; i++) {
if (lengths[i] [2] lengths[maxIndex]) {
maxIndex = i;
}
}
List<int> lis = new List<int>();
for (int i = maxIndex; i != [3]; i = previous[i]) {
lis.Add(nums[i]);
}
lis.Reverse();
return lis;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in comparisons.
Using 0 instead of -1 as the stopping condition.
✗ Incorrect
We check if nums[i] > nums[j] to extend subsequence, update maxIndex if lengths[i] > lengths[maxIndex], and stop reconstruction when previous index is -1.
