Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,037 questions

40,877 answers

573 users

How to find the number of sorted rows in a matrix with Node.js

1 Answer

0 votes
function isRowSorted(matrix, row) {
    const cols = matrix[0].length;
    
    for (let i = 0; i < cols; i++) {
        if (matrix[row][i - 1] > matrix[row][i]) {
            return false;
        }
    }
    return true;
}

const matrix = [[ 4,  7,  9, 12], 
                [ 1,  8,  3,  4], 
                [-9, -4, -3,  2], 
                [-8, -3, -9,  4], 
                [ 2,  6,  7, 18]];

const rows = matrix.length;
let count = 0;

for (let i = 0; i < rows; i++) {
    if (isRowSorted(matrix, i)) {
        count++;
    }
}

console.log("Total sorted rows: " + count);




/*
run:

Total sorted rows: 3

*/

 





answered Jun 24, 2023 by avibootz
...