Distance of nearest 0 in binary matrix in JavaScript

JavascriptWeb DevelopmentFront End Technology

A binary matrix is an array of arrays containing only 0 or 1. We are required to write a JavaScript function that takes in a binary matrix as the only argument.

Our function should create a new matrix containing the same number of rows and columns, and for each element of the original matrix the resulting matrix should contain that element's nearest distance from 0 in the original matrix.

We have to keep in mind that while calculating distance it can move either horizontally or vertically and not diagonally. And it's guaranteed that the matrix contains at least one 0.

For example −

If the input matrix is −

const arr = [
   [0, 0, 0]
   [0, 1, 0]
   [1, 1, 1]
];

Then the output matrix should be −

const output = [
   [0, 0, 0]
   [0, 1, 0]
   [1, 2, 1]
];

Example

The code for this will be −

 Live Demo

const arr = [
   [0, 0, 0],
   [0, 1, 0],
   [1, 1, 1],
];
const findNearestDistance = (arr = []) => {
   let array = [];
   let res = arr.map((el, ind) => el.map((subEl, subInd) => {
      if (subEl === 0) {
         array.push([ind, subInd])
         return 0
      };
      return Number.MAX_SAFE_INTEGER;
   }));
   const updateAdjacent = (ind, subInd, min, array = []) => {
      if (ind < 0 || subInd < 0 || ind == arr.length || subInd == arr[0].length){
         return;
      };
      if (res[ind][subInd] < min + 2) return
         res[ind][subInd] = min + 1
         array.push([ind, subInd])
   };
   while (array.length) {
      let next = []
      for (let [ind, subInd] of array) {
         updateAdjacent(ind, subInd + 1, res[ind][subInd], next)
         updateAdjacent(ind, subInd - 1, res[ind][subInd], next)
         updateAdjacent(ind + 1, subInd, res[ind][subInd], next)
         updateAdjacent(ind - 1, subInd, res[ind][subInd], next)
      };
      array = next;
   }
   return res;
};
console.log(findNearestDistance(arr));

Output

And the output in the console will be −

[ [ 0, 0, 0 ], [ 0, 1, 0 ], [ 1, 2, 1 ] ]
raja
Published on 03-Mar-2021 05:32:09
Advertisements