Invierta los elementos solo en posiciones impares en la array dada

Dada una array arr[] que contiene N enteros, la tarea es reorganizar la array de modo que los elementos impares indexados estén en orden inverso.

Ejemplos:

Entrada: arr[] = {5, 7, 6, 2, 9, 18, 11, 15} 
Salida: {5, 15, 6, 18, 9, 2, 11, 7} 
Explicación: 
Los elementos en el índice par [ 5, 6, 9, 11] no cambian y los elementos con índice impar se invierten de [7, 2, 18, 15] a [15, 18, 2, 7].

Entrada: arr[] = {1, 2, 3, 4, 5, 6} 
Salida: {1, 6, 3, 4, 5, 2} 
Explicación: 
Los elementos de índice par no cambian y los elementos de índice impar se invierten de [2, 4, 6] a [6, 4, 2]. 

Enfoque: Para resolver el problema mencionado anteriormente, siga los pasos que se detallan a continuación: 

  • Empuje los elementos de los índices impares de la array dada en una estructura de datos de pila .
  • Reemplace los elementos de la array actual en los índices impares con elementos en la parte superior de la pila y siga apareciendo hasta que la pila esté vacía. 
     

A continuación se muestra la implementación del enfoque anterior: 

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 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;
}

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 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<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>
Producción: 

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

 

Complejidad temporal: O(N)
Complejidad espacial auxiliar: O(N/2) 
 

Publicación traducida automáticamente

Artículo escrito por yashbeersingh42 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *