You are viewing a single comment's thread. Return to all comments →
You dont even need to create a new array, we can just manipulate the existing array and O(n) will be half.
See this implementation in C#:-
int n = a.Length;
int lengthToTraverse = a.Length % 2==0?a.Length/2:a.Length/2+1;
for (int i = 0; i < lengthToTraverse; i++)
int temp = a[i];
a[i] = a[n - (i + 1)];
a[n - (i + 1)] = temp;
Here i am dividing input array by 2, if its even then my for loop will traverse lesser than half, if odd then one plus. I have divided the array and will swap the index values, last one will go in first like wise...