Open In App

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

Last Updated : 27 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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


Output:

5 4 1 6 3 2

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

Please refer complete article on Rotate all odd numbers right and all even numbers left in an Array of 1 to N for more details!



Similar Reads

C++ Program to 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
2 min read
Java Program to 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
2 min read
Javascript Program to 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
2 min read
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,
5 min read
Python3 Program to Rotate the matrix right by K times
Given a matrix of size N*M, and a number K. We have to rotate the matrix K times to the right side. Examples: Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4 A simple yet effective approach is to consider each row of the matrix as an array and perform an
2 min read
Python3 Program to Rotate the sub-list of a linked list from position M to N to the right by K places
Given a linked list and two positions 'm' and 'n'. The task is to rotate the sublist from position m to n, to the right by k places. Examples: Input: list = 1->2->3->4->5->6, m = 2, n = 5, k = 2 Output: 1->4->5->2->3->6 Rotate the sublist 2 3 4 5 towards right 2 times then the modified list are: 1 4 5 2 3 6 Input: list
4 min read
Python3 Program to Minimize characters to be changed to make the left and right rotation of a string same
Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = “abcd”Output: 2Explanation:String after the left shift: “bcda”String after the right shift: “dabc”Changing the character at position 3 to 'a' and c
3 min read
Python3 Program for Longest subsequence of a number having same left and right rotation
Given a numeric string S, the task is to find the maximum length of a subsequence having its left rotation equal to its right rotation. Examples: Input: S = "100210601" Output: 4 Explanation: The subsequence "0000" satisfies the necessary condition. The subsequence "1010" generates the string "0101" on left rotation and string "0101" on right rotat
4 min read
Python3 Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d <= n)Right (Or clockwise) rotate the given string by d elements (where d <= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
3 min read
Python3 Program to Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i
Given an array of n elements. Our task is to write a program to rearrange the array such that elements at even positions are greater than all elements before it and elements at odd positions are less than all elements before it.Examples: Input : arr[] = {1, 2, 3, 4, 5, 6, 7} Output : 4 5 3 6 2 7 1 Input : arr[] = {1, 2, 1, 4, 5, 6, 8, 8} Output : 4
3 min read
Practice Tags :