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.
The logic is to store the first index value as temp value, set the first index to the last index and last index to temp which is the first index that we replaced; then increment the index (u) but decrement the last index (o). So, at first it is like this: arr[0] = arr[last_index], then it will keep going like this: arr[1] = arr[last_index - 1], (2, last_index - 2), and so on... However, when it comes to middle index value, the loop will break since I set the condition as first index (u) < (num / 2). Otherwise, it would replace rest of the array values to the old versions which will end up getting the same (first version of) array.
Array Reversal
You are viewing a single comment's thread. Return to all comments →
The logic is to store the first index value as temp value, set the first index to the last index and last index to temp which is the first index that we replaced; then increment the index (u) but decrement the last index (o). So, at first it is like this: arr[0] = arr[last_index], then it will keep going like this: arr[1] = arr[last_index - 1], (2, last_index - 2), and so on... However, when it comes to middle index value, the loop will break since I set the condition as first index (u) < (num / 2). Otherwise, it would replace rest of the array values to the old versions which will end up getting the same (first version of) array.