rotation of array in c 2b 2b

Solutions on MaxInterview for rotation of array in c 2b 2b by the best coders in the world

showing results for - "rotation of array in c 2b 2b"
Emely
04 Aug 2018
1// C program to rotate an array cyclically
2
3#include <stdio.h>
4
5void rightRotateByOne(int arr[], int n) //function for cyclically rotating an array once
6{
7   int x = arr[n-1], i;
8   for (i = n-1; i > 0; i--)
9      arr[i] = arr[i-1];
10   arr[0] = x;
11}
12
13int main()
14{int t;
15scanf("%d",&t);//number of test cases
16int p;
17for(p=0;p<t;p++){
18    int n,i,k;
19    scanf("%d %d",&n,&k); // n--> size of array ; k--> number of rotations
20    int arr[n];
21    k=k%n;
22    for(i=0;i<n;i++){
23        scanf("%d",&arr[i]);
24    }
25int j;
26 for(j=0;j<k;j++) //cyclically rotating an array k times
27{rightRotateByOne(arr, n);}
28
29
30    for (i = 0; i < n; i++){
31        printf("%d ", arr[i]);}
32        printf("\n");}
33
34    return 0;
35}
36
Alberto
12 Jan 2018
1List<int> iList = new List<int>(); 
2
3    private void shift(int n)
4    {
5        for (int i = 0; i < n; i++)
6        {
7            iList.Add(iList[0]);
8            iList.RemoveAt(0);
9        }
10
11
12    }
13
Marion
17 Feb 2020
1Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7
21) Store the first d elements in a temp array
3   temp[] = [1, 2]
42) Shift rest of the arr[]
5   arr[] = [3, 4, 5, 6, 7, 6, 7]
63) Store back the d elements
7   arr[] = [3, 4, 5, 6, 7, 1, 2]
similar questions
queries leading to this page
rotation of array in c 2b 2b