In this tutorial, we are going to solve the HackerRank Array reversal problem with practical program code example and step-by-step explanation. in this problem first, we need to read an integer value from user input that is the size of the array and then the next line contains space-separated integers denoting the array values. after that, we need to verse the array and print it on the output screen.
![Array reversal problem solution](https://programmingoneonone.com/wp-content/uploads/2024/07/HackerRank-array-reversal-problem-solution-in-c.webp)
Solution in c programming
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, *arr, i;
scanf("%d", &num);
arr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++) {
scanf("%d", arr + i);
}
int temp;
for (i = 0; i < num / 2; i++) {
temp = (int) *(arr + num - i - 1);
*(arr + num - i - 1) = *(arr + i);
*(arr + i) = temp;
}
for(i = 0; i < num; i++)
printf("%d ", *(arr + i));
return 0;
}
Second solution
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, *arr, i;
scanf("%d", &num);
arr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++) {
scanf("%d", arr + i);
}
/* Write the logic to reverse the array. */
int* left_ptr = arr;
int* right_ptr;
int temp;
for(i = 0; i < num; i++) {
if(i == num - 1) {
right_ptr = (arr + i);
}
}
while(left_ptr < right_ptr) {
temp = *right_ptr;
*right_ptr = *left_ptr;
*left_ptr = temp;
right_ptr--;
left_ptr++;
}
for(i = 0; i < num; i++) {
printf("%d ", *(arr + i));
}
free(arr);
return 0;
}