Given an array of length say N, move some specified amount of elements say X to the end of the given array.
arr = [1, 2, 3, 4, 5] X = 2
Output: The following array should be the output as the first two numbers are moved to the end of the array.
[3, 4, 5, 1, 2]
- First, we will extract first X elements from the array into a new array arr1.
- Then extract the last (N-X) elements from the array into a new array arr2.
- Then concatenate arr1 after arr2 to get the resulting array.
[ 6, 1, 2, 3, 4, 5 ]
- Run a for loop from index i = 0 till X-1
- In each iteration take the element at the current index and append it at the end of the array.
[ 3, 4, 5, 1, 2 ]
My Personal Notes arrow_drop_up
Please Login to comment...