Rotating right an array of int in c#?

right rotation of array in c
left rotation in c
c program to shift elements of array to right
left rotation of array in c++
array rotation in python
array left rotation hackerrank solution java
simple array rotation game hackerrank solution
right rotation of array in python

I've got an homework assignment:

need to implement a function (RotateRight) that gets an array of INT and a number:

int[] res = RotateRight(new int[] { 1, 2, 3, 4, 5, 6 }, 2);
//so then res will be {5,6,1,2,3,4}

and return the array after rotating all of the items to the right according to the number that been given, In our case 2.

And I have to do this efficiently in terms of memory space.

my best idea is:

if the number that been given is x, to use a new int[] tmpArray in the size of x to copy all the last x items to it. then with a for loop to shift all the rest of the int to the right. And in the end to copy the items in the tmpArray to the begining of the original array.

Thanks in advance for any advice or help

You can use the beauty of the Linq langage to return an IEnumerable without dealing with array size:

/// <summary>
/// Get c = a mod (b) with c in [0, b[ like the mathematical definition
/// </summary>
public static int MathMod(int a, int b)
    int c = ((a % b) + b) % b;
    return c;
public static IEnumerable<T> ShiftRight<T>(IList<T> values, int shift)
    for (int index = 0; index < values.Count; index++)
        yield return values[MathMod(index - shift, values.Count)];

Usage :

public void TestMethod1()
    var res = ShiftRight(new [] { 1, 2, 3, 4, 5, 6 }, 2).ToArray();
    Assert.IsTrue(res.SequenceEqual(new[] { 5, 6, 1, 2, 3, 4 }));

Java Program to right rotate the elements of an array, Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. Array. Rotation of Duration: 4:49 Posted: May 2, 2012 Array1: [1, 2, 8] After rotating array becomes: [2, 8, 1] Flowchart: C# Sharp Code Editor: Improve this sample solution and post your code through Disqus. Previous: Write a C# program to check if the first element or the last element of the two arrays ( length 1 or more) are equal.

Most memory possible makes no sense, you probably mean as little memory as possible? If so you should swap each item in the array using XOR, i.e:

var a = 2096;
var b = 842390;

a ^= b;
b ^= a;
a ^= b;

would swap these numbers.


Code to do the whole thing in place:

    public static void RotateRight(int[] input, int right)
        for (var i = 0; i < right; i += 1)

    public static void RotateRightOne(int[] input)
        var last = input.Length - 1;
        for (var i = 0; i < last; i += 1)
            input[i] ^= input[last];
            input[last] ^= input[i];
            input[i] ^= input[last];


    var arr = new[] {1, 2, 3, 4, 5, 6};
    RotateRight(arr, 2);

As Servy points out, this is only for integers

Program for array rotation, Given an array, cyclically rotate the array clockwise by one. C++. filter_none. edit close. play_arrow. link brightness_4 code void rotate( int arr[], int n) Please write comments if you find any bug in above code/algorithm, or find other ways  Given an array of n integers and a number, d, perform left rotations on the array. Then print the updated array as a single line of space-separated integers. Sample Input: The first line contains two space-separated integers denoting the respective values of n (the number of integers) and d (the number of left rotations you must perform).

Don't know C#, but here are two C++ versions, both in place, the first (rotate) does the minimum possible number of element moves by exploiting the cyclic structure of the rotation permutation, the second (rotate_k) just does 2*n moves for an array of length n. In both versions it's used that rotate right by k is the same as rotate left by n - k % n, so they in fact do the equivalent left rotation.

#include <iostream>
#include <vector>
#include <algorithm>

rotate (size_t k, std::vector<int> &a) {
    size_t n = a.size();
    k = n - k % n;

    size_t m = n;

    size_t i = 0;
    while (m > 0) {
        int t = a[i];
        size_t j = i;
        while (i != (j + k) % n) {
            a[j] = a[(j + k) % n];
            j = (j + k) % n;
        a[j] = t;

rotate_k (size_t k, std::vector<int> &a) {
    size_t n = a.size();

    k = n - k % n;

    std::reverse (a.begin(), a.end());
    std::reverse (a.begin(), a.begin() + n - k);
    std::reverse (a.begin() + n - k, a.end());

main () {
    std::vector<int> a = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
    rotate (12, a);

    for (auto i : a)
        std::cout << i << " ";

    std::cout << std::endl;

Program to cyclically rotate an array by one, C# Sharp exercises and solution: Write a C# program to rotate an array (length 3) of integers in left direction. Right Circular Rotation of an Array in C#: In the following example, first we take the input integer array from the user. Then we perform right circular rotation using for loop.

You just need to figure out the final index for each element after rotating it k times rather than actually rotating it k times. This worked for me:

for(int i=0;i<a.Length;i++){

C# Sharp exercises: Rotate an array of integers in left direction , C program to left rotate an array – In this article, we will brief in on the various means to int main(). {. int a[10000],i,n,j,k,temp;. printf("Enter size of the array : ");​. C# program to Rotate array to the right given a pivot. In this article, we will learn how to rotate an array to the right given a pivot. . Let’s have a look at the implementation in C#.

Here's a quick sample on rotating an array A right K steps:

    var splitPoint=A.Length-(K%A.Length);

    var result=new int[A.Length];
    int idx=0;
    for(var pos=0;pos<A.Length;pos++)

    return result;

C Program To Left Rotate An Array, In this post, we will see how to right rotate an array by specified positions. This is demonstrated below in C and Java – void rightRotate(int A[], int k, int n). Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes. Write a function:

Right rotate an array k times, Write a C function for rotating an array left (in-place rotation) that does not use a write a function void transpose(double **x, int n); which transposes the matrix. Block swap algorithm for array rotation; Program for array rotation; Circular rotation of an array using deque in C++; Left rotation of an array using vectors in C++; Left Rotation and Right Rotation of a String; Print left rotation of array in O(n) time and O(1) space; Find the Rotation Count in Rotated Sorted array

Scientific Programming and Computer Architecture, This C program is to shift the elements of an array in the left direction by one position int i,n,a[100],temp; printf("\nNew array after rotating by one postion in the left direction\n"); ⟵C program to shift elements of an array in the right direction. In this program, we need to rotate the elements of array towards its right by the specified number of times. An array is said to be right rotated if all elements of the array are moved to its right by one position. One approach is to loop through the array by shifting each element of the array to its next position.

C program to shift elements of an array in the left direction , C program to right rotate an array */ #include <stdio.h> #define SIZE 10 /* Size of the array */ void printArray(int arr[]); void rotateByOne(int arr[]);  Block swap algorithm for array rotation; Left Rotation and Right Rotation of a String; Find the Rotation Count in Rotated Sorted array; Maximum number by concatenating every element in a rotation of an array; Print left rotation of array in O(n) time and O(1) space; Sorting possible using size 3 subarray rotation

  • It sounds like your idea would achieve the desired result.
  • Have you tried actually implementing your idea?
  • If you passed 6 would you get back the original array? Is passing 7 the same as passing 1?
  • Andrew Walters - thanks for the comment, i've fixed my question Kevin DiTraglia - yes
  • Using a copy of the array is hardly "efficiently in terms of memory space". This can be done in-place.
  • Thanks! looks elegant
  • Note that for a right shift you can just use %. You only need modulus for a left shift.
  • Looks nice and in the real world probably the best answer but creates a brand new array so not efficient as it could be. If you want to do this in place (no need to create a new array) see my answer below
  • That's a neat trick, but can you figure out which pairwise switches to perform in order the cyclical shift?
  • Also note this option only works for integer arrays; it can't handle any other kind of array.
  • This is particulary slow for big shift. In fact, for a random shift, this is a O(n^2)
  • @Cyril: I'm aware of that, this is just homework with the requirement to use the least amount of memory possible... No one said anything about speed