# Replace every element of the array by its previous element

Given an array arr, the task is to replace each element of the array with the element that appears before it and replace the first element with -1.

Examples:

Input: arr[] = {5, 1, 3, 2, 4}
Output: -1 5 1 3 2

Input: arr[] = {6, 8, 32, 12, 14, 10, 25 }
Output: -1 6 8 32 12 14 10

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Traverse the array from n – 1 to 1 and update arr[i] = arr[i-1]. In the end set a = -1 and print the contents of the updated array.

Below is the implementation of the above approach:

 `// C++ program to replace every element of the array ` `// with the element that appears before it ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the array after replacing every element ` `// of the array with the element that appears before it ` `void` `updateArray(``int` `arr[], ``int` `n) ` `{ ` `    ``// Update array ` `    ``for` `(``int` `i = n - 1; i > 0; i--) ` `        ``arr[i] = arr[i - 1]; ` ` `  `    ``// Change the first element to -1 ` `    ``arr = -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); ` `    ``updateArray(arr, N); ` `    ``return` `0; ` `} `

 `// Java program to replace every element  ` `// of the array with the element that  ` `// appears before it ` `class` `GFG ` `{ ` ` `  `// Function to print the array after  ` `// replacing every element of the ` `// array with the element that appears  ` `// before it ` `static` `void` `updateArray(``int` `arr[], ``int` `n) ` `{ ` `    ``// Update array ` `    ``for` `(``int` `i = n - ``1``; i > ``0``; i--) ` `        ``arr[i] = arr[i - ``1``]; ` ` `  `    ``// Change the first element to -1 ` `    ``arr[``0``] = -``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 `

 `# Python 3 program to replace every element  ` `# of the array with the element that appears  ` `# before it ` ` `  `# Function to print the array after replacing  ` `# every element of the array with the element  ` `# that appears before it ` `def` `updateArray(arr, n): ` `     `  `    ``# Update array ` `    ``i ``=` `n ``-` `1` `    ``while``(i > ``0``): ` `        ``arr[i] ``=` `arr[i ``-` `1``] ` `        ``i ``-``=` `1` ` `  `    ``# Change the first element to -1 ` `    ``arr[``0``] ``=` `-``1` ` `  `    ``# Print the updated array ` `    ``for` `i ``in` `range``(``0``, n, ``1``): ` `        ``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 ` `# Surendra_Gangwar `

 `// C# program to replace every element  ` `// of the array with the element that  ` `// appears before it ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to print the array after  ` `// replacing every element of the ` `// array with the element that appears  ` `// before it ` `static` `void` `updateArray(``int``[] arr, ``int` `n) ` `{ ` `    ``// Update array ` `    ``for` `(``int` `i = n - 1; i > 0; i--) ` `        ``arr[i] = arr[i - 1]; ` ` `  `    ``// Change the first element to -1 ` `    ``arr = -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 `

 ` 0; ``\$i``--) ` `        ``\$arr``[``\$i``] = ``\$arr``[``\$i` `- 1]; ` ` `  `    ``// Change the first element to -1 ` `    ``\$arr`` = -1; ` ` `  `    ``// Print the updated array ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `        ``echo` `\$arr``[``\$i``] ,``" "``; ` `} ` ` `  `// Driver Code ` `\$arr` `= ``array``(5, 1, 3, 2, 4 ); ` `\$N` `= sizeof(``\$arr``); ` `updateArray(``\$arr``, ``\$N``); ` ` `  `// This code is contributed ` `// by Sach_Code ` `?> `

Output:
```-1 5 1 3 2
```

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.

Check out this Author's contributed articles.

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.

Practice Tags :