- 🧩 Problem link: Amazon HackerRank Demo OA practice test
- 🚦 Difficulty: 🟡 Medium
-
The function applies a sequence of reverse operations on an array.
-
For each operation
[left, right]:- Reverse the subarray between indices
leftandright(inclusive). - This is done with a two-pointer swap until
left >= right.
- Reverse the subarray between indices
-
After applying all operations, return the final modified array.
- Time: O(m * n)
- Space: O(1)
Where
nisoperations.size()andmisarr.size()
vector<int> performOperations(vector<int> arr, vector<vector<int>> operations)
{
for (vector<int> curOp : operations) // O(n)
{
int left = curOp[0], right = curOp[1];
while (left < right) // O(m)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
return arr;
};