Arrays - DS HackerRank Solution

 



Problem Statement

An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, , of size , each memory location has some unique index,  (where ), that can be referenced as  or .

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

Return .

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, , the number of integers in .
The second line contains  space-separated integers that make up .

Solution Program

[C++]


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
            int N;
            cin>>N;
   
            vector<int> arr(N);
   
            for (int i=0;i<N;i++)
                cin>>arr[i];
   
            reverse(arr.begin(),arr.end());
   
                for (int i=0;i<N;i++)
                cout<<arr[i]<<" ";
   
            return 0;
}


if (You Like This Post)

{   

      <<"Do Share it with Your Friends and Classmates">>

}

return Comments and Likes;

Post a Comment

0 Comments