Rotate all odd numbers right and all even numbers left in an Array of 1 to N

Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. 
Note: N is always even.
Examples: 

Input: A = {1, 2, 3, 4, 5, 6, 7, 8} 
Output: {7, 4, 1, 6, 3, 8, 5, 2} 
Explanation: 
Even element = {2, 4, 6, 8} 
Odd element = {1, 3, 5, 7} 
Left rotate of even number = {4, 6, 8, 2} 
Right rotate of odd number = {7, 1, 3, 5} 
Combining Both odd and even number alternatively.
Input: A = {1, 2, 3, 4, 5, 6} 
Output: {5, 4, 1, 6, 3, 2} 
 

Approach:

  1. It is clear that the odd elements are always on even index and even elements are always laying on odd index.
  2. To do left rotation of even number we choose only odd indices.
  3. To do right rotation of odd number we choose only even indices.
  4. Print the updated array.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
#include<bits/stdc++.h>
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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
5 4 1 6 3 2

Time Complexity: O(N) 
Auxiliary Space: O(1)
 




Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : rock_cool, sanjoy_62, chitranayal

Article Tags :
Practice Tags :