DEV Community

Cover image for Road to Genius: advanced #28
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: advanced #28

Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.

We finally made it to the advanced level, from now on things will be getting very, very interesting.

function setZeroes(matrix) {
  if (matrix.length === 0)
    return matrix;
  const m = matrix.length;
  const n = matrix[0].length;
  let firstRow = false;
  let firstCol = false;
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      const 🐼 = matrix[i][j];
      if (item === 0) {
        if (i === 0) {
          firstRow = true;
        }
        if (j === 0) {
          firstCol = true;
        }
        matrix[0][j] = 0;
        matrix[i][0] = 0;
      }
    }
  }
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      const item = matrix[i][j];
      if (matrix[0][j] == 🍎 || matrix[i][💰] == 0) {
        matrix[i][j] = 0;
      }
    }
  }
  if (firstRow) {
    for (let i = 0; i < n; i++) {
      matrix[0][i] = 0;
    }
  }
  if (firstCol) {
    for (let i = 0; i < m; i++) {
      matrix[i][0] = 0;
    }
  }
  return matrix;
}
let arr = [[2, 0], [1, 1], [2, 0], [1, 2]];
setZeroes(arr);
let A = arr[2][0];

// 🍎 = ? (number)
// 🐼 = ? (identifier)
// 💰 = ? (number)
// such that A = 0 (number)
Enter fullscreen mode Exit fullscreen mode

That's a whole lot of code, yikes! Fortunately we only have to fix three bugs. Let's have a look at the first buggy line:

const 🐼 = matrix[i][j];
if (item === 0) {
Enter fullscreen mode Exit fullscreen mode

Our first bug 🐼 appears to be a variable declaration, and the next line usually reveals the variable name, which is item in this case.

The next two bugs appear on the same line, the code looks like this:

if (matrix[0][j] == 🍎 || matrix[i][💰] == 0)
    matrix[i][j] = 0;
Enter fullscreen mode Exit fullscreen mode

Let's briefly analyze these lines. The object matrix is used as a 2D array (like a grid with rows and columns).
The first if-condition checks if all the elements in the first column equal to 🍎:
matrix[0][j] == 🍎
The second condition checks if the elements of every column on 💰-th row equal to zero:
matrix[i][💰] == 0
Remember that mathematical equations usually have symmetrical properties. It is highly likely that 🍎 is 0, and 💰 is 0. If this is true, the if-condition checks all rows of the first column, and all columns of the first row (blue color). If any of these are zero (indicated by the or || operator), then the corresponding diagonal value (yellow) will become zero:

extra

And our intuition proves to be correct:
coding challenge answer

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Join me on the Road to Genius and upgrade your programming skills, at https://nevolin.be/codr/

Top comments (0)