Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y | Function to print the required matrix ; For 1 * 1 matrix ; Greater number ; Smaller number ; Sets / Resets for alternate filling of the matrix ; Print the matrix ; If end of row is reached ; Driver Code
function FindMatrix ( n , m , x , y ) { var a , b , i , j ; if ( n * m == 1 ) { if ( x > y ) { document . write ( y ) ; } else { document . write ( x ) ; } return ; } a = Math . min ( x , y ) ; b = Math . min ( 2 * x , y ) - a ; var flag = true ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( flag ) document . write ( a + " " ) ; else document . write ( b + " " ) ; flag = ! flag ; } if ( ( ( n % 2 != 0 && m % 2 == 0 ) || ( n % 2 == 0 && m % 2 == 0 ) ) ) flag = ! flag ; document . write ( " " ) ; } } var N , M , X , Y ; N = 3 ; M = 3 ; X = 5 ; Y = 3 ; FindMatrix ( N , M , X , Y ) ;