We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

vector<int> reverseArray(vector<int> a) {
int aux = 0;
int n = a.size();
for (int i = 0; i < n/2; ++i) {
aux = a[n-i-1];
a[n-i-1] = a[i];
a[i] = aux;
}
return a;
}

not really a better solution. your solution requires a new y vector. so the space complexity is O(n), while the solution above did everything inplace, so its space complexity is O(1).

## Arrays - DS

You are viewing a single comment's thread. Return to all comments →

My c++ solution:

why this much over programming required?

I'd say a much bettery solution (for c++14) is:

not really a better solution. your solution requires a new y vector. so the space complexity is O(n), while the solution above did everything inplace, so its space complexity is O(1).

And I'd say much better is simply

why u typing this much.... i finished the code within a couple of lines in the function;

that's all the finished...

the attempt was to show traversing backwards simply reverts it when you push back

i cant understand you bro.

You can follow the below link to know more.

Here is the video explaination - https://youtu.be/u_oUMtj7C3k

and you can find most of the hackerrank solutions with video explaination here- https://github.com/Java-aid/Hackerrank-Solutions

and many more needs to be addeed.

Regards,

Kanahaiya Gupta

Git Hub URL | https://github.com/Java-aid/

LIKE US | https://www.facebook.com/javaaid/

SUBSCRIBE US | https://www.youtube.com/channel/UCx1hbK753l3WhwXP5r93eYA

Can anyone explain why can't we just use the following plain approach compared to these solutions? `

vector reverseArray(vector a) { vector b; for(int i = a.size()-1;i>=0;i--){ b.push_back(a[i]); } return b; }

`

That was my solution, except in Java.