Replace every element of the array by its next element

11 May, 2022
Given an array arr, the task is to replace each element of the array with the element that appears after it and replace the last element with -1.Examples:  
Input: arr[] = {5, 1, 3, 2, 4} Output: 1 3 2 4 -1Input: arr[] = {6, 8, 32, 12, 14, 10, 25 } Output: 8 32 12 14 10 25 -1  
 
Approach: Traverse the array from 0 to n-2 and update arr[i] = arr[i+1]. In the end set a[n-1] = -1 and print the contents of the updated array.Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
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 itvoid 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 programint main(){    int arr[] = { 5, 1, 3, 2, 4 };    int N = sizeof(arr) / sizeof(arr[0]);    updateArray(arr, N);    return 0;}
// Java program to replace every element// of the array with the element that// appears after itclass GFG{ // Function to print the array after// replacing every element of the array// with the element that appears after itstatic 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 Codepublic 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 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 itdef 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 Codeif __name__ == "__main__":     arr = [ 5, 1, 3, 2, 4 ]    N = len(arr)    updateArray(arr, N) # This code is contributed# by ChitraNayal
// C# program to replace every element// of the array with the element that// appears after itusing System; class GFG{ // Function to print the array after// replacing every element of the array// with the element that appears after itstatic 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 Codepublic 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 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 itfunction 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?>
<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 itfunction 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 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 itvoid 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 programint main(){    int arr[] = { 5, 1, 3, 2, 4 };    int N = sizeof(arr) / sizeof(arr[0]);    updateArray(arr, N);    return 0;}
1 3 2 4 -1
 
Time Complexity : O(n)Auxiliary Space: O(1) 
ankthon
Akanksha_Rai
ukasp
Shivi_Aggarwal
VishalBachchas
mayanktyagi1709
pankajsharmagfg
kothavvsaakash
Technical Scripter 2018
Arrays
C++ Programs
Data Structures
Technical Scripter
Data Structures
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
Shallow Copy and Deep Copy in C++