Problem 2 : Array C++ Dsa Reverse an array without using the extra space. #array #programming

preview_player
Показать описание
Here is a description of how to reverse an array without using extra space in C++:

*Algorithm:*

1. Initialize two pointers, `start` and `end`, to the beginning and end of the array respectively.

2. Swap the elements at the `start` and `end` indices.

3. Increment `start` and decrement `end` until they meet or cross each other.

*Explanation:*

- The `reverseArray` function takes an array `arr` and its size `n` as input.
- It initializes two pointers, `start` and `end`, to the beginning and end of the array.
- The function swaps elements at the `start` and `end` indices and moves the pointers towards each other until they meet or cross.
- The `printArray` function prints the array elements.
- In the `main` function, an example array is reversed and printed.

*Time Complexity:* O(n/2) = O(n)

*Space Complexity:* O(1) (no extra space used)

This algorithm efficiently reverses the array in-place without using additional storage.

#coding #array #programming #c++ #dsa
Рекомендации по теме