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).

I like this one. This is how would write up. The current programming structure looks really complex and it does not have to be this way. thanks for posting here so everyone knows. I am picking up programming after several years so atleast I can tell one way or other but newbies may not. thanks again

## Arrays - DS

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

My c++ solution:

why this much over programming required?

I've found that in technical interviews where they are strict on syntax, you may be asked to implement a reverse function from scratch.

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?

That was my solution, except in Java.

I like this one. This is how would write up. The current programming structure looks really complex and it does not have to be this way. thanks for posting here so everyone knows. I am picking up programming after several years so atleast I can tell one way or other but newbies may not. thanks again