Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.6 kB
2
Indexable
Never
function specificDiagonals(R, C, matrix, K){
    var row = -1,col = -1;
    var isFound = false;
    var s1 = "";
    var s2 = "";
    
    for(var i=0; i<R; i++) {
        if(isFound === true) {
            break;
        }
        for(var j=0; j<C; j++) {
            if(matrix[i][j] === K) {
                row = i;
                col = j;
                
                break;
            }
        }
    }
    var p = row, q = col;
     var r1=-1, c1=-1;
//   Primary Diagonal
  while(p >= 0 && q >= 0) {
        if(p === 0 || q === 0) {
            r1 = p;
            c1 = q;
          
            break; 
        }     
        p--;
        q--;
  }
  
     var r2 = -1, c2=-1;
//   Secondary Diagonal
while(row >= 0 && col <= C-1) {
    if(row === 0 || col === C-1) {
            r2 = row;
            c2 = col;
            
            break;
        }     
        row--;
        col++;
}


primaryDiag(matrix, R, C, r1, c1, s1);
secondaryDiag(matrix, R, C, r2, c2, s2);
}

function primaryDiag(matrix, R, C, r1, c1, s1) {
    
    while(r1<R || c1<C) {
        if(r1=== R-1 || c1 === C-1){
             s1 += matrix[r1][c1] + " ";  
            break;
        }
            
        s1 += matrix[r1][c1] + " ";            
        
        ++r1;
        ++c1;
    }
    console.log(s1);
}

function secondaryDiag(matrix, R, C, r2, c2, s2) {
       while(r2<R || c2>0) {
              if(r2=== R-1 || c2 === 0){
             s2 += matrix[r2][c2] + " ";  
            break;
        }
            
            s2 += matrix[r2][c2] + " ";            
        
        ++r2;
        --c2;
    }
    console.log(s2);
}