# Program to copy the contents of one array into another in the reverse order

Given an array, the task is to copy these array elements into another array in reverse array.
Examples:

`Input: array: 1 2 3 4 5 Output: 5 4 3 2 1 Input: array: 10 20 30 40 50 Output: 50 40 30 20 10  `

Let len be the length of original array. We copy every element original_arr[i] to copy_arr[n-i-1] to get reverse in copy_arr[].

## C++

 `// C program to copy the contents``// of one array into another``// in the reverse order` `#include ` `// Function to print the array``void` `printArray(``int` `arr[], ``int` `len)``{``    ``int` `i;``    ``for` `(i = 0; i < len; i++) {``        ``printf``(``"%d "``, arr[i]);``    ``}``}` `// Driver code``int` `main()``{``    ``int` `original_arr[] = {1, 2, 3, 4, 5};``    ``int` `len = ``sizeof``(original_arr)/``sizeof``(original_arr[0]);` `    ``int` `copied_arr[len], i, j;``   ` `    ``// Copy the elements of the array``    ``// in the copied_arr in Reverse Order``    ``for` `(i = 0; i < len; i++) {``        ``copied_arr[i] = original_arr[len - i - 1];``    ``}` `    ``// Print the original_arr``    ``printf``(``"\nOriginal array: "``);``    ``printArray(original_arr, len);` `    ``// Print the copied array``    ``printf``(``"\nResultant array: "``);``    ``printArray(copied_arr, len);` `    ``return` `0;``}`

## Java

 `// Java program to copy the contents``// of one array into another``// in the reverse order``class` `GFG {` `// Function to print the array``static` `void` `printArray(``int` `arr[], ``int` `len)``{``    ``int` `i;``    ``for` `(i = ``0``; i < len; i++)``    ``{``        ``System.out.printf(``"%d "``, arr[i]);``    ``}``}` `// Driver code``public` `static` `void` `main(String[] args) ``{``    ``int` `original_arr[] = {``1``, ``2``, ``3``, ``4``, ``5``};``    ``int` `len = original_arr.length;``    ``int` `copied_arr[] = ``new` `int``[len], i, j;``    ` `    ``// Copy the elements of the array``    ``// in the copied_arr in Reverse Order``    ``for` `(i = ``0``; i < len; i++)``    ``{``        ``copied_arr[i] = original_arr[len - i - ``1``];``    ``}` `    ``// Print the original_arr``    ``System.out.printf(``"\nOriginal array: "``);``    ``printArray(original_arr, len);` `    ``// Print the copied array``    ``System.out.printf(``"\nResultant array: "``);``    ``printArray(copied_arr, len);``    ``}``}` `// This code is contributed by ``// PrinciRaj1992 `

## Python3

 `# Python3 program to copy the contents of one``# array into another in the reverse order``import` `math as mt` `# Function to print the array``def` `printArray(arr, ``Len``):` `    ``for` `i ``in` `range``(``Len``):``        ``print``(arr[i], end ``=` `" "``)``    ` `# Driver code``original_arr ``=` `[``1``, ``2``, ``3``, ``4``, ``5``]``Len` `=` `len``(original_arr)` `copied_arr ``=` `[``0` `for` `i ``in` `range``(``Len``)]` `# Copy the elements of the array``# in the copied_arr in Reverse Order``for` `i ``in` `range``(``Len``): ``    ``copied_arr[i] ``=` `original_arr[``Len` `-` `i ``-` `1``]` `# Print the original_arr``print``(``"Original array: "``, end ``=` `"")``printArray(original_arr, ``Len``)` `# Print the copied array``print``(``"\nResultant array: "``, end ``=` `"")``printArray(copied_arr, ``Len``)` `# This code is contributed by``# Mohit kumar 29`

## C#

 `// C# program to copy the contents ``// of one array into another ``// in the reverse order``using` `System;``class` `GFG ``{ ` `// Function to print the array ``static` `void` `printArray(``int` `[]arr, ``int` `len) ``{ ``    ``int` `i; ``    ``for` `(i = 0; i < len; i++) ``    ``{ ``        ``Console.Write(arr[i]); ``    ``} ``} ` `// Driver code ``public` `static` `void` `Main() ``{ ``    ``int` `[]original_arr = {1, 2, 3, 4, 5}; ``    ``int` `len = original_arr.Length; ``    ``int` `[]copied_arr = ``new` `int``[len];``    ``int` `i; ``    ` `    ``// Copy the elements of the array ``    ``// in the copied_arr in Reverse Order ``    ``for` `(i = 0; i < len; i++) ``    ``{ ``        ``copied_arr[i] = original_arr[len - i - 1]; ``    ``} ` `    ``// Print the original_arr ``    ``Console.Write(``"\nOriginal array: "``); ``    ``printArray(original_arr, len); ` `    ``// Print the copied array ``    ``Console.Write(``"\nResultant array: "``); ``    ``printArray(copied_arr, len); ``    ``} ``} ` `// This code is contributed by Rajput-Ji`

## Javascript

 ``

## PHP

 ``

Output
```Original array: 1 2 3 4 5
Resultant array: 5 4 3 2 1

```

Time Complexity: O(len)

Auxiliary Space: O(len)

Approach: Using the reversed() function

Steps:

• First declare an integer array arr and initialize it.
• Then use the sizeof operator to determine the size of the array.
• store it in a variable.
• Declare another integer array reverse_arr & with the same size as arr.
• then use for loop to iterate over the elements arr.
• copy them into reverse_arr in reverse order.
• Use the expression size-i-1 to calculate the index of the corresponding element in reverse_arr.
• use another for loop to print the elements of reverse_arr to the console.

Below is the code implementation for the above approach:

## C++

 `#include ``#include ` `using` `namespace` `std;` `int` `main() {``    ``int` `arr[] = { 10 ,20, 30, 40, 50 };``    ``int` `size = ``sizeof``(arr)/``sizeof``(arr[0]);` `    ``int` `reverse_arr[size];``    ``for``(``int` `i=0; i

## Java

 `import` `java.util.Arrays;` `public` `class` `Main {``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int``[] arr = { ``10``, ``20``, ``30``, ``40``, ``50` `};``        ``int` `size = arr.length;` `        ``int``[] reverseArr = ``new` `int``[size];` `        ``// Reversing the array``        ``for` `(``int` `i = ``0``; i < size; i++) {``            ``reverseArr[size - i - ``1``] = arr[i];``        ``}` `        ``// Printing the reversed array``        ``System.out.print(``"Reversed array: "``);``        ``for` `(``int` `i = ``0``; i < size; i++) {``            ``System.out.print(reverseArr[i] + ``" "``);``        ``}``    ``}``}`

## Python3

 `arr ``=` `[``10``, ``20``, ``30``, ``40``, ``50``]``size ``=` `len``(arr)``reverse_arr ``=` `[``0``] ``*` `size``for` `i ``in` `range``(size):``    ``reverse_arr[size ``-` `i ``-` `1``] ``=` `arr[i]``for` `i ``in` `range``(size):``    ``print``(reverse_arr[i], end``=``" "``)`

## C#

 `using` `System;` `class` `GFG``{``    ``static` `void` `Main()``    ``{``        ``// Given array in C++: int arr[] = { 10 ,20, 30, 40, 50 };``        ``int``[] arr = { 10, 20, 30, 40, 50 };` `        ``// Calculate the size of the array``        ``int` `size = arr.Length;` `        ``// Create a new array to store the reversed elements``        ``int``[] reverseArr = ``new` `int``[size];` `        ``// Loop through the original array and reverse its elements``        ``for` `(``int` `i = 0; i < size; i++)``        ``{``            ``// In C++, reverse_arr[size-i-1] = arr[i];``            ``// In C#, reverseArr[size-i-1] is equivalent to reverse_arr[size-i-1] in C++``            ``reverseArr[size - i - 1] = arr[i];``        ``}` `        ``// Display the reversed array``        ` `        ``for` `(``int` `i = 0; i < size; i++)``        ``{``            ``// In C++, cout << reverse_arr[i] << " ";``            ``// In C#, Console.Write(reverseArr[i] + " ") is equivalent to cout << reverse_arr[i] << " " in C++``            ``Console.Write(reverseArr[i] + ``" "``);``        ``}` `        ``// Pause the console so the output is visible``        ``Console.ReadLine();``    ``}``}`

## Javascript

 `console.log(``"Hello, World!"``);` `const arr = [10, 20, 30, 40, 50];``const size = arr.length;` `const reverse_arr = [];``for``(let i = 0; i < size; i++) {``    ``reverse_arr[size - i - 1] = arr[i];``}` `for``(let i = 0; i < size; i++) {``    ``console.log(reverse_arr[i] + ``" "``);``}`

Output
```50 40 30 20 10

```

Time Complexity: O(n)

Auxiliary Space: O(n)

Previous
Next