Bird
0
0
DSA Cprogramming~5 mins

Array Deletion at End in DSA C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Array Deletion at End
O(1)
Understanding Time Complexity

We want to understand how fast it is to remove an element from the end of an array.

How does the time needed change as the array gets bigger?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


void deleteAtEnd(int arr[], int *size) {
    if (*size == 0) return; // no elements to delete
    (*size)--;
}
    

This code removes the last element by reducing the size count of the array.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Decreasing the size variable by one.
  • How many times: Happens once per deletion call, no loops or recursion.
How Execution Grows With Input

Removing the last element always takes the same small step, no matter how big the array is.

Input Size (n)Approx. Operations
101
1001
10001

Pattern observation: The time stays constant regardless of array size.

Final Time Complexity

Time Complexity: O(1)

This means deleting the last element takes the same short time no matter how big the array is.

Common Mistake

[X] Wrong: "Deleting from the end takes longer as the array grows because we have to move many elements."

[OK] Correct: We do not move any elements when deleting at the end; we just reduce the size count, so time stays the same.

Interview Connect

Knowing that deleting at the end is very fast helps you choose the right data structure and shows you understand how arrays work under the hood.

Self-Check

"What if we changed deletion to remove the first element instead? How would the time complexity change?"