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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,086 questions

55,959 answers

573 users

How to check if a given row is sorted 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]];
        
console.log("Row 0: " + isRowSorted(matrix, 0));
console.log("Row 1: " + isRowSorted(matrix, 1));
console.log("Row 2: " + isRowSorted(matrix, 2));
console.log("Row 3: " + isRowSorted(matrix, 3));
console.log("Row 4: " + isRowSorted(matrix, 4));




/*
run:

Row 0: true
Row 1: false
Row 2: true
Row 3: false
Row 4: true

*/

 



answered Jun 24, 2023 by avibootz
...