Reverse the elements only at odd positions in the given Array
Last Updated :
10 May, 2021
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++
#include <bits/stdc++.h>
using namespace std;
void show( int arr[], int n)
{
cout << "{" ;
for ( int i = 0; i < n - 1; i++)
cout << arr[i] << ", " ;
cout << arr[n - 1] << "}" ;
}
void flipHalf( int arr[], int n)
{
int c = 0;
int dup = n;
stack< int > st;
for ( int i = 0; i < n; i++) {
int x = arr[i];
if (c % 2 == 1)
st.push(x);
c++;
}
c = 0;
for ( int i = 0; i < n; i++) {
int x = arr[i];
if (c % 2 == 1) {
x = st.top();
st.pop();
}
arr[i] = x;
c++;
}
}
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
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 ] + "}" );
}
public static void flipHalf( int arr[], int n)
{
int c = 0 ;
int dup = n;
Stack<Integer> st = new Stack<>();
for ( int i = 0 ; i < n; i++)
{
int x = arr[i];
if (c % 2 == 1 )
{
st.push(x);
}
c++;
}
c = 0 ;
for ( int i = 0 ; i < n; i++)
{
int x = arr[i];
if (c % 2 == 1 )
{
x = st.peek();
st.pop();
}
arr[i] = x;
c++;
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
int n = arr.length;
flipHalf(arr, n);
show(arr, n);
}
}
|
Python3
def peek_stack(stack):
if stack:
return stack[ - 1 ]
def show(arr, n):
print ( "{" , end = " " )
for i in range ( 0 , n - 1 ):
print (arr[i], "," , end = " " )
print (arr[n - 1 ] , "}" )
def flipHalf(arr, n):
c = 0
dup = n
stack = []
for i in range ( 0 , n):
x = arr[i]
if c % 2 = = 1 :
stack.append(x)
c = c + 1
c = 0
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
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len (arr)
flipHalf(arr, n)
show(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
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] + "}" );
}
public static void flipHalf( int []arr, int n)
{
int c = 0;
int dup = n;
Stack< int > st = new Stack< int >();
for ( int i = 0; i < n; i++)
{
int x = arr[i];
if (c % 2 == 1)
{
st.Push(x);
}
c++;
}
c = 0;
for ( int i = 0; i < n; i++)
{
int x = arr[i];
if (c % 2 == 1)
{
x = st.Peek();
st.Pop();
}
arr[i] = x;
c++;
}
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.Length;
flipHalf(arr, n);
show(arr, n);
}
}
|
Javascript
<script>
function show(arr, n)
{
document.write( "{" );
for (let i = 0; i < n - 1; i++)
document.write(arr[i] + ", " );
document.write(arr[n - 1] + "}" );
}
function flipHalf(arr, n)
{
let c = 0;
let dup = n;
let st = [];
for (let i = 0; i < n; i++) {
let x = arr[i];
if (c % 2 == 1)
st.push(x);
c++;
}
c = 0;
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>
|
Output:
{1, 6, 3, 4, 5, 2}
Time complexity: O(N)
Auxiliary Space Complexity: O(N/2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...