![]() |
VOOZH | about |
Given an array of integers, the task is to insert an element at the end of the array.
Examples:
Input: arr[] = [10, 20, 30, 40], ele = 50
Output: [10, 20, 30, 40, 50]Input: arr[] = [], ele = 20
Output: [20]
We will use library methods like push_back() in C++, add() in Java and C#, append() in Python and push() in JavaScript.
Array before insertion 10 20 30 40 Array after insertion 10 20 30 40 50
Time Complexity: O(1)
Auxiliary Space: O(1)
To insert an element at the end of an array, we can simply add the new element at the nth index.
Array before insertion 10 20 30 40 Array after insertion 10 20 30 40 50
Time Complexity: O(1)
Auxiliary Space: O(1)