Array -DS || Hackerrank Solution
An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, A , of size N, each memory location has some unique index, i (where 0 ≤ i < N), that can be referenced as A[i] or Ai .
Reverse an array of integers.
Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this.
Example
A = [1,2,3]
Return [3,2,1].
Function Description
Complete the function reverseArray in the editor below.
reverseArray has the following parameter(s):
- int A[n]: the array to reverse
Returns
- int[n]: the reversed array
Input Format
The first line contains an integer, N, the number of integers in A.
The second line contains N space-separated integers that make up A .
Constraints
- 1 ≤ N ≤ 10^3
- 1 ≤ A[i] ≤ 10^4, where A[i] is the i integer in A
Sample Input 1
4
1 4 3 2
Sample output
2 3 4 1
Solution
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int i,n;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=n-1;i>=0;i--){
printf("%d ",a[i]);
}
return 0;
}
Comments
Post a Comment