Be the first user to complete this post
|
Add to List |
329. Rotate the given array in cycles
Objective: Given an array of integers, write a program to rotate the array in cyclic manner by one.
Example:
Int a [] = {1, 2, 3, 4, 5} Output: {2, 3, 4, 5, 1}
Approach:
- Store the first element of array in temporary variable.
- From 2nd element to last element, left shift all the elements by one position.
- Put the temporary variable value at the last index of the array.
- See the image below for better understanding.

Original Array: [1, 2, 3, 4, 5] Rotated Array: [2, 3, 4, 5, 1] __________________________ Original Array: [2, 3, 4, 5, 1] Rotated Array: [3, 4, 5, 1, 2]