Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the count array with zeros.
DSA Java
int[] count = new int[max + 1]; for (int i = 0; i < count.length; i++) { count[i] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing count array with 1 instead of 0.
Using loop variable 'i' as initial value.
✗ Incorrect
The count array must start with zeros to correctly count occurrences.
2fill in blank
mediumComplete the code to count the occurrences of each element in the input array.
DSA Java
for (int i = 0; i < arr.length; i++) { count[arr[i]] [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator -- instead of increment.
Assigning values instead of incrementing.
✗ Incorrect
We increment the count for each element found in the input array.
3fill in blank
hardFix the error in updating the count array to hold cumulative counts.
DSA Java
for (int i = 1; i < count.length; i++) { count[i] = count[i] [1] count[i - 1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using multiplication or division which is incorrect here.
✗ Incorrect
Cumulative count is found by adding the previous count to the current count.
4fill in blank
hardFill both blanks to place elements into the output array in sorted order.
DSA Java
for (int i = arr.length - 1; i >= 0; i--) { output[count[arr[i]] [1]] = arr[i]; count[arr[i]] [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using +1 instead of -1 for index.
Incrementing count instead of decrementing.
✗ Incorrect
We use count[arr[i]] - 1 as index and then decrement count[arr[i]] after placing the element.
5fill in blank
hardFill the two blanks to copy the sorted elements back to the original array.
DSA Java
for (int i = 0; i < arr.length; i++) { arr[i] = output[[1]]; [2]++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using loop variable i instead of index for output access.
Not incrementing the index variable.
✗ Incorrect
We use an index variable to track position in output, increment it after copying each element.
