# Reverse an Array with out altering place of zeroes

Given an array arr[] and N, which is the scale of this array, the duty is to reverse this array with out altering the place of zeroes on this array.

Examples:

Enter: arr[] = {0, 3, 0, 6, 0, 8}, N = 6
Output: [0, 8, 0, 6, 0, 3]
Rationalization: The place of the zeroes shouldn’t be disturbed.

Enter: arr[] = {2, 0, 5, 0, 7, 0, 3}, N = 6
Output: [3, 0, 7, 0, 5, 0, 2]

Strategy: To resolve the issue observe the under concept:

The strategy is to traverse the array from each ends and swap the non-zero parts till the center is reached. The place of zeroes stays unchanged throughout this course of. The code makes use of some time loop to skip the zeroes and decrement the index from the tip. As soon as a non-zero factor is discovered, it’s swapped with the non-zero factor from the opposite finish. The swapping continues till the center is reached.

Steps that have been to observe the above strategy:

• Let arr be an array of integers of dimension n.
• Initialize two pointers left and proper to the start and finish of the array, respectively.
• Whereas the left is lower than the proper, repeat the next steps:
• If arr[left] and arr[right] are each non-zero:
• Swap arr[left] and arr[right].
• Increment left and decrement proper.
• If arr[left] is zero, increment left.
• If arr[right] is zero, decrement proper.
• The array arr is now reversed with out altering the place of zeroes.

Under is the code to implement the above steps:

## C++

 `#embrace ` `utilizing` `namespace` `std;` ` `  `void` `printReverse(``int``* arr, ``int` `n)` `{` ` `  `    ` `    ``cout << ``"Authentic array: "``;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``cout << arr[i] << ``" "``;` `    ``}` `    ``cout << endl;` ` `  `    ` `    ` ` `  `    ` `    ` `    ``int` `j = n - 1;` `    ``for` `(``int` `i = 0; i < j; i++) {` ` `  `        ` `        ` `        ``if` `(arr[i] == 0) {` `            ``proceed``;` `        ``}` ` `  `        ` `        ` `        ``whereas` `(j > i && arr[j] == 0) {` ` `  `            ` `            ` `            ``j--;` `        ``}` ` `  `        ` `        ``swap(arr[i], arr[j]);` ` `  `        ` `        ` `        ``j--;` `    ``}` ` `  `    ` `    ``cout << ``"Reversed array: "``;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``cout << arr[i] << ``" "``;` `    ``}` `    ``cout << endl;` `}` ` `  `int` `major()` `{` ` `  `    ` `    ``int` `arr[] = { 2, 0, 5, 0, 7, 0, 3 };` ` `  `    ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` ` `  `    ` `    ``printReverse(arr, N);` ` `  `    ``return` `0;` `}`
Output

```Authentic array: 2 0 5 0 7 0 3
Reversed array: 3 0 7 0 5 0 2 ```

Time complexity: O(N)
Auxiliary House: O(1)