Content of the page:
- Question
- Approach
- Complete Code Solution
Que - Reverse the array
Now we swap the values at index i and index j of the array and increase i by 1 and decrease J by 1.
swap(a[i], a[j]);
i++;
j--;
Round 1 :
When the condition does not satisfy that i < j , break from the loop and return the final array.
Repeat the same while i is less than j.
while (i < j)
{
swap(a[i], a[j]);
i++;
j--;
}
Round 2 :
Final Array :
Complete Code :
vector<int> reverseArray(vector<int> a)
{
int i = 0;
int j = a.size() - 1;
while (i < j)
{
swap(a[i], a[j]);
i++;
j--;
}
return a;
}
0 Comments