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
  1.  Create two integers n and k which represent the size of the array and the number of rotations respectively.
  2. Create an array of size n and insert values in it
  3. Use a while loop to iterate for k times i.e. the number of rotations
  4. Inside the while loop, create a variable temp and store the first element in it.
  5. 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).
  6. Finally print the array to display the output.

Here's the code:

Output:



Comments