#C14363. Minimum Distance to Ones
Minimum Distance to Ones
Minimum Distance to Ones
You are given a binary array of length n. Your task is to compute for each element the shortest distance to the nearest 1 in the array. For an element that is a 1 its distance is 0. If there are no 1s in the array, then the distance for every element is defined as n.
The distance between two indices i and j is given by \( |i - j| \) in \( \LaTeX \) format. This problem can be efficiently solved using a Breadth-First Search (BFS) approach.
Input Format: The first line contains an integer n indicating the size of the array. The second line contains n space-separated integers which are either 0 or 1.
Output Format: Output a single line with n space-separated integers, where the i-th integer represents the minimum distance from index i to the nearest index with value 1.
inputFormat
The first line contains an integer n (the number of elements in the binary array). The second line contains n space-separated integers, where each integer is either 0 or 1.
outputFormat
Print a single line containing n space-separated integers. Each integer represents the minimum distance from that position to the nearest 1 in the array. If there is no 1 in the array, output n for every position.
## sample6
0 0 1 0 1 0
2 1 0 1 0 1