Generate a matrix with each row and column of given sum | Function to generate a matrix with sum of each row equal to sum of r [ ] and sum of each column equal to sum of c [ ] ; Initialize a matrix ; Traverse each cell ( i , j ) of the matrix ; Assign the minimum of the row or column value ; Subtract the minimum from both row and column sum ; Prlet the matrix obtained ; Driver Code
function restoreGem ( r , c ) { let dp = new Array ( r . length ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < r . length ; i ++ ) { for ( let j = 0 ; j < c . length ; j ++ ) { let m = Math . min ( r [ i ] , c [ j ] ) ; dp [ i ] [ j ] = m ; r [ i ] -= m ; c [ j ] -= m ; } } return dp ; } function prletMatrix ( ans , N , M ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { document . write ( ans [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let rowSum = [ 5 , 7 , 10 ] ; let colSum = [ 8 , 6 , 8 ] ; let ans = restoreGem ( rowSum , colSum ) ; prletMatrix ( ans , rowSum . length , colSum . length ) ;