
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Distance of nearest 0 in binary matrix in JavaScript
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 −
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 ] ]
- Related Articles
- Nearest 1 in a binary matrix in C++
- Program to generate matrix where each cell holds Manhattan distance from nearest 0 in Python
- Distance to nearest vowel in a string - JavaScript
- Longest distance between 1s in binary JavaScript
- Nearest palindrome in JavaScript
- Find Column Which is Having Maximum 0’s in a Binary Matrix in Java
- JavaScript Program for Counting sets of 1s and 0s in a binary matrix
- JavaScript Program to Check horizontal and vertical symmetry in binary matrix
- Finding nearest Gapful number in JavaScript
- Shortest Path in Binary Matrix in C++
- How to convert a matrix to binary matrix in R?
- Minimum Number of Flips to Convert Binary Matrix to Zero Matrix in C++
- Finding points nearest to origin in JavaScript
- Python Program to find the Next Nearest element in a Matrix
- How to convert a binary matrix to logical matrix in R?

Advertisements