codeparrot/xlcost-text-to-code huggingface dataset:
Find difference between sums of two diagonals | Javascript Code for Find difference between sums of two diagonals ; Initialize sums of diagonals ; finding sum of primary diagonal ; finding sum of secondary diagonal ; Absolute difference of the sums across the diagonals ; Driver program to test above function
function difference ( arr , n ) { let d1 = 0 , d2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( i == j ) d1 += arr [ i ] [ j ] ; if ( i == n - j - 1 ) d2 += arr [ i ] [ j ] ; } } return Math . abs ( d1 - d2 ) ; } let n = 3 ; let arr = [ [ 11 , 2 , 4 ] , [ 4 , 5 , 6 ] , [ 10 , 8 , - 12 ] ] ; document . write ( difference ( arr , n ) ) ;
Find difference between sums of two diagonals | JAVA SCRIPT Code for Find difference between sums of two diagonals ; Initialize sums of diagonals ; Absolute difference of the sums across the diagonals ; Driver program to test above function
function difference ( arr , n ) { let d1 = 0 , d2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { d1 += arr [ i ] [ i ] ; d2 += arr [ i ] [ n - i - 1 ] ; } return Math . abs ( d1 - d2 ) ; } let n = 3 ; let arr = [ [ 11 , 2 , 4 ] , [ 4 , 5 , 6 ] , [ 10 , 8 , - 12 ] ] ; document . write ( difference ( arr , n ) ) ;
Circular Matrix ( Construct a matrix with numbers 1 to m * n in spiral way ) | Javascript program to fill a matrix with values from 1 to n * n in spiral fashion . ; Fills a [ m ] [ n ] with values from 1 to m * n in spiral fashion . ; Initialize value to be filled in matrix ; k - starting row index m - ending row index l - starting column index n - ending column index ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Driver program to test above functions
const MAX = 100 ; function spiralFill ( m , n , a ) { let val = 1 ; let k = 0 , l = 0 ; while ( k < m && l < n ) { for ( let i = l ; i < n ; ++ i ) a [ k ] [ i ] = val ++ ; k ++ ; for ( let i = k ; i < m ; ++ i ) a [ i ] [ n - 1 ] = val ++ ; n -- ; if ( k < m ) { for ( let i = n - 1 ; i >= l ; -- i ) a [ m - 1 ] [ i ] = val ++ ; m -- ; } if ( l < n ) { for ( let i = m - 1 ; i >= k ; -- i ) a [ i ] [ l ] = val ++ ; l ++ ; } } } let m = 4 , n = 4 ; let a = Array . from ( Array ( MAX ) , ( ) => new Array ( MAX ) ) ; spiralFill ( m , n , a ) ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) document . write ( a [ i ] [ j ] + " " ) ; document . write ( " " ) ; }
Maximum and Minimum in a square matrix . | Javascript program for finding maximum and minimum in a matrix . ; Finds maximum and minimum in arr [ 0. . n - 1 ] [ 0. . n - 1 ] using pair wise comparisons ; Traverses rows one by one ; Compare elements from beginning and end of current row ; Driver Code
let MAX = 100 ; function maxMin ( arr , n ) { let min = + 2147483647 ; let max = - 2147483648 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j <= n / 2 ; j ++ ) { if ( arr [ i ] [ j ] > arr [ i ] [ n - j - 1 ] ) { if ( min > arr [ i ] [ n - j - 1 ] ) min = arr [ i ] [ n - j - 1 ] ; if ( max < arr [ i ] [ j ] ) max = arr [ i ] [ j ] ; } else { if ( min > arr [ i ] [ j ] ) min = arr [ i ] [ j ] ; if ( max < arr [ i ] [ n - j - 1 ] ) max = arr [ i ] [ n - j - 1 ] ; } } } document . write ( " " + max + " " + min ) ; } let arr = [ [ 5 , 9 , 11 ] , [ 25 , 0 , 14 ] , [ 21 , 6 , 4 ] ] ; maxMin ( arr , 3 ) ;
Print matrix in antispiral form | Javascript Code for Print matrix in antispiral form ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Driver program to test above function
function antiSpiralTraversal ( m , n , a ) { let i , k = 0 , l = 0 ; let stk = [ ] ; while ( k <= m && l <= n ) { for ( i = l ; i <= n ; ++ i ) stk . push ( a [ k ] [ i ] ) ; k ++ ; for ( i = k ; i <= m ; ++ i ) stk . push ( a [ i ] [ n ] ) ; n -- ; if ( k <= m ) { for ( i = n ; i >= l ; -- i ) stk . push ( a [ m ] [ i ] ) ; m -- ; } if ( l <= n ) { for ( i = m ; i >= k ; -- i ) stk . push ( a [ i ] [ l ] ) ; l ++ ; } } while ( stk . length != 0 ) { document . write ( stk [ stk . length - 1 ] + " " ) ; stk . pop ( ) ; } } let mat = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 6 , 7 , 8 , 9 , 10 ] , [ 11 , 12 , 13 , 14 , 15 ] , [ 16 , 17 , 18 , 19 , 20 ] ] ; antiSpiralTraversal ( mat . length - 1 , mat [ 0 ] . length - 1 , mat ) ;
Minimum operations required to set all elements of binary matrix | Javascript program to find minimum operations required to set all the element of binary matrix ; Return minimum operation required to make all 1 s . ; check if this cell equals 0 ; increase the number of moves ; flip from this cell to the start point ; flip the cell ; Driven Program
let N = 5 ; let M = 5 ; function minOperation ( arr ) { let ans = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { for ( let j = M - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] == false ) { ans ++ ; for ( let k = 0 ; k <= i ; k ++ ) { for ( let h = 0 ; h <= j ; h ++ ) { if ( arr [ k ] [ h ] == true ) { arr [ k ] [ h ] = false ; } else { arr [ k ] [ h ] = true ; } } } } } } return ans ; } let mat = [ [ 0 , 0 , 1 , 1 , 1 ] , [ 0 , 0 , 0 , 1 , 1 ] , [ 0 , 0 , 0 , 1 , 1 ] , [ 1 , 1 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 , 1 ] ] document . write ( minOperation ( mat ) )
Sum of matrix element where each elements is integer division of row and column | Return sum of matrix element where each element is division of its corresponding row and column . ; For each column . ; count the number of elements of each column . Initialize to i - 1 because number of zeroes are i - 1. ; For multiply ; Driven Program
function findSum ( n ) { let ans = 0 , temp = 0 , num ; for ( let i = 1 ; i <= n && temp < n ; i ++ ) { temp = i - 1 ; num = 1 ; while ( temp < n ) { if ( temp + i <= n ) ans += ( i * num ) ; else ans += ( ( n - temp ) * num ) ; temp += i ; num ++ ; } } return ans ; } let N = 2 ; document . write ( findSum ( N ) ) ;
Form coils in a matrix | Print coils in a matrix of size 4 n x 4 n ; Number of elements in each coil ; Let us fill elements in coil 1. ; First element of coil1 4 * n * 2 * n + 2 * n ; ; Fill remaining m - 1 elements in coil1 [ ] ; Fill elements of current step from down to up ; Next element from current element ; Fill elements of current step from up to down . ; get coil2 from coil1 ; Print both coils ; Driver code
function printCoils ( n ) { let m = 8 * n * n ; let coil1 = new Array ( m ) ; coil1 . fill ( 0 ) ; coil1 [ 0 ] = 8 * n * n + 2 * n ; let curr = coil1 [ 0 ] ; let nflg = 1 , step = 2 ; let index = 1 ; while ( index < m ) { for ( let i = 0 ; i < step ; i ++ ) { curr = coil1 [ index ++ ] = ( curr - 4 * n * nflg ) ; if ( index >= m ) break ; } if ( index >= m ) break ; for ( let i = 0 ; i < step ; i ++ ) { curr = coil1 [ index ++ ] = curr + nflg ; if ( index >= m ) break ; } nflg = nflg * ( - 1 ) ; step += 2 ; } let coil2 = new Array ( m ) ; coil2 . fill ( 0 ) ; for ( let i = 0 ; i < 8 * n * n ; i ++ ) coil2 [ i ] = 16 * n * n + 1 - coil1 [ i ] ; document . write ( " " ) ; for ( let i = 0 ; i < 8 * n * n ; i ++ ) document . write ( coil1 [ i ] + " " ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < 8 * n * n ; i ++ ) document . write ( coil2 [ i ] + " " ) ; } let n = 1 ; printCoils ( n ) ;
Sum of matrix in which each element is absolute difference of its row and column numbers | Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Generate matrix ; Compute sum ; Driver Code
function findSum ( n ) { let arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { arr [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = Math . abs ( i - j ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) sum += arr [ i ] [ j ] ; return sum ; } let n = 3 ; document . write ( findSum ( n ) ) ;
Sum of matrix in which each element is absolute difference of its row and column numbers | Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driver Code
function findSum ( n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += i * ( n - i ) ; return 2 * sum ; } let n = 3 ; document . write ( findSum ( n ) ) ;
Sum of matrix in which each element is absolute difference of its row and column numbers | Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driver Code
function findSum ( n ) { n -- ; let sum = 0 ; sum += ( n * ( n + 1 ) ) / 2 ; sum += ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; return sum ; } let n = 3 ; document . write ( findSum ( n ) ) ;
Sum of both diagonals of a spiral odd | function returns sum of diagonals ; as order should be only odd we should pass only odd - integers ; Driver program
function spiralDiaSum ( n ) { if ( n == 1 ) return 1 ; return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) ) ; } let n = 7 ; document . write ( spiralDiaSum ( n ) ) ;
Find perimeter of shapes formed with 1 s in binary matrix | JavaScript program to find perimeter of area coverede by 1 in 2D matrix consisits of 0 ' s ▁ and ▁ 1' s ; Find the number of covered side for mat [ i ] [ j ] . ; UP ; LEFT ; DOWN ; RIGHT ; Returns sum of perimeter of shapes formed with 1 s ; Traversing the matrix and finding ones to calculate their contribution . ; Driver Code
let R = 3 ; let C = 5 ; function numofneighbour ( mat , i , j ) { let count = 0 ; if ( i > 0 && mat [ i - 1 ] [ j ] == 1 ) count ++ ; if ( j > 0 && mat [ i ] [ j - 1 ] == 1 ) count ++ ; if ( i < R - 1 && mat [ i + 1 ] [ j ] == 1 ) count ++ ; if ( j < C - 1 && mat [ i ] [ j + 1 ] == 1 ) count ++ ; return count ; } function findperimeter ( mat ) { let perimeter = 0 ; for ( let i = 0 ; i < R ; i ++ ) for ( let j = 0 ; j < C ; j ++ ) if ( mat [ i ] [ j ] == 1 ) perimeter += ( 4 - numofneighbour ( mat , i , j ) ) ; return perimeter ; } let mat = [ [ 0 , 1 , 0 , 0 , 0 ] , [ 1 , 1 , 1 , 0 , 0 ] , [ 1 , 0 , 0 , 0 , 0 ] ] ; document . write ( findperimeter ( mat ) ) ;
Print matrix in diagonal pattern | Javascript program to print matrix in diagonal order ; Initialize indices to traverse through the array ; Direction is initially from down to up ; Traverse the matrix till all elements get traversed ; If isUp = true traverse from bottom to top ; Set i and j according to direction ; If isUp = false then traverse from top to bottom ; Set i and j according to direction ; Inverse the value of isUp to change the direction ; function call
function printMatrixDiagonal ( arr , len ) { let i = 0 , j = 0 ; let isUp = true ; for ( let k = 0 ; k < len * len ; ) { if ( isUp ) { for ( ; i >= 0 && j < len ; i -- , j ++ ) { document . write ( arr [ i ] [ j ] + ' ' ) ; k ++ ; } if ( i < 0 && j < len ) i = 0 ; if ( j === len ) i = i + 2 , j -- ; } else { for ( ; j >= 0 && i < len ; i ++ , j -- ) { document . write ( arr [ i ] [ j ] + ' ' ) ; k ++ ; } if ( j < 0 && i < len ) j = 0 ; if ( i === len ) j = j + 2 , i -- ; } isUp = ! isUp } } let arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; let arrLength = arr . length ; printMatrixDiagonal ( arr , arrLength ) ;
Print matrix in diagonal pattern | Javascript program to print an n * n matrix in diagonal order ; Initialize matrix ; n - size of the array mode - to switch from top / bottom treversal it - iterator count - increases until it reaches n and then decreases lower - to ensure we move to the next row when columns go out of bounds ; A 4 * 4 matrix has 7 diagonals . Hence ( 2 * n - 1 ) iterations ; function call
function MatrixDiag ( arr ) { let arr = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; let n = arr . length , mode = 0 , it = 0 , lower = 0 ; for ( let t = 0 ; t < ( 2 * n - 1 ) ; t ++ ) { let t1 = t ; if ( t1 >= n ) { mode ++ ; t1 = n - 1 ; it -- ; lower ++ ; } else { lower = 0 ; it ++ ; } for ( let i = t1 ; i >= lower ; i -- ) { if ( ( t1 + mode ) % 2 === 0 ) { console . log ( arr [ i ] [ t1 + lower - i ] ) } else { console . log ( arr [ t1 + lower - i ] [ i ] ) } } } } MatrixDiag ( arr )
Maximum difference of sum of elements in two rows in a matrix | Function to find maximum difference of sum of elements of two rows such that second row appears before first row . ; Auxiliary array to store sum of all elements of each row ; Calculate sum of each row and store it in rowSum array ; Calculating maximum difference of two elements such that rowSum [ i ] < rowsum [ j ] ; If current difference is greater than previous then update it ; If new element is less than previous minimum element then update it so that we may get maximum difference in remaining array ; Driver code
function maxRowDiff ( mat , m , n ) { let rowSum = new Array ( m ) ; for ( let i = 0 ; i < m ; i ++ ) { let sum = 0 ; for ( let j = 0 ; j < n ; j ++ ) sum += mat [ i ] [ j ] ; rowSum [ i ] = sum ; } let max_diff = rowSum [ 1 ] - rowSum [ 0 ] ; let min_element = rowSum [ 0 ] ; for ( let i = 1 ; i < m ; i ++ ) { if ( rowSum [ i ] - min_element > max_diff ) max_diff = rowSum [ i ] - min_element ; if ( rowSum [ i ] < min_element ) min_element = rowSum [ i ] ; } return max_diff ; } let m = 5 , n = 4 ; let mat = [ [ - 1 , 2 , 3 , 4 ] , [ 5 , 3 , - 2 , 1 ] , [ 6 , 7 , 2 , - 3 ] , [ 2 , 9 , 1 , 4 ] , [ 2 , 1 , - 2 , 0 ] ] ; document . write ( maxRowDiff ( mat , m , n ) ) ;
Total coverage of all zeros in a binary matrix | Javascript program to get total coverage of all zeros in a binary matrix ; Returns total coverage of all zeros in mat [ ] [ ] ; looping for all rows of matrix ; 1 is not seen yet ; looping in columns from left to right direction to get left ones ; If one is found from left ; If 0 is found and we have found a 1 before . ; Repeat the above process for right to left direction . ; Traversing across columms for up and down directions . ; 1 is not seen yet ; Driver code to test above methods
let R = 4 ; let C = 4 ; function getTotalCoverageOfMatrix ( mat ) { let res = 0 ; for ( let i = 0 ; i < R ; i ++ ) { let isOne = false ; for ( let j = 0 ; j < C ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) isOne = true ; else if ( isOne ) res ++ ; } isOne = false ; for ( let j = C - 1 ; j >= 0 ; j -- ) { if ( mat [ i ] [ j ] == 1 ) isOne = true ; else if ( isOne ) res ++ ; } } for ( let j = 0 ; j < C ; j ++ ) { let isOne = false ; for ( let i = 0 ; i < R ; i ++ ) { if ( mat [ i ] [ j ] == 1 ) isOne = true ; else if ( isOne ) res ++ ; } isOne = false ; for ( let i = R - 1 ; i >= 0 ; i -- ) { if ( mat [ i ] [ j ] == 1 ) isOne = true ; else if ( isOne ) res ++ ; } } return res ; } let mat = [ [ 0 , 0 , 0 , 0 ] , [ 1 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 0 ] , [ 0 , 1 , 0 , 0 ] ] ; document . write ( getTotalCoverageOfMatrix ( mat ) ) ;
Count all sorted rows in a matrix | Javascript program to find number of sorted rows ; Function to count all sorted rows in a matrix ; Initialize result ; Start from left side of matrix to count increasing order rows ; Check if there is any pair ofs element that are not in increasing order . ; If the loop didn 't break (All elements of current row were in increasing order) ; Start from right side of matrix to count increasing order rows ( reference to left these are in decreasing order ) ; Check if there is any pair ofs element that are not in decreasing order . ; Note c > 1 condition is required to make sure that a single column row is not counted twice ( Note that a single column row is sorted both in increasing and decreasing order ) ; Driver code
let MAX = 100 ; function sortedCount ( mat , r , c ) { let result = 0 ; for ( let i = 0 ; i < r ; i ++ ) { let j ; for ( j = 0 ; j < c - 1 ; j ++ ) if ( mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] ) break ; if ( j == c - 1 ) result ++ ; } for ( let i = 0 ; i < r ; i ++ ) { let j ; for ( j = c - 1 ; j > 0 ; j -- ) if ( mat [ i ] [ j - 1 ] <= mat [ i ] [ j ] ) break ; if ( c > 1 && j == 0 ) result ++ ; } return result ; } let m = 4 , n = 5 ; let mat = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 4 , 3 , 1 , 2 , 6 ] , [ 8 , 7 , 6 , 5 , 4 ] , [ 5 , 7 , 8 , 9 , 10 ] ] document . write ( sortedCount ( mat , m , n ) )
Maximum XOR value in matrix | Javascript program to Find maximum XOR value in matrix either row / column wise maximum number of row and column ; function return the maximum xor value that is either row or column wise ; for row xor and column xor ; traverse matrix ; xor row element ; for each column : j is act as row & i act as column xor column element ; update maximum between r_xor , c_xor ; return maximum xor value ; driver Code
const MAX = 1000 ; function maxXOR ( mat , N ) { let r_xor , c_xor ; let max_xor = 0 ; for ( let i = 0 ; i < N ; i ++ ) { r_xor = 0 , c_xor = 0 ; for ( let j = 0 ; j < N ; j ++ ) { r_xor = r_xor ^ mat [ i ] [ j ] ; c_xor = c_xor ^ mat [ j ] [ i ] ; } if ( max_xor < Math . max ( r_xor , c_xor ) ) max_xor = Math . max ( r_xor , c_xor ) ; } return max_xor ; } let N = 3 ; let mat = [ [ 1 , 5 , 4 ] , [ 3 , 7 , 2 ] , [ 5 , 9 , 10 ] ] ; document . write ( " " + maxXOR ( mat , N ) ) ;
Direction at last square block | Function which tells the Current direction ; Driver code
function direction ( R , C ) { if ( R != C && R % 2 == 0 && C % 2 != 0 && R < C ) { document . write ( " " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 == 0 && R > C ) { document . write ( " " ) ; return ; } if ( R == C && R % 2 != 0 && C % 2 != 0 ) { document . write ( " " ) ; return ; } if ( R == C && R % 2 == 0 && C % 2 == 0 ) { document . write ( " " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 != 0 && R < C ) { document . write ( " " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 != 0 && R > C ) { document . write ( " " ) ; return ; } if ( R != C && R % 2 == 0 && C % 2 == 0 && R < C ) { document . write ( " " ) ; return ; } if ( R != C && R % 2 == 0 && C % 2 == 0 && R > C ) { document . write ( " " ) ; return ; } if ( R != C && R % 2 == 0 && C % 2 != 0 && R > C ) { document . write ( " " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 == 0 && R < C ) { document . write ( " " ) ; return ; } } let R = 3 , C = 1 ; direction ( R , C ) ;
Print K 'th element in spiral form of matrix |  ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; check the first row from the remaining rows ; check the last column from the remaining columns ; check the last row from the remaining rows ; check the first column from the remaining columns ; Driver code
let R = 3 ; let C = 6 ; function spiralPrint ( m , n , a , c ) { let i , k = 0 , l = 0 ; let count = 0 ; while ( k < m && l < n ) { for ( i = l ; i < n ; ++ i ) { count ++ ; if ( count == c ) document . write ( a [ k ] [ i ] + " " ) ; } k ++ ; for ( i = k ; i < m ; ++ i ) { count ++ ; if ( count == c ) document . write ( a [ i ] [ n - 1 ] + " " ) ; } n -- ; if ( k < m ) { for ( i = n - 1 ; i >= l ; -- i ) { count ++ ; if ( count == c ) document . write ( a [ m - 1 ] [ i ] + " " ) ; } m -- ; } if ( l < n ) { for ( i = m - 1 ; i >= k ; -- i ) { count ++ ; if ( count == c ) document . write ( a [ i ] [ l ] + " " ) ; } l ++ ; } } } let a = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 , 17 , 18 ] ] , k = 17 ; spiralPrint ( R , C , a , k ) ;
Print K 'th element in spiral form of matrix | JavaScript program for Kth element in spiral form of matrix ; function for Kth element ; Element is in first row ; Element is in last column ; Element is in last row ; Element is in first column ; Recursion for sub - matrix . & A [ 1 ] [ 1 ] is address to next inside sub matrix . ; Driver code
let MAX = 100 ; function findK ( A , i , j , n , m , k ) { if ( n < 1 m < 1 ) return - 1 ; if ( k <= m ) return A [ i + 0 ] [ j + k - 1 ] ; if ( k <= ( m + n - 1 ) ) return A [ i + ( k - m ) ] [ j + m - 1 ] ; if ( k <= ( m + n - 1 + m - 1 ) ) return A [ i + n - 1 ] [ j + m - 1 - ( k - ( m + n - 1 ) ) ] ; if ( k <= ( m + n - 1 + m - 1 + n - 2 ) ) return A [ i + n - 1 - ( k - ( m + n - 1 + m - 1 ) ) ] [ j + 0 ] ; return findK ( A , i + 1 , j + 1 , n - 2 , m - 2 , k - ( 2 * n + 2 * m - 4 ) ) ; } let a = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 , 17 , 18 ] ] ; let k = 17 ; document . write ( findK ( a , 0 , 0 , 3 , 6 , k ) ) ;
Find if given matrix is Toeplitz or not | Javascript program to check whether given matrix is a Toeplitz matrix or not ; Function to check if all elements present in descending diagonal starting from position ( i , j ) in the matrix are all same or not ; mismatch found ; we only reach here when all elements in given diagonal are same ; Function to check whether given matrix is a Toeplitz matrix or not ; do for each element in first row ; check descending diagonal starting from position ( 0 , j ) in the matrix ; do for each element in first column ; check descending diagonal starting from position ( i , 0 ) in the matrix ; we only reach here when each descending diagonal from left to right is same ; Driver code ; Function call
let N = 5 ; let M = 4 ; function checkDiagonal ( mat , i , j ) { let res = mat [ i ] [ j ] ; while ( ++ i < N && ++ j < M ) { if ( mat [ i ] [ j ] != res ) return false ; } return true ; } function isToepliz ( mat ) { for ( let i = 0 ; i < M ; i ++ ) { if ( ! checkDiagonal ( mat , 0 , i ) ) return false ; } for ( let i = 1 ; i < N ; i ++ ) { if ( ! checkDiagonal ( mat , i , 0 ) ) return false ; } return true ; } let mat = [ [ 6 , 7 , 8 , 9 ] , [ 4 , 6 , 7 , 8 ] , [ 1 , 4 , 6 , 7 ] , [ 0 , 1 , 4 , 6 ] , [ 2 , 0 , 1 , 4 ] ] ; if ( isToepliz ( mat ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count zeros in a row wise and column wise sorted matrix | JavaScript program to count number of 0 s in the given row - wise and column - wise sorted binary matrix ; Function to count number of 0 s in the given row - wise and column - wise sorted binary matrix . ; start from bottom - left corner of the matrix ; stores number of zeroes in the matrix ; move up until you find a 0 ; if zero is not found in current column , we are done ; add 0 s present in current column to result ; move right to next column ; Driver code
let N = 5 ; function countZeroes ( mat ) { let row = N - 1 , col = 0 ; let count = 0 ; while ( col < N ) { while ( mat [ row ] [ col ] > 0 ) if ( -- row < 0 ) return count ; count += ( row + 1 ) ; col ++ ; } return count ; } let mat = [ [ 0 , 0 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 1 , 1 ] , [ 0 , 1 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 , 1 ] ] ; document . write ( countZeroes ( mat ) ) ;
Return previous element in an expanding matrix | Javascript program to return previous element in an expanding matrix ; Returns left of str in an expanding matrix of a , b , c and d . ; Start from rightmost position ; If the current character is b or d , change to a or c respectively and break the loop ; If the current character is a or c , change it to b or d respectively ; driver program to test above method
String . prototype . replaceAt = function ( index , replacement ) { return this . substr ( 0 , index ) + replacement + this . substr ( index + replacement . length ) ; } function findLeft ( str ) { let n = str . length ; while ( n > 0 ) { n -- ; if ( str [ n ] == ' ' ) { str = str . replaceAt ( n , ' ' ) ; break ; } if ( str [ n ] == ' ' ) { str = str . replaceAt ( n , ' ' ) ; break ; } if ( str [ n ] == ' ' ) str = str . replaceAt ( n , ' ' ) ; else if ( str [ n ] == ' ' ) str = str . replaceAt ( n , ' ' ) ; } return str ; } let str = " " ; document . write ( " " + str + " " + findLeft ( str ) ) ;