Array Rotation | C++
Problem Statement:
The problem statement basically gives you an array of size n and asks you to rotate it k times towards the right or the left
Example:
The Solution:
The following steps are going to describe what we are going to do in out code
- Create two integers n and k which represent the size of the array and the number of rotations respectively.
- Create an array of size n and insert values in it
- Use a while loop to iterate for k times i.e. the number of rotations
- Inside the while loop, create a variable temp and store the first element in it.
- Use a for loop to scan all the elements of the array and in it use a[i] = a[i+1]; (i.e. the first element will be replaced by the second, the second by the third and so on).
- Finally print the array to display the output.
Here's the code:
Output:


Comments
Post a Comment