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 the above approach:
C++
// C++ program to reverse the// elements only at odd positions// in the given Array#include <bits/stdc++.h>using namespace std;// Function to display elementsvoid 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 indexesvoid 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 Codeint main(){ int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); flipHalf(arr, n); show(arr, n); return 0;} |
Java
// 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 |
Python3
# Python3 program to reverse the# elements only at odd positions# in the given Array# Function to get top of the stackdef peek_stack(stack): if stack: return stack[-1]# Function to display elementsdef show(arr, n): print("{", end = " ") for i in range(0, n - 1): print(arr[i], ",", end = " ") print(arr[n - 1] , "}")# Function to flip elements# at odd indexesdef flipHalf(arr, n): c = 0 dup = n stack = [] # Pushing elements at odd indexes # of a array to a stack for i in range(0, n): x = arr[i] if c % 2 == 1: stack.append(x) c = c + 1 c = 0 # Replacing current elements at odd # indexes with element at top of stack for i in range(0, n): x = arr[i] if c % 2 == 1: x = peek_stack(stack) stack.pop() arr[i] = x c = c + 1# Driver Codeif __name__ == "__main__": arr = [ 1, 2, 3, 4, 5, 6 ] n = len(arr) flipHalf(arr, n) show(arr, n)# This code is contributed by akhilsaini |
C#
// 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 |
Javascript
<script> // Javascript program to reverse the // elements only at odd positions // in the given Array // Function to display elements function show(arr, n) { document.write("{"); for (let i = 0; i < n - 1; i++) document.write(arr[i] + ", "); document.write(arr[n - 1] + "}"); } // Function to flip elements // at odd indexes function flipHalf(arr, n) { let c = 0; let dup = n; let st = []; // Pushing elements at odd indexes // of a array to a stack for (let i = 0; i < n; i++) { let 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 (let i = 0; i < n; i++) { let x = arr[i]; if (c % 2 == 1) { x = st[st.length - 1]; st.pop(); } arr[i] = x; c++; } } let arr = [ 1, 2, 3, 4, 5, 6 ]; let n = arr.length; flipHalf(arr, n); show(arr, n);</script> |
{1, 6, 3, 4, 5, 2}
Time complexity: O(N)
Auxiliary Space Complexity: O(N/2)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
