Maximize the sum of Kth column of a Matrix | Java program to implement the above approach ; Function to maximize the Kth column sum ; Store all the elements of the resultant matrix of size N * N ; Store value of each elements of the matrix ; Fill all the columns < K ; Fill all the columns >= K ; Function to print the matrix ; Driver Code
class GFG { static int [ ] [ ] findMatrix ( int N , int K ) { int [ ] [ ] mat = new int [ N ] [ N ] ; int element = 1 ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < K - 1 ; ++ j ) { mat [ i ] [ j ] = element ++ ; } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = K - 1 ; j < N ; ++ j ) { mat [ i ] [ j ] = element ++ ; } } return mat ; } static void printMatrix ( int [ ] [ ] mat , int N ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 3 , K = 2 ; int [ ] [ ] mat = findMatrix ( N , K ) ; printMatrix ( mat , N ) ; } }