3. What will be the output of the following code snippet for
n = 4?
function isSafe(board, row, col) {
for (let i = 0; i < row; i++) {
if (board[i] === col || Math.abs(board[i] - col) === row - i) return false;
}
return true;
}
function solveNQueens(n) {
const board = Array(n).fill(-1);
const results = [];
function backtrack(row) {
if (row === n) {
results.push(board.slice());
return;
}
for (let col = 0; col < n; col++) {
if (isSafe(board, row, col)) {
board[row] = col;
backtrack(row + 1);
board[row] = -1;
}
}
}
backtrack(0);
return results.length;
}
console.log(solveNQueens(4));