Bird
Raised Fist0
DSA Javaprogramming~20 mins

Array Deletion at Middle Index in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting middle element from an odd-length array
What is the output of the following Java code after deleting the middle element from the array?
DSA Java
public class Main {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50};
        int n = arr.length;
        int mid = n / 2;
        for (int i = mid; i < n - 1; i++) {
            arr[i] = arr[i + 1];
        }
        n = n - 1;
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
A10 20 40 50
B10 20 30 40 50
C10 20 30 50
D10 20 40 30
Attempts:
2 left
💡 Hint
Think about how elements after the middle index shift left by one position.
❓ Predict Output
intermediate
2:00remaining
Output after deleting middle element from an even-length array
What is the output of this Java code after deleting the middle element from an even-length array?
DSA Java
public class Main {
    public static void main(String[] args) {
        int[] arr = {5, 15, 25, 35, 45, 55};
        int n = arr.length;
        int mid = n / 2;
        for (int i = mid; i < n - 1; i++) {
            arr[i] = arr[i + 1];
        }
        n = n - 1;
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
A5 15 25 35 45 55
B5 15 25 35 55
C5 15 35 45 55
D5 15 25 45 55
Attempts:
2 left
💡 Hint
For even length, middle index is n/2, which element is removed?
🔧 Debug
advanced
2:00remaining
Identify the error in this array deletion code
What error will this Java code produce when trying to delete the middle element from the array?
DSA Java
public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        int n = arr.length;
        int mid = (n - 1) / 2;
        for (int i = mid; i <= n; i++) {
            arr[i] = arr[i + 1];
        }
        n = n - 1;
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
ACompilation error due to missing semicolon
BArrayIndexOutOfBoundsException
CPrints 1 2 4
DNo output, runs fine
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
🧠 Conceptual
advanced
1:30remaining
Effect of deleting middle element on array size and indexing
After deleting the middle element from an array of length n, which statement is true?
AArray size becomes n-1 and elements after middle shift left by one index
BArray size remains n but middle element is set to zero
CArray size becomes n-1 but elements after middle remain at same index
DArray size remains n and elements after middle shift right by one index
Attempts:
2 left
💡 Hint
Think about how arrays store elements continuously.
❓ Predict Output
expert
3:00remaining
Output after multiple deletions of middle element
What is the output of this Java code after deleting the middle element twice in a row?
DSA Java
public class Main {
    public static void main(String[] args) {
        int[] arr = {2, 4, 6, 8, 10, 12, 14};
        int n = arr.length;
        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 = n - 1;
        }
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
A2 4 6 10 12 14
B2 4 6 8 10 12 14
C2 4 6 12 14
D2 4 6 8 12 14
Attempts:
2 left
💡 Hint
Delete middle element once, then again on the new array.

Practice

(1/5)
1. What happens when you delete the middle element from an array of size 5?
int[] arr = {10, 20, 30, 40, 50};
Delete element at index 2 (middle). What is the resulting array?
easy
A. [10, 20, 40, 50]
B. [10, 20, 30, 40, 50]
C. [10, 20, 30, 50]
D. [20, 30, 40, 50]

Solution

  1. Step 1: Identify the middle index and element

    The array size is 5, so middle index is 2 (0-based). Element at index 2 is 30.
  2. Step 2: Remove the element and shift remaining

    Remove 30, then shift elements after index 2 left: 40 moves to index 2, 50 moves to index 3.
  3. Final Answer:

    [10, 20, 40, 50] -> Option A
  4. Quick Check:

    Middle element 30 removed, array shrinks by one [OK]
Hint: Remove middle, shift right elements left by one [OK]
Common Mistakes:
  • Not shifting elements after deletion
  • Deleting wrong index (not middle)
  • Leaving array size unchanged
2. Which of the following Java code snippets correctly deletes the middle element from an array arr of odd length n?
easy
A. for (int i = n - 1; i > n/2; i--) arr[i] = arr[i - 1];
B. for (int i = 0; i < n/2; i++) arr[i] = arr[i + 1];
C. for (int i = n/2; i < n - 1; i++) arr[i] = arr[i + 1];
D. for (int i = 0; i < n; i++) arr[i] = arr[i + 1];

Solution

  1. Step 1: Understand the deletion index

    Middle index for odd length n is n/2 (integer division). We must shift elements after this index left.
  2. Step 2: Check loop correctness

    Loop from n/2 to n-2 shifts elements left by copying arr[i+1] to arr[i]. This correctly removes middle element.
  3. Final Answer:

    for (int i = n/2; i < n - 1; i++) arr[i] = arr[i + 1]; -> Option C
  4. Quick Check:

    Shift elements after middle left [OK]
Hint: Shift elements from middle index to end left by one [OK]
Common Mistakes:
  • Looping from 0 instead of middle index
  • Shifting elements right instead of left
  • Loop running out of array bounds
3. What is the output of the following Java code?
int[] arr = {5, 10, 15, 20, 25, 30, 35};
int n = arr.length;
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++) {
    System.out.print(arr[i] + " ");
}
medium
A. 5 10 15 20 25 35
B. 5 10 15 25 30 35
C. 5 10 15 20 25 30 35
D. 5 10 15 20 25 30

Solution

  1. Step 1: Identify middle index and element

    Array length n=7, middle index mid=3, element at index 3 is 20.
  2. Step 2: Shift elements left from middle

    Loop shifts elements from index 3 to 5 left by one: arr[3]=arr[4]=25, arr[4]=arr[5]=30, arr[5]=arr[6]=35.
  3. Step 3: Decrease size and print

    n is decremented to 6, so print first 6 elements: 5 10 15 25 30 35.
  4. Final Answer:

    5 10 15 25 30 35 -> Option B
  5. Quick Check:

    Middle element 20 removed, array shifted left [OK]
Hint: Shift elements after middle left, reduce size by one [OK]
Common Mistakes:
  • Printing full original array length
  • Not decrementing array size after deletion
  • Confusing middle index calculation
4. Find the error in this code that tries to delete the middle element from an array arr of length n:
int mid = n / 2;
for (int i = mid; i < n; i++) {
    arr[i] = arr[i + 1];
}
n--;
medium
A. Loop should start from 0, not mid
B. Middle index calculation is wrong for odd length arrays
C. Array size n should not be decremented
D. Loop runs out of array bounds accessing arr[i+1] when i = n-1

Solution

  1. Step 1: Analyze loop range and array access

    Loop runs from i = mid to i = n-1. At i = n-1, arr[i+1] = arr[n] is accessed, which is out of bounds.
  2. Step 2: Correct loop boundary

    Loop should run until i < n-1 to avoid out-of-bounds access when shifting elements left.
  3. Final Answer:

    Loop runs out of array bounds accessing arr[i+1] when i = n-1 -> Option D
  4. Quick Check:

    Array index out of bounds error [OK]
Hint: Loop till n-1, not n, to avoid arr[i+1] error [OK]
Common Mistakes:
  • Using wrong loop end condition
  • Ignoring array index limits
  • Incorrect middle index for even length arrays
5. Given an array arr = {2, 4, 6, 8, 10, 12} of even length 6, how do you delete the middle element(s) to keep the array balanced? Choose the correct resulting array after deleting the middle element(s).
hard
A. [2, 4, 8, 10, 12]
B. [2, 4, 6, 10, 12]
C. [2, 4, 8, 12]
D. [2, 4, 6, 8, 10]

Solution

  1. Step 1: Identify middle elements for even length

    For length 6, middle indices are 2 and 3 (elements 6 and 8).
  2. Step 2: Delete one or both middle elements to keep balance

    Usually, delete one middle element to keep array balanced. Deleting element at index 2 (6) shifts elements left.
  3. Step 3: Resulting array after deleting element at index 2

    Array becomes [2, 4, 8, 10, 12] after shifting elements left and reducing size by one.
  4. Final Answer:

    [2, 4, 8, 10, 12] -> Option A
  5. Quick Check:

    Delete one middle element, shift left, size reduces [OK]
Hint: For even length, delete lower middle index element [OK]
Common Mistakes:
  • Deleting both middle elements instead of one
  • Deleting wrong middle index
  • Not shifting elements after deletion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard