#C12942. Reverse an Array without Using Built-in Functions
Reverse an Array without Using Built-in Functions
Reverse an Array without Using Built-in Functions
You are given an array of integers. Your task is to reverse the array without using any built-in functions for reversing. You must implement your own algorithm to reverse the array.
Input Format: The input is read from stdin
and consists of two lines. The first line contains a single integer \(n\), which denotes the number of elements in the array. The second line contains \(n\) space-separated integers representing the elements of the array.
Output Format: Print the reversed array to stdout
as space-separated integers in one line. If the array is empty, output an empty line.
Example:
Input: 5 1 2 3 4 5</p>Output: 5 4 3 2 1
Note: You must reverse the array manually by iterating through the array and swapping or assigning values appropriately. Avoid using any built-in reverse methods or functions.
inputFormat
The input is provided via stdin
in the following format:
- The first line contains a single integer \(n\), the number of elements in the array.
- The second line contains \(n\) space-separated integers.
outputFormat
Output the reversed array as a single line of space-separated integers to stdout
. In case the array is empty, output an empty line.
5
1 2 3 4 5
5 4 3 2 1