📌  相关文章
📜  仅在给定数组中的奇数位置反转元素

📅  最后修改于: 2021-05-17 05:02:42             🧑  作者: Mango

给定一个包含N个整数的数组arr [] ,任务是重新排列该数组,以使奇数索引元素的顺序相反。
例子:

方法:要解决上述问题,请执行以下步骤:

  • 将给定数组的奇数索引元素推入堆栈数据结构
  • 将当前位于奇数索引处的数组元素替换为堆栈顶部的元素,并不断弹出直到堆栈为空。

下面是上述方法的实现:

C++
// C++ program to reverse the
// elements only at odd positions
// in the given Array
 
#include 
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 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;
}


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 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 stack
def peek_stack(stack):
     
    if stack:
        return stack[-1]
 
# Function to display elements
def 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 indexes
def 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 Code
if __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 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 29AjayKumar


输出:
{1, 6, 3, 4, 5, 2}


时间复杂度: O(N)
辅助空间复杂度: O(N / 2)