Remove leading zeros from an array
Last Updated :
13 Dec, 2022
Given an array of N numbers, the task is to remove all leading zeros from the array.
Examples:
Input : arr[] = {0, 0, 0, 1, 2, 3}
Output : 1 2 3
Input : arr[] = {0, 0, 0, 1, 0, 2, 3}
Output : 1 0 2 3
Approach: Mark the first non-zero number’s index in the given array. Store the numbers from that index to the end in a different array. Print the array once all numbers have been stored in a different container.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void removeZeros( int a[], int n)
{
int ind = -1;
for ( int i = 0; i < n; i++) {
if (a[i] != 0) {
ind = i;
break ;
}
}
if (ind == -1) {
cout << "Array has leading zeros only" ;
return ;
}
int b[n - ind];
for ( int i = 0; i < n - ind; i++)
b[i] = a[ind + i];
for ( int i = 0; i < n - ind; i++)
cout << b[i] << " " ;
}
int main()
{
int a[] = { 0, 0, 0, 1, 2, 0, 3 };
int n = sizeof (a) / sizeof (a[0]);
removeZeros(a, n);
return 0;
}
|
Java
import java.util.*;
class solution
{
static void removeZeros( int [] a, int n)
{
int ind = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] != 0 ) {
ind = i;
break ;
}
}
if (ind == - 1 ) {
System.out.print( "Array has leading zeros only" );
return ;
}
int [] b = new int [n - ind];
for ( int i = 0 ; i < n - ind; i++)
b[i] = a[ind + i];
for ( int i = 0 ; i < n - ind; i++)
System.out.print(b[i]+ " " );
}
public static void main(String args[])
{
int [] a = { 0 , 0 , 0 , 1 , 2 , 0 , 3 };
int n = a.length;
removeZeros(a, n);
}
}
|
Python3
def removeZeros(a, n):
ind = - 1 ;
for i in range (n):
if (a[i] ! = 0 ):
ind = i;
break ;
if (ind = = - 1 ):
print ( "Array has leading zeros only" );
return ;
b = [ 0 ] * (n - ind);
for i in range (n - ind):
b[i] = a[ind + i];
for i in range (n - ind):
print ( b[i] , end = " " );
a = [ 0 , 0 , 0 , 1 , 2 , 0 , 3 ];
n = len (a);
removeZeros(a, n);
|
C#
using System;
class solution
{
static void removeZeros( int [] a, int n)
{
int ind = -1;
for ( int i = 0; i < n; i++)
{
if (a[i] != 0)
{
ind = i;
break ;
}
}
if (ind == -1)
{
Console.Write( "Array has leading zeros only" );
return ;
}
int [] b = new int [n - ind];
for ( int i = 0; i < n - ind; i++)
b[i] = a[ind + i];
for ( int i = 0; i < n - ind; i++)
Console.Write(b[i]+ " " );
}
public static void Main(String []args)
{
int [] a = { 0, 0, 0, 1, 2, 0, 3 };
int n = a.Length;
removeZeros(a, n);
}
}
|
PHP
<?php
function removeZeros( $a , $n )
{
$ind = -1;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] != 0)
{
$ind = $i ;
break ;
}
}
if ( $ind == -1)
{
echo "Array has leading " .
"zeros only" ;
return ;
}
for ( $i = 0; $i < $n - $ind ; $i ++)
$b [ $i ] = $a [ $ind + $i ];
for ( $i = 0; $i < $n - $ind ; $i ++)
echo $b [ $i ] , " " ;
}
$a = array (0, 0, 0, 1, 2, 0, 3);
$n = sizeof( $a );
removeZeros( $a , $n );
?>
|
Javascript
<script>
function removeZeros(a , n) {
var ind = -1;
for (i = 0; i < n; i++) {
if (a[i] != 0) {
ind = i;
break ;
}
}
if (ind == -1) {
document.write( "Array has leading zeros only" );
return ;
}
var b = Array(n - ind).fill(0);
for (i = 0; i < n - ind; i++)
b[i] = a[ind + i];
for (i = 0; i < n - ind; i++)
document.write(b[i] + " " );
}
var a = [ 0, 0, 0, 1, 2, 0, 3 ];
var n = a.length;
removeZeros(a, n);
</script>
|
Time complexity: O(n) where n is the size of the given array
Auxiliary space: O(n) because extra space for array b is used
Share your thoughts in the comments
Please Login to comment...