📌  相关文章
📜  在 1 到 N 的数组中向右旋转所有奇数和向左旋转所有偶数

📅  最后修改于: 2021-09-05 11:44:31             🧑  作者: Mango

给定一个由[1, N]范围内的N 个数字组成的排列数组A[] ,任务是向左旋转所有偶数,向右旋转所有奇数,并打印更新后的排列。
注意: N 总是偶数。
例子:

方法:

  1. 很明显,奇数元素总是在偶数索引上,偶数元素总是在奇数索引上。
  2. 要进行偶数的左旋转,我们只选择奇数索引。
  3. 为了对奇数进行右旋转,我们只选择偶数索引。
  4. 打印更新的数组。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include
using namespace std;
 
// function to left rotate
void left_rotate(int arr[])
{
    int last = arr[1];
    for (int i = 3; i < 6; i = i + 2)
    {
        arr[i - 2] = arr[i];
    }
    arr[6 - 1] = last;
}
 
// function to right rotate
void right_rotate(int arr[])
{
    int start = arr[6 - 2];
    for (int i = 6- 4; i >= 0; i = i - 2)
    {
        arr[i + 2] = arr[i];
    }
    arr[0] = start;
}
 
// Function to rotate the array
void rotate(int arr[])
{
    left_rotate(arr);
    right_rotate(arr);
    for (int i = 0; i < 6; i++)
    {
        cout << (arr[i]) << " ";
    }
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
 
    rotate(arr);
}
 
// This code is contributed by rock_cool


Java
// Java program to implement
// the above approach
 
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // function to left rotate
    static void left_rotate(int[] arr)
    {
        int last = arr[1];
        for (int i = 3;
            i < arr.length;
            i = i + 2) {
            arr[i - 2] = arr[i];
        }
        arr[arr.length - 1] = last;
    }
 
    // function to right rotate
    static void right_rotate(int[] arr)
    {
        int start = arr[arr.length - 2];
        for (int i = arr.length - 4;
            i >= 0;
            i = i - 2) {
            arr[i + 2] = arr[i];
        }
        arr[0] = start;
    }
 
    // Function to rotate the array
    public static void rotate(int arr[])
    {
        left_rotate(arr);
        right_rotate(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3, 4, 5, 6 };
 
        rotate(arr);
    }
}


Python3
# Python3 program for the above approach
 
# Function to left rotate
def left_rotate(arr):
     
    last = arr[1];
    for i in range(3, len(arr), 2):
        arr[i - 2] = arr[i]
         
    arr[len(arr) - 1] = last
 
# Function to right rotate
def right_rotate(arr):
     
    start = arr[len(arr) - 2]
    for i in range(len(arr) - 4, -1, -2):
        arr[i + 2] = arr[i]
         
    arr[0] = start
 
# Function to rotate the array
def rotate(arr):
     
    left_rotate(arr)
    right_rotate(arr)
    for i in range(len(arr)):
        print(arr[i], end = " ")
 
# Driver code
arr = [ 1, 2, 3, 4, 5, 6 ]
 
rotate(arr);
 
# This code is contributed by sanjoy_62


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to left rotate
static void left_rotate(int[] arr)
{
    int last = arr[1];
    for(int i = 3;
            i < arr.Length;
            i = i + 2)
    {
        arr[i - 2] = arr[i];
    }
    arr[arr.Length - 1] = last;
}
 
// Function to right rotate
static void right_rotate(int[] arr)
{
    int start = arr[arr.Length - 2];
    for(int i = arr.Length - 4;
            i >= 0; i = i - 2)
    {
        arr[i + 2] = arr[i];
    }
    arr[0] = start;
}
 
// Function to rotate the array
public static void rotate(int[] arr)
{
    left_rotate(arr);
    right_rotate(arr);
     
    for(int i = 0; i < arr.Length; i++)
    {
        Console.Write(arr[i] + " ");
    }
}
 
// Driver code
public static void Main()
{
    int[] arr = { 1, 2, 3, 4, 5, 6 };
 
    rotate(arr);
}
}
 
// This code is contributed by chitranayal


Javascript


输出:

5 4 1 6 3 2

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live