Reverse an Array without changing position of zeroes
Given an array arr[] and N, which is the size of this array, the task is to reverse this array without changing the position of zeroes in this array.
Examples:
Input: arr[] = {0, 3, 0, 6, 0, 8}, N = 6
Output: [0, 8, 0, 6, 0, 3]
Explanation: The position of the zeroes is not disturbed.
Input: arr[] = {2, 0, 5, 0, 7, 0, 3}, N = 6
Output: [3, 0, 7, 0, 5, 0, 2]
Approach: To solve the problem follow the below idea:
The approach is to traverse the array from both ends and swap the non-zero elements until the middle is reached. The position of zeroes remains unchanged during this process. The code uses a while loop to skip the zeroes and decrement the index from the end. Once a non-zero element is found, it is swapped with the non-zero element from the other end. The swapping continues until the middle is reached.
Steps that were to follow the above approach:
- Let arr be an array of integers of size n.
- Initialize two pointers left and right to the beginning and end of the array, respectively.
- While the left is less than the right, repeat the following steps:
- If arr[left] and arr[right] are both non-zero:
- Swap arr[left] and arr[right].
- Increment left and decrement right.
- If arr[left] is zero, increment left.
- If arr[right] is zero, decrement right.
- The array arr is now reversed without changing the position of zeroes.
Below is the code to implement the above steps:
C++
#include <bits/stdc++.h>
using namespace std;
void printReverse( int * arr, int n)
{
cout << "Original 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) {
continue ;
}
while (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 main()
{
int arr[] = { 2, 0, 5, 0, 7, 0, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
printReverse(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
public class ReverseArrayWithoutZeros {
public static void printReverse( int [] arr) {
System.out.print( "Original array: " );
System.out.println(Arrays.toString(arr));
int j = arr.length - 1 ;
for ( int i = 0 ; i < j; i++) {
if (arr[i] == 0 ) {
continue ;
}
while (j > i && arr[j] == 0 ) {
j--;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;
}
System.out.print( "Reversed array: " );
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
int [] arr = { 2 , 0 , 5 , 0 , 7 , 0 , 3 };
printReverse(arr);
}
}
|
Python3
def printReverse(arr, n):
print ( "Original array:" , end = " " )
for i in range (n):
print (arr[i], end = " " )
print ()
j = n - 1
i = 0
while i < j:
if arr[i] = = 0 :
i + = 1
continue
while j > i and arr[j] = = 0 :
j - = 1
arr[i], arr[j] = arr[j], arr[i]
j - = 1
i + = 1
print ( "Reversed array:" , end = " " )
for i in range (n):
print (arr[i], end = " " )
print ()
arr = [ 2 , 0 , 5 , 0 , 7 , 0 , 3 ]
N = len (arr)
printReverse(arr, N)
|
C#
using System;
public class GFG {
static void printReverse( int [] arr)
{
Console.Write( "Original array: " );
Console.WriteLine( string .Join( ", " , arr));
int j = arr.Length - 1;
for ( int i = 0; i < j; i++) {
if (arr[i] == 0) {
continue ;
}
while (j > i && arr[j] == 0) {
j--;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;
}
Console.Write( "Reversed array: " );
Console.WriteLine( string .Join( ", " , arr));
}
static public void Main()
{
int [] arr = { 2, 0, 5, 0, 7, 0, 3 };
printReverse(arr);
}
}
|
Javascript
function printReverse(arr, n) {
console.log( "Original array: " + arr.join( " " ));
let j = n - 1;
for (let i = 0; i < j; i++) {
if (arr[i] === 0) {
continue ;
}
while (j > i && arr[j] === 0) {
j--;
}
[arr[i], arr[j]] = [arr[j], arr[i]];
j--;
}
console.log( "Reversed array: " + arr.join( " " ));
}
const arr = [2, 0, 5, 0, 7, 0, 3];
const N = arr.length;
printReverse(arr, N);
|
Output
Original array: 2 0 5 0 7 0 3
Reversed array: 3 0 7 0 5 0 2
Time complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
10 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...