Back In the print window choose "Save as PDF" as the destination
NOTE.CS

Reverse an Array

1 topic · 28 Nov 2026
1

Reverse an Array ★★★★★

Problem Solving
Problem: Reverse the elements of an array.
Solution 1: Two pointer [BEST] · C++
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int l = 0, r = n - 1;
while(l < r){
    swap(arr[l], arr[r]);
    l++; r--;
}
for(int i = 0; i < n; i++) cout << arr[i] << " ";
Solution 2: STL reverse · C++
reverse(arr, arr + n);