Wednesday, July 3, 2024
HomeLanguagesPythonPython3 Program to Rotate all odd numbers right and all even numbers...

Python3 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, 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!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
You’ll access excellent video content by our CEO, Sandeep Jain, tackle common interview questions, and engage in real-time coding contests covering various DSA topics. We’re here to prepare you thoroughly for online assessments and interviews.
Ready to dive in? Explore our free demo content and join our DSA course, trusted by over 100,000neveropen! Whether it’s DSA in C++, Java, Python, or JavaScript we’ve got you covered. Let’s embark on this exciting journey together!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments