原文:

给定一个大小为 narr【】和一个值 k,任务是。

示例:

输入: arr = {1,3,5,7,9},k = 2 输出: 7 9 1 3 5

输入: arr = {1,2,3,4,5},k = 4 t3】输出: 2 3 4 5 1

算法:给定的问题可以通过来解决。可以遵循以下步骤来解决问题:

  • 将所有数组元素从 1 反转到 n -1
  • 将数组元素从 1 反转到k–1
  • 将数组元素从 k 反转到 n -1

c

// c   implementation for the above approach
#include 
using namespace std;
// function to rotate the array
// to the right, k times
void rightrotate(int array[], int n, int k)
{
    // reverse all the array elements
    reverse(array, array   n);
    // reverse the first k elements
    reverse(array, array   k);
    // reverse the elements from k
    // till the end of the array
    reverse(array   k, array   n);
    // print the array after rotation
    for (int i = 0; i < n; i  ) {
        cout << array[i] << " ";
    }
    cout << endl;
}
// driver code
int main()
{
    // initialize the array
    int array[] = { 1, 2, 3, 4, 5 };
    // find the size of the array
    int n = sizeof(array) / sizeof(array[0]);
    // initialize k
    int k = 4;
    // call the function and
    // print the answer
    rightrotate(array, n, k);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

/*package whatever //do not write package name here */
import java.io.*;
class gfg
{
    // function to rotate the array
    // to the right, k times
    static void rightrotate(int[] array, int n, int k)
    {
        // reverse all the array elements
        for (int i = 0; i < n / 2; i  ) {
            int temp = array[i];
            array[i] = array[n - i - 1];
            array[n - i - 1] = temp;
        }
        // reverse the first k elements
        for (int i = 0; i < k / 2; i  ) {
            int temp = array[i];
            array[i] = array[k - i - 1];
            array[k - i - 1] = temp;
        }
        // reverse the elements from k
        // till the end of the array
        for (int i = 0; i < (k   n) / 2; i  ) {
            int temp = array[(i   k) % n];
            array[(i   k) % n] = array[(n - i   k - 1) % n];
            array[(n - i   k - 1) % n] = temp;
        }
        // print the array after rotation
        for (int i = 0; i < n; i  ) {
            system.out.print(array[i]   " ");
        }
        system.out.println();
    }
    // driver code
    public static void main(string[] args)
    {
        // initialize the array
        int array[] = { 1, 2, 3, 4, 5 };
        // find the size of the array
        int n = array.length;
        // initialize k
        int k = 4;
        // call the function and
        // print the answer
        rightrotate(array, n, k);
    }
}
// this code is contributed by maddler.

python 3

# python program for the above approach
import math
# function to rotate the array
# to the right, k times
def rightrotate(array, n, k):
    # reverse all the array elements
    for i in range(math.ceil(n / 2)):
        temp = array[i]
        array[i] = array[n - i - 1]
        array[n - i - 1] = temp
    # reverse the first k elements
    for i in range(math.ceil(k / 2)):
        temp = array[i]
        array[i] = array[k - i - 1]
        array[k - i - 1] = temp
    # reverse the elements from k
    # till the end of the array
    for i in range(math.ceil((k   n) / 2)):
        temp = array[(i   k) % n]
        array[(i   k) % n] = array[(n - i   k - 1) % n]
        array[(n - i   k - 1) % n] = temp
    # print the array after rotation
    for i in range(n):
        print(array[i], end=" ")
# driver code
arr = [1, 2, 3, 4, 5]
n = len(arr)
k = 4
# call the function and
# print the answer
rightrotate(arr, n, k)
# this code is contributed by saurabh jaiswal

c

// c# program for the above approach
using system;
class gfg
{
    // function to rotate the array
    // to the right, k times
    static void rightrotate(int []array, int n, int k)
    {
        // reverse all the array elements
        for (int i = 0; i < n / 2; i  ) {
            int temp = array[i];
            array[i] = array[n - i - 1];
            array[n - i - 1] = temp;
        }
        // reverse the first k elements
        for (int i = 0; i < k / 2; i  ) {
            int temp = array[i];
            array[i] = array[k - i - 1];
            array[k - i - 1] = temp;
        }
        // reverse the elements from k
        // till the end of the array
        for (int i = 0; i < (k   n) / 2; i  ) {
            int temp = array[(i   k) % n];
            array[(i   k) % n] = array[(n - i   k - 1) % n];
            array[(n - i   k - 1) % n] = temp;
        }
        // print the array after rotation
        for (int i = 0; i < n; i  ) {
            console.write(array[i]   " ");
        }
    }
    // driver code
    public static void main()
    {
        // initialize the array
        int []array = { 1, 2, 3, 4, 5 };
        // find the size of the array
        int n = array.length;
        // initialize k
        int k = 4;
        // call the function and
        // print the answer
        rightrotate(array, n, k);
    }
}
// this code is contributed by samim hossain mondal.

java 描述语言


output

2 3 4 5 1 

时间复杂度 : o(n) 辅助空间 : o(1)