Reverse the elements only at odd positions in the given Array

Given an array arr[] containing N integers, the task is to rearrange the array such that the odd indexed elements are in reverse order.

Examples:

Input: arr[] = {5, 7, 6, 2, 9, 18, 11, 15}
Output: {5, 15, 6, 18, 9, 2, 11, 7}
Explanation:
The elements at even index [5, 6, 9, 11] are unchanged and elements at odd index are reversed from [7, 2, 18, 15] to [15, 18, 2, 7].

Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: {1, 6, 3, 4, 5, 2}
Explanation:
The elements at even index are unchanged and elements at odd index are reversed from [2, 4, 6] to [6, 4, 2].

Approach: To solve the problem mentioned above follow the steps given below:



  • Push the elements of odd indexes of the given array into a stack data structure.
  • Replace the current array elements at odd indexes with elements at top of the stack and keep popping until the stack is empty.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to reverse the
// elements only at odd positions
// in the given Array
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to display elements
void show(int arr[], int n)
{
    cout << "{";
  
    for (int i = 0; i < n - 1; i++)
        cout << arr[i] << ", ";
  
    cout << arr[n - 1] << "}";
}
  
// Function to flip elements
// at odd indexes
void flipHalf(int arr[], int n)
{
    int c = 0;
    int dup = n;
  
    stack<int> st;
  
    // Pushing elements at odd indexes
    // of a array to a stack
    for (int i = 0; i < n; i++) {
        int x = arr[i];
  
        if (c % 2 == 1)
            st.push(x);
        c++;
    }
  
    c = 0;
  
    // Replacing current elements at odd
    // indexes with element at top of stack
    for (int i = 0; i < n; i++) {
        int x = arr[i];
  
        if (c % 2 == 1) {
            x = st.top();
            st.pop();
        }
        arr[i] = x;
        c++;
    }
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    flipHalf(arr, n);
  
    show(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to reverse the 
// elements only at odd positions 
// in the given array 
import java.io.*; 
import java.util.*;
  
class GFG { 
  
// Function to count the valley points 
// in the given character array 
static void show(int arr[], int n) 
{
    System.out.print("{");
      
    for(int i = 0; i < n - 1; i++)
       System.out.print(arr[i] + ", ");
  
    System.out.print(arr[n - 1] + "}");         
  
// Function to flip elements 
// at odd indexes 
public static void flipHalf(int arr[], int n) 
    int c = 0
    int dup = n; 
    Stack<Integer> st = new Stack<>(); 
      
    // Pushing elements at odd indexes
    // of a array to a stack 
    for(int i = 0; i < n; i++) 
    
       int x = arr[i]; 
         
       if (c % 2 == 1)
       {
           st.push(x);
       }
       c++; 
    
    c = 0
      
    // Replacing current elements at odd 
    // indexes with element at top of stack 
    for(int i = 0; i < n; i++) 
    
       int x = arr[i]; 
         
       if (c % 2 == 1
       
           x = st.peek(); 
           st.pop(); 
       
       arr[i] = x; 
       c++; 
    
  
// Driver Code 
public static void main(String[] args) 
    int arr[] = { 1, 2, 3, 4, 5, 6 }; 
    int n = arr.length; 
      
    flipHalf(arr, n); 
    show(arr, n); 
}     
  
// This code is contributed by Aman Kumar 27

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to reverse the 
// elements only at odd positions 
// in the given array 
using System;
using System.Collections.Generic;
  
class GFG{ 
  
// Function to count the valley points 
// in the given character array 
static void show(int []arr, int n) 
{
    Console.Write("{");
      
    for(int i = 0; i < n - 1; i++)
    Console.Write(arr[i] + ", ");
  
    Console.Write(arr[n - 1] + "}");         
  
// Function to flip elements 
// at odd indexes 
public static void flipHalf(int []arr, int n) 
    int c = 0; 
    int dup = n; 
    Stack<int> st = new Stack<int>(); 
      
    // Pushing elements at odd indexes
    // of a array to a stack 
    for(int i = 0; i < n; i++) 
    
        int x = arr[i]; 
              
        if (c % 2 == 1)
        {
            st.Push(x);
        }
        c++; 
    
    c = 0; 
      
    // Replacing current elements at odd 
    // indexes with element at top of stack 
    for(int i = 0; i < n; i++) 
    
        int x = arr[i]; 
              
        if (c % 2 == 1) 
        
            x = st.Peek(); 
            st.Pop(); 
        
        arr[i] = x; 
        c++; 
    
  
// Driver Code 
public static void Main(String[] args) 
    int []arr = { 1, 2, 3, 4, 5, 6 }; 
    int n = arr.Length; 
      
    flipHalf(arr, n); 
    show(arr, n); 
}     
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

{1, 6, 3, 4, 5, 2}

Time complexity: O(N)

Auxiliary Space Complexity: O(N/2)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

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 : Aman Kumar 27, 29AjayKumar