Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y | Java implementation of the above approach ; 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
class GFG { static void FindMatrix ( int n , int m , int x , int y ) { int a , b , i , j ; if ( n * m == 1 ) { if ( x > y ) { System . out . print ( y + "NEW_LINE"); } else { System . out . print ( x + "NEW_LINE"); } return ; } a = Math . min ( x , y ) ; b = Math . min ( 2 * x , y ) - a ; boolean flag = true ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( flag ) System . out . print ( a + " ▁ " ) ; else System . out . print ( b + " ▁ " ) ; flag = ! flag ; } if ( ( ( n % 2 != 0 && m % 2 == 0 ) || ( n % 2 == 0 && m % 2 == 0 ) ) ) flag = ! flag ; System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int N , M , X , Y ; N = 3 ; M = 3 ; X = 5 ; Y = 3 ; FindMatrix ( N , M , X , Y ) ; } }