Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting middle element from array
What is the output of the following C++ code that deletes the middle element from an array?
DSA C++
int arr[] = {10, 20, 30, 40, 50}; int n = 5; int mid = n / 2; for (int i = mid; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Remember that deleting the middle element shifts all elements after it one position left.
✗ Incorrect
The middle index is 2 (0-based). The element 30 at index 2 is removed. Elements after index 2 shift left, so 40 moves to index 2, 50 to index 3. The new array size is 4.
❓ Predict Output
intermediateResulting array after middle deletion with even length
What is the output of this C++ code deleting the middle element from an even-length array?
DSA C++
int arr[] = {5, 15, 25, 35, 45, 55}; int n = 6; int mid = n / 2; for (int i = mid; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
For even length, middle index is n/2, which is 3 here (0-based).
✗ Incorrect
Middle index is 3, element 35 is removed. Elements after index 3 shift left: 45 moves to index 3, 55 to index 4. New size is 5.
🔧 Debug
advancedIdentify the bug in middle deletion code
What error will this code produce when deleting the middle element from an array?
DSA C++
int arr[] = {1, 2, 3, 4}; int n = 4; int mid = n / 2; for (int i = mid; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The original loop condition was incorrect and caused out-of-bounds access. It has been corrected to i < n - 1 to avoid accessing arr[n].
🧠 Conceptual
advancedEffect of deleting middle element on array size
If you delete the middle element from an array of size n, what will be the size of the array afterwards?
Attempts:
2 left
💡 Hint
Deleting one element reduces the total count by one.
✗ Incorrect
Removing one element from an array reduces its size by exactly one, regardless of n being even or odd.
🚀 Application
expertFinal array state after multiple middle deletions
Given the array and code below, what is the final printed array after deleting the middle element twice in a row?
DSA C++
int arr[] = {2, 4, 6, 8, 10, 12, 14}; int n = 7; for (int count = 0; count < 2; count++) { int mid = n / 2; for (int i = mid; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; } for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Delete middle element once, then again on the smaller array.
✗ Incorrect
First deletion removes element at index 3 (8). Array becomes {2,4,6,10,12,14}, size 6.
Second deletion removes element at index 3 (10). Array becomes {2,4,6,12,14}, size 5.
Final output: 2 4 6 12 14
