Array Rotation in C

Array Rotation in C Programming Language

Array Rotation in C can be of 2 types

1. Right Rotation of elements
           i.e moving the elements of array to the right side or clockwise direction.

2. Left Rotation of elements
           i.e moving the elements of array to the left side or anticlockwise direction.

In this page we will look into a coding question that will clear the concept how we can do the rotation of array in C programming Language.
 
array rotation in C

Problem Statement

Write a program in C program language where you need to do Array Rotation as per the input of question.

Input Format
    –
First line predicts the total number of elements present int the array.
    -Second line contains the elements of array
    -Third line contains the number of time rotation need to be done
    -Fourth line contains the character ‘L’ or ‘R’ that defines what type of rotation needs to be done.

Output Format
   
-Output contains only 1 line that is the resultant output

array rotation representation prepinsta

Sample Test Case : 1
Input 
    5
    1 2 3 4 5
    4
    L

Output
    5 1 2 3 4

Rotation Test Case 1

Sample Test Case : 2
Input
    7
    10 11 12 13 14 15 16
    4
    R

Output
    13 14 15 16 10 11 12

Rotation Test Case 2
#include <stdio.h>

int rotLeft(int arr[], int nint d) {     int ij;    int first;     for(i=0i<di++)     {         first = arr[0];         for(j=0j<n-1j++)         {             arr[j] = arr[j+1];         }         arr[j] = first;     }     return *arr; } int rotRight(int arr[], int nint d) {     int ij;     int first;     for(i=0i<di++)     {         first = arr[n-1];         for(j=n-1j>=0j--)         {             arr[j] = arr[j-1];         }         arr[0] = first;     }     return *arr; } int main() {     int ndi;     char choice;     scanf("%d",&n);     int list[n];     for(i=0i<ni++)     {         scanf("%d",&list[i]);     }     scanf("%d",&d);     scanf(" %c",&choice);         if(choice=='L')     {         rotLeft(listnd);         for(i=0i<ni++)         {             printf("%d ",list[i]);         }     }     else if(choice=='R')     {         rotRight(listnd);         for(i=0i<ni++)         {             printf("%d ",list[i]);         }     }     else     {         printf("Not a valid choice");     }     return 0;    }

Learn About Arrays in C

Click on the below button to learn about in C Programming Language

Learn Arrays in C

Learn Data Structures

Click on the below button to study more Data Structure

Learn Data Structures