Distance of closest zero to every element
Last Updated :
18 Sep, 2023
Given an array of n integers, for each element, print the distance to the closest zero. Array has a minimum of 1 zero in it.
Examples:
Input: 5 6 0 1 -2 3 4
Output: 2 1 0 1 2 3 4
Explanation : The nearest 0(indexed 2) to
5(indexed 0) is at a distance of 2, so we
print 2. Same is done for the rest of elements.
Naive Approach:
A naive approach is, for every element, slide towards left and find out the nearest 0 and again slide towards the right to find out the nearest zero if any, and print the minimum of both the distances. It will be space efficient but the time complexity will be high as we have to iterate for every element till we find the 0, and in worst case we may not find in one direction.
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Efficient Approach:
An efficient approach is to use sliding window technique two time. One is traversing from right to left and other from left right.
Initialize ans[0] with a max value. Iterate over array from left to right. If value in current position is 0, then set distance to 0, otherwise increase distance by 1. In each step, write value of distance to the answer array.
Do the same thing but going from right to left. This will find closest zero to the right. Now we should store the minimum of current value of distance and value that’s already in answer array.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void print_distance( int arr[], int n)
{
int ans[n];
memset (arr, 0, sizeof (arr[0]));
if (arr[0] == 0)
ans[0] = 0;
else
ans[0] = INT_MAX;
for ( int i = 1; i < n; ++i) {
ans[i] = ans[i - 1] + 1;
if (arr[i] == 0)
ans[i] = 0;
}
if (arr[n - 1] == 0)
ans[n - 1] = 0;
for ( int i = n - 2; i >= 0; --i) {
ans[i] = min(ans[i], ans[i + 1] + 1);
if (arr[i] == 0)
ans[i] = 0;
}
for ( int i = 0; i < n; ++i)
cout << ans[i] << " " ;
}
int main()
{
int a[] = { 2, 1, 0, 3, 0, 0, 3, 2, 4 };
int n = sizeof (a) / sizeof (a[0]);
print_distance(a, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static void print_distance( int arr[], int n)
{
int ans[]= new int [n];
Arrays.fill(ans, 0 );
if (arr[ 0 ] == 0 )
ans[ 0 ] = 0 ;
else
ans[ 0 ] = + 2147483647 ;
for ( int i = 1 ; i < n; ++i)
{
ans[i] = ans[i - 1 ] + 1 ;
if (arr[i] == 0 )
ans[i] = 0 ;
}
if (arr[n - 1 ] == 0 )
ans[n - 1 ] = 0 ;
for ( int i = n - 2 ; i >= 0 ; --i)
{
ans[i] = Math.min(ans[i], ans[i + 1 ] + 1 );
if (arr[i] == 0 )
ans[i] = 0 ;
}
for ( int i = 0 ; i < n; ++i)
System.out.print(ans[i] + " " );
}
public static void main (String[] args)
{
int a[] = { 2 , 1 , 0 , 3 , 0 , 0 , 3 , 2 , 4 };
int n = a.length;
print_distance(a, n);
}
}
|
Python3
def print_distance(arr, n):
ans = [ 0 for i in range (n)]
if (arr[ 0 ] = = 0 ):
ans[ 0 ] = 0
else :
ans[ 0 ] = 10 * * 9
for i in range ( 1 , n):
ans[i] = ans[i - 1 ] + 1
if (arr[i] = = 0 ):
ans[i] = 0
if (arr[n - 1 ] = = 0 ):
ans[n - 1 ] = 0
for i in range (n - 2 , - 1 , - 1 ):
ans[i] = min (ans[i], ans[i + 1 ] + 1 )
if (arr[i] = = 0 ):
ans[i] = 0
for i in ans:
print (i, end = " " )
a = [ 2 , 1 , 0 , 3 , 0 , 0 , 3 , 2 , 4 ]
n = len (a)
print_distance(a, n)
|
C#
using System;
class GFG
{
static void print_distance( int []arr, int n)
{
int []ans= new int [n];
for ( int i = 0; i < n; i++)
ans[i] = 0;
if (arr[0] == 0)
ans[0] = 0;
else
ans[0] = +2147483646;
for ( int i = 1; i < n; ++i)
{
ans[i] = ans[i - 1] + 1;
if (arr[i] == 0)
ans[i] = 0;
}
if (arr[n - 1] == 0)
ans[n - 1] = 0;
for ( int i = n - 2; i >= 0; --i)
{
ans[i] = Math.Min(ans[i], ans[i + 1] + 1);
if (arr[i] == 0)
ans[i] = 0;
}
for ( int i = 0; i < n; ++i)
Console.Write(ans[i] + " " );
}
public static void Main (String[] args)
{
int []a = { 2, 1, 0, 3, 0, 0, 3, 2, 4 };
int n = a.Length;
print_distance(a, n);
}
}
|
PHP
<?php
function print_distance( $arr , $n )
{
$ans [ $n ] = array ();
$ans = array_fill (0, $n , true);
if ( $arr [0] == 0)
$ans [0] = 0;
else
$ans [0] = PHP_INT_MAX;
for ( $i = 1; $i < $n ; ++ $i )
{
$ans [ $i ] = $ans [ $i - 1] + 1;
if ( $arr [ $i ] == 0)
$ans [ $i ] = 0;
}
if ( $arr [ $n - 1] == 0)
$ans [ $n - 1] = 0;
for ( $i = $n - 2; $i >= 0; -- $i )
{
$ans [ $i ] = min( $ans [ $i ], $ans [ $i + 1] + 1);
if ( $arr [ $i ] == 0)
$ans [ $i ] = 0;
}
for ( $i = 0; $i < $n ; ++ $i )
echo $ans [ $i ] , " " ;
}
$a = array ( 2, 1, 0, 3, 0, 0, 3, 2, 4 );
$n = sizeof( $a );
print_distance( $a , $n );
?>
|
Javascript
Time complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...