Reemplace cada elemento de la array por su siguiente elemento

Dada una array arr , la tarea es reemplazar cada elemento de la array con el elemento que aparece después y reemplazar el último elemento con -1 .
Ejemplos: 
 

Entrada: arr[] = {5, 1, 3, 2, 4} 
Salida: 1 3 2 4 -1
Entrada: arr[] = {6, 8, 32, 12, 14, 10, 25 } 
Salida: 8 32 12 14 10 25 -1 
 

Enfoque: recorrer la array de 0 a n-2 y actualizar arr[i] = arr[i+1] . Al final, establezca a[n-1] = -1 e imprima el contenido de la array actualizada.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to replace every element of the array
// with the element that appears after it
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the array after replacing every element
// of the array with the element that appears after it
void updateArray(int arr[], int n)
{
    // Update array
    for (int i = 0; i <= n - 2; i++)
        arr[i] = arr[i + 1];
 
    // Change the last element to -1
    arr[n - 1] = -1;
 
    // Print the updated array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Driver program
int main()
{
    int arr[] = { 5, 1, 3, 2, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    updateArray(arr, N);
    return 0;
}

Java

// Java program to replace every element
// of the array with the element that
// appears after it
class GFG
{
 
// Function to print the array after
// replacing every element of the array
// with the element that appears after it
static void updateArray(int arr[], int n)
{
    // Update array
    for (int i = 0; i <= n - 2; i++)
        arr[i] = arr[i + 1];
 
    // Change the last element to -1
    arr[n - 1] = -1;
 
    // Print the updated array
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
}
 
// Driver Code
public static void main(String []args)
{
    int arr[] = { 5, 1, 3, 2, 4 } ;
    int N = arr.length ;
    updateArray(arr, N);
}
}
 
// This code is contributed by Ryuga

Python3

# Python3 program to replace every
# element of the array with the
# element that appears after it
 
# Function to print the array after
# replacing every element of the
# array with the element that appears
# after it
def updateArray(arr, n):
 
    # Update array
    for i in range (n - 1):
        arr[i] = arr[i + 1]
 
    # Change the last element to -1
    arr[n - 1] = -1
 
    # Print the updated array
    for i in range( n):
        print (arr[i], end = " ")
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 5, 1, 3, 2, 4 ]
    N = len(arr)
    updateArray(arr, N)
 
# This code is contributed
# by ChitraNayal

C#

// C# program to replace every element
// of the array with the element that
// appears after it
using System;
 
class GFG
{
 
// Function to print the array after
// replacing every element of the array
// with the element that appears after it
static void updateArray(int[] arr, int n)
{
    // Update array
    for (int i = 0; i <= n - 2; i++)
        arr[i] = arr[i + 1];
 
    // Change the last element to -1
    arr[n - 1] = -1;
 
    // Print the updated array
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 5, 1, 3, 2, 4 } ;
    int N = arr.Length ;
    updateArray(arr, N);
}
}
 
// This code is contributed
// by Akanksha Rai

PHP

<?php
// PHP program to replace every element
// of the array with the element that
// appears after it
 
// Function to print the array after
// replacing every element of the
// array with the element that appears
// after it
function updateArray(&$arr, $n)
{
    // Update array
    for ($i = 0; $i <= $n - 2; $i++)
        $arr[$i] = $arr[$i + 1];
 
    // Change the last element to -1
    $arr[$n - 1] = -1;
 
    // Print the updated array
    for ($i = 0; $i < $n; $i++)
    {
        echo ($arr[$i]);
        echo (" ");
    }
}
 
// Driver Code
$arr = array(5, 1, 3, 2, 4 );
$N = sizeof($arr);
updateArray($arr, $N);
     
// This code is contributed
// by Shivi_Aggarwal
?>

Javascript

<script>
 
// Javascript program to replace every element of the array
// with the element that appears after it
 
// Function to print the array after replacing every element
// of the array with the element that appears after it
function updateArray(arr, n)
{
    // Update array
    for (let i = 0; i <= n - 2; i++)
        arr[i] = arr[i + 1];
 
    // Change the last element to -1
    arr[n - 1] = -1;
 
    // Print the updated array
    for (let i = 0; i < n; i++)
        document.write(arr[i] + " ");
}
 
// Driver program
 
    let arr = [ 5, 1, 3, 2, 4 ];
    let N = arr.length;
    updateArray(arr, N);
 
//This code is contributed by Mayank Tyagi
 
</script>

C

// C program to replace every element of the array
// with the element that appears after it
#include <stdio.h>
 
// Function to print the array after replacing every element
// of the array with the element that appears after it
void updateArray(int arr[], int n)
{
    // Update array
    for (int i = 0; i <= n - 2; i++)
        arr[i] = arr[i + 1];
 
    // Change the last element to -1
    arr[n - 1] = -1;
 
    // Print the updated array
    for (int i = 0; i < n; i++)
        printf("%d ",arr[i]);
}
 
// Driver program
int main()
{
    int arr[] = { 5, 1, 3, 2, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    updateArray(arr, N);
    return 0;
}
Producción: 

1 3 2 4 -1

 

Tiempo Complejidad : O(n)
Espacio Auxiliar : O(1)
 

Publicación traducida automáticamente

Artículo escrito por gfg_sal_gfg 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 *