#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Reversing the array
for (int i = 0, j = n - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Printing the reversed array
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Let's go through the solution step by step:
We include the
In the main() function, we declare an integer n to store the size of the array.
We use the scanf() function to read input from the user and store it in the variable n.
We declare an integer array arr of size n to store the elements of the array.
We use a for loop to iterate from 0 to n-1 and read each element of the array using the scanf() function. The elements are stored in the corresponding indices of the arr array.
To reverse the array, we use another for loop with two loop variables: i starting from 0 and j starting from n - 1. We continue the loop until i is less than j.
Inside the loop, we swap the elements at indices i and j using a temporary variable temp. This swaps the first and last elements, then the second and second-to-last elements, and so on, effectively reversing the array.
After reversing the array, we use another for loop to iterate from 0 to n-1 and print each element of the reversed array using the printf() function.
By providing the expected input and executing the code, you should see the reversed array printed according to the problem statement on HackerRank.