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++
// 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 ?> |
1 3 2 4 -1
Time Complexity : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Replace every array element by Bitwise Xor of previous and next element
- Replace every array element by multiplication of previous and next
- Replace every array element by sum of previous and next
- Replace every element in a circular array by sum of next K elements
- Replace every element of the array by its previous element
- Replace every element with the greatest element on its left side
- Replace every element with the smallest element on its left side
- Replace every element with the least greater element on its right
- Replace every element of array with sum of elements on its right side
- Queries to replace every array element by its XOR with a given value with updates
- Replace every elements in the array by its frequency in the array
- Replace every vowels with lexicographically next vowel in a String
- Count of Array elements greater than all elements on its left and next K elements on its right
- Maximum element in an array such that its previous and next element product is maximum
- Modify given array by reducing each element by its next smaller element
- Find next Smaller of next Greater in an array
- Replace array elements by sum of next two consecutive elements
- Replace every element with the greatest element on right side
- Replace every element of the array with BitWise XOR of all other
- Replace every element of the array by sum of all other elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

