codeparrot/xlcost-text-to-code huggingface dataset:
Minimum operations required to make each row and column of matrix equals | Java Program to Find minimum number of operation required such that sum of elements on each row and column becomes same ; Function to find minimum operation required to make sum of each row and column equals ; Initialize the sumRow [ ] and sumCol [ ] array to 0 ; Calculate sumRow [ ] and sumCol [ ] array ; Find maximum sum value in either row or in column ; Find minimum increment required in either row or column ; Add difference in corresponding cell , sumRow [ ] and sumCol [ ] array ; Update the count variable ; If ith row satisfied , increment ith value for next iteration ; If jth column satisfied , increment jth value for next iteration ; Utility function to print matrix ; Driver program
import java . io . * ; class GFG { static int findMinOpeartion ( int matrix [ ] [ ] , int n ) { int [ ] sumRow = new int [ n ] ; int [ ] sumCol = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) for ( int j = 0 ; j < n ; ++ j ) { sumRow [ i ] += matrix [ i ] [ j ] ; sumCol [ j ] += matrix [ i ] [ j ] ; } int maxSum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { maxSum = Math . max ( maxSum , sumRow [ i ] ) ; maxSum = Math . max ( maxSum , sumCol [ i ] ) ; } int count = 0 ; for ( int i = 0 , j = 0 ; i < n && j < n ; ) { int diff = Math . min ( maxSum - sumRow [ i ] , maxSum - sumCol [ j ] ) ; matrix [ i ] [ j ] += diff ; sumRow [ i ] += diff ; sumCol [ j ] += diff ; count += diff ; if ( sumRow [ i ] == maxSum ) ++ i ; if ( sumCol [ j ] == maxSum ) ++ j ; } return count ; } static void printMatrix ( int matrix [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < n ; ++ j ) System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 1 , 2 } , { 3 , 4 } } ; System . out . println ( findMinOpeartion ( matrix , 2 ) ) ; printMatrix ( matrix , 2 ) ; } }
Count frequency of k in a matrix of size n where matrix ( i , j ) = i + j | Java program to find the frequency of k in matrix in matrix where m ( i , j ) = i + j ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static int find ( int n , int k ) { if ( n + 1 >= k ) return ( k - 1 ) ; else return ( 2 * n + 1 - k ) ; } public static void main ( String argc [ ] ) { int n = 4 , k = 7 ; int freq = find ( n , k ) ; if ( freq < 0 ) System . out . print ( " ▁ element " + " not exist NEW_LINE "); else System . out . print ( " ▁ Frequency " + " ▁ of ▁ " + k + " ▁ is ▁ " + freq + "NEW_LINE"); } }
Given 1 ' s , ▁ 2' s , 3 ' s ▁ . . . . . . k ' s print them in zig zag way . | Java program to print given number of 1 ' s , ▁ 2' s , 3 ' s ▁ . . . . k ' s in zig - zag way . ; function that prints given number of 1 ' s , ▁ ▁ 2' s , 3 ' s ▁ . . . . k ' s in zig - zag way . ; two - dimensional array to store numbers . ; for even row . ; for each column . ; storing element . ; decrement element at kth index . ; if array contains zero then increment index to make this next index ; for odd row . ; for each column . ; storing element . ; decrement element at kth index . ; if array contains zero then increment index to make this next index . ; printing the stored elements . ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static void ZigZag ( int rows , int columns , int numbers [ ] ) { int k = 0 ; int [ ] [ ] arr = new int [ rows ] [ columns ] ; for ( int i = 0 ; i < rows ; i ++ ) { if ( i % 2 == 0 ) { for ( int j = 0 ; j < columns && numbers [ k ] > 0 ; j ++ ) { arr [ i ] [ j ] = k + 1 ; numbers [ k ] -- ; if ( numbers [ k ] == 0 ) k ++ ; } } else { for ( int j = columns - 1 ; j >= 0 && numbers [ k ] > 0 ; j -- ) { arr [ i ] [ j ] = k + 1 ; numbers [ k ] -- ; if ( numbers [ k ] == 0 ) k ++ ; } } } for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j < columns ; j ++ ) System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String argc [ ] ) { int rows = 4 ; int columns = 5 ; int [ ] Numbers = new int [ ] { 3 , 4 , 2 , 2 , 3 , 1 , 5 } ; ZigZag ( rows , columns , Numbers ) ; } }
Number of cells a queen can move with obstacles on the chessborad | Java program to find number of cells a queen can move with obstacles on the chessborad ; Return the number of position a Queen can move . ; d11 , d12 , d21 , d22 are for diagnoal distances . r1 , r2 are for vertical distance . c1 , c2 are for horizontal distance . ; Initialise the distance to end of the board . ; For each obstacle find the minimum distance . If obstacle is present in any direction , distance will be updated . ; Driver code ; Chessboard size ; number of obstacles ; Queen x position ; Queen y position ; x position of obstacles ; y position of obstacles
import java . io . * ; class GFG { static int numberofPosition ( int n , int k , int x , int y , int obstPosx [ ] , int obstPosy [ ] ) { int d11 , d12 , d21 , d22 , r1 , r2 , c1 , c2 ; d11 = Math . min ( x - 1 , y - 1 ) ; d12 = Math . min ( n - x , n - y ) ; d21 = Math . min ( n - x , y - 1 ) ; d22 = Math . min ( x - 1 , n - y ) ; r1 = y - 1 ; r2 = n - y ; c1 = x - 1 ; c2 = n - x ; for ( int i = 0 ; i < k ; i ++ ) { if ( x > obstPosx [ i ] && y > obstPosy [ i ] && x - obstPosx [ i ] == y - obstPosy [ i ] ) d11 = Math . min ( d11 , x - obstPosx [ i ] - 1 ) ; if ( obstPosx [ i ] > x && obstPosy [ i ] > y && obstPosx [ i ] - x == obstPosy [ i ] - y ) d12 = Math . min ( d12 , obstPosx [ i ] - x - 1 ) ; if ( obstPosx [ i ] > x && y > obstPosy [ i ] && obstPosx [ i ] - x == y - obstPosy [ i ] ) d21 = Math . min ( d21 , obstPosx [ i ] - x - 1 ) ; if ( x > obstPosx [ i ] && obstPosy [ i ] > y && x - obstPosx [ i ] == obstPosy [ i ] - y ) d22 = Math . min ( d22 , x - obstPosx [ i ] - 1 ) ; if ( x == obstPosx [ i ] && obstPosy [ i ] < y ) r1 = Math . min ( r1 , y - obstPosy [ i ] - 1 ) ; if ( x == obstPosx [ i ] && obstPosy [ i ] > y ) r2 = Math . min ( r2 , obstPosy [ i ] - y - 1 ) ; if ( y == obstPosy [ i ] && obstPosx [ i ] < x ) c1 = Math . min ( c1 , x - obstPosx [ i ] - 1 ) ; if ( y == obstPosy [ i ] && obstPosx [ i ] > x ) c2 = Math . min ( c2 , obstPosx [ i ] - x - 1 ) ; } return d11 + d12 + d21 + d22 + r1 + r2 + c1 + c2 ; } public static void main ( String [ ] args ) { int n = 8 ; int k = 1 ; int Qposx = 4 ; int Qposy = 4 ; int obstPosx [ ] = { 3 } ; int obstPosy [ ] = { 5 } ; System . out . println ( numberofPosition ( n , k , Qposx , Qposy , obstPosx , obstPosy ) ) ; } }
Maximum product of 4 adjacent elements in matrix | Java program to find out the maximum product in the matrix which four elements are adjacent to each other in one direction ; function to find max product ; iterate the rows . ; iterate the columns . ; check the maximum product in horizontal row . ; check the maximum product in vertical row . ; check the maximum product in diagonal ( going through down - right ) ; check the maximum product in diagonal ( going through up - right ) ; Driver code
class GFG { static final int n = 5 ; static int FindMaxProduct ( int arr [ ] [ ] , int n ) { int max = 0 , result ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( j - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i ] [ j - 1 ] * arr [ i ] [ j - 2 ] * arr [ i ] [ j - 3 ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j ] * arr [ i - 2 ] [ j ] * arr [ i - 3 ] [ j ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 && ( j - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j - 1 ] * arr [ i - 2 ] [ j - 2 ] * arr [ i - 3 ] [ j - 3 ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 && ( j - 1 ) <= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j + 1 ] * arr [ i - 2 ] [ j + 2 ] * arr [ i - 3 ] [ j + 3 ] ; if ( max < result ) max = result ; } } } return max ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 1 } , { 2 , 3 , 4 , 5 , 6 } , { 7 , 8 , 9 , 1 , 0 } , { 9 , 6 , 4 , 2 , 3 } } ; System . out . print ( FindMaxProduct ( arr , n ) ) ; } }
Maximum product of 4 adjacent elements in matrix | Java implementation of the above approach ; Window Product for each row . ; Maximum window product for each row ; Global maximum window product ; Driver Code ; Function call
import java . io . * ; class GFG { public static int maxPro ( int [ ] [ ] a , int n , int m , int k ) { int maxi = 1 , mp = 1 ; for ( int i = 0 ; i < n ; ++ i ) { int wp = 1 ; for ( int l = 0 ; l < k ; ++ l ) { wp *= a [ i ] [ l ] ; } mp = wp ; for ( int j = k ; j < m ; ++ j ) { wp = wp * a [ i ] [ j ] / a [ i ] [ j - k ] ; maxi = Math . max ( maxi , Math . max ( mp , wp ) ) ; } } return maxi ; } public static void main ( String [ ] args ) { int n = 6 , m = 5 , k = 4 ; int [ ] [ ] a = new int [ ] [ ] { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 1 } , { 2 , 3 , 4 , 5 , 6 } , { 7 , 8 , 9 , 1 , 0 } , { 9 , 6 , 4 , 2 , 3 } , { 1 , 1 , 2 , 1 , 1 } } ; int maxpro = maxPro ( a , n , m , k ) ; System . out . println ( maxpro ) ; } }
Construct a Binary Tree from Postorder and Inorder | Java program to contree using inorder and postorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Recursive function to conbinary of size n from Inorder traversal in [ ] and Postorder traversal post [ ] . Initial values of inStrt and inEnd should be 0 and n - 1. The function doesn 't do any error checking for cases where inorder and postorder do not form a tree  ; Base case ; Pick current node from Postorder traversal using postIndex and decrement postIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , con left and right subtress ; This function mainly creates an unordered_map , then calls buildTreeUtil ( ) ; Store indexes of all items so that we we can quickly find later ; Index in postorder ; Helper function that allocates a new node ; This funtcion is here just to test ; Driver code
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node buildUtil ( int in [ ] , int post [ ] , int inStrt , int inEnd ) { if ( inStrt > inEnd ) return null ; int curr = post [ index ] ; Node node = newNode ( curr ) ; ( index ) -- ; if ( inStrt == inEnd ) return node ; int iIndex = mp . get ( curr ) ; node . right = buildUtil ( in , post , iIndex + 1 , inEnd ) ; node . left = buildUtil ( in , post , inStrt , iIndex - 1 ) ; return node ; } static HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; static int index ; static Node buildTree ( int in [ ] , int post [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) mp . put ( in [ i ] , i ) ; index = len - 1 ; return buildUtil ( in , post , 0 , len - 1 ) ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void preOrder ( Node node ) { if ( node == null ) return ; System . out . printf ( " % d ▁ " , node . data ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void main ( String [ ] args ) { int in [ ] = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int post [ ] = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = in . length ; Node root = buildTree ( in , post , n ) ; System . out . print ( "Preorder of the constructed tree : NEW_LINE"); preOrder ( root ) ; } }
Minimum flip required to make Binary Matrix symmetric | Java Program to find minimum flip required to make Binary Matrix symmetric along main diagonal ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; finding the transpose of the matrix ; Finding the number of position where element are not same . ; Driver program to test above function
import java . util . * ; class GFG { static int minimumflip ( int mat [ ] [ ] , int n ) { int transpose [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) transpose [ i ] [ j ] = mat [ j ] [ i ] ; int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( transpose [ i ] [ j ] != mat [ i ] [ j ] ) flip ++ ; return flip / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; int mat [ ] [ ] = { { 0 , 0 , 1 } , { 1 , 1 , 1 } , { 1 , 0 , 0 } } ; System . out . println ( minimumflip ( mat , n ) ) ; } }
Minimum flip required to make Binary Matrix symmetric | Java Program to find minimum flip required to make Binary Matrix symmetric along main diagonal ; Return the minimum flip required to make Binary Matrix symmetric along main diagonal . ; Comparing elements across diagonal ; Driver program to test above function
import java . util . * ; class GFG { static int minimumflip ( int mat [ ] [ ] , int n ) { int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) flip ++ ; return flip ; } public static void main ( String [ ] args ) { int n = 3 ; int mat [ ] [ ] = { { 0 , 0 , 1 } , { 1 , 1 , 1 } , { 1 , 0 , 0 } } ; System . out . println ( minimumflip ( mat , n ) ) ; } }
Frequencies of even and odd numbers in a matrix | Java Program to Find the frequency of even and odd numbers in a matrix ; function for calculating frequency ; modulo by 2 to check even and odd ; print Frequency of numbers ; Driver code
class GFG { static final int MAX = 100 ; static void freq ( int ar [ ] [ ] , int m , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < m ; ++ i ) { for ( int j = 0 ; j < n ; ++ j ) { if ( ( ar [ i ] [ j ] % 2 ) == 0 ) ++ even ; else ++ odd ; } } System . out . print ( " ▁ Frequency ▁ of ▁ odd ▁ number ▁ = " + odd + " NEW_LINE"); System . out . print ( " ▁ Frequency ▁ of ▁ even ▁ number ▁ = ▁ " + even + " NEW_LINE"); } public static void main ( String [ ] args ) { int m = 3 , n = 3 ; int array [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; freq ( array , m , n ) ; } }
Center element of matrix equals sums of half diagonals | Java program to find maximum elements that can be made equal with k updates ; Function to Check center element is equal to the individual sum of all the half diagonals ; Find sums of half diagonals ; Driver code
import java . util . Arrays ; public class GFG { static int MAX = 100 ; static boolean HalfDiagonalSums ( int mat [ ] [ ] , int n ) { int diag1_left = 0 , diag1_right = 0 ; int diag2_left = 0 , diag2_right = 0 ; for ( int i = 0 , j = n - 1 ; i < n ; i ++ , j -- ) { if ( i < n / 2 ) { diag1_left += mat [ i ] [ i ] ; diag2_left += mat [ j ] [ i ] ; } else if ( i > n / 2 ) { diag1_right += mat [ i ] [ i ] ; diag2_right += mat [ j ] [ i ] ; } } return ( diag1_left == diag2_right && diag2_right == diag2_left && diag1_right == diag2_left && diag2_right == mat [ n / 2 ] [ n / 2 ] ) ; } public static void main ( String args [ ] ) { int a [ ] [ ] = { { 2 , 9 , 1 , 4 , - 2 } , { 6 , 7 , 2 , 11 , 4 } , { 4 , 2 , 9 , 2 , 4 } , { 1 , 9 , 2 , 4 , 4 } , { 0 , 2 , 4 , 2 , 5 } } ; System . out . print ( HalfDiagonalSums ( a , 5 ) ? " Yes " : " No " ) ; } }
Program for Identity Matrix | Java program to print Identity Matrix ; Checking if row is equal to column ; Driver Code
class GFG { static int identity ( int num ) { int row , col ; for ( row = 0 ; row < num ; row ++ ) { for ( col = 0 ; col < num ; col ++ ) { if ( row == col ) System . out . print ( 1 + " ▁ " ) ; else System . out . print ( 0 + " ▁ " ) ; } System . out . println ( ) ; } return 0 ; } public static void main ( String args [ ] ) { int size = 5 ; identity ( size ) ; } }
Program for Identity Matrix | Java program to check if a given matrix is identity ; Driver Code
class GFG { int MAX = 100 ; static boolean isIdentity ( int mat [ ] [ ] , int N ) { for ( int row = 0 ; row < N ; row ++ ) { for ( int col = 0 ; col < N ; col ++ ) { if ( row == col && mat [ row ] [ col ] != 1 ) return false ; else if ( row != col && mat [ row ] [ col ] != 0 ) return false ; } } return true ; } public static void main ( String args [ ] ) { int N = 4 ; int mat [ ] [ ] = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 1 } } ; if ( isIdentity ( mat , N ) ) System . out . println ( " Yes ▁ " ) ; else System . out . println ( " No ▁ " ) ; } }
Ways of filling matrix such that product of all rows and all columns are equal to unity | Java program to find number of ways to fill a matrix under given constraints ; Returns a raised power t under modulo mod ; Counting number of ways of filling the matrix ; Function calculating the answer ; if sum of numbers of rows and columns is odd i . e ( n + m ) % 2 == 1 and k = - 1 , then there are 0 ways of filiing the matrix . ; If there is one row or one column then there is only one way of filling the matrix ; If the above cases are not followed then we find ways to fill the n - 1 rows and m - 1 columns which is 2 ^ ( ( m - 1 ) * ( n - 1 ) ) . ; Driver function for the program
import java . io . * ; class Example { final static long mod = 100000007 ; static long modPower ( long a , long t , long mod ) { long now = a , ret = 1 ; while ( t > 0 ) { if ( t % 2 == 1 ) ret = now * ( ret % mod ) ; now = now * ( now % mod ) ; t >>= 1 ; } return ret ; } static long countWays ( int n , int m , int k ) { if ( n == 1 m == 1 ) return 1 ; else if ( ( n + m ) % 2 == 1 && k == - 1 ) return 0 ; return ( modPower ( modPower ( ( long ) 2 , n - 1 , mod ) , m - 1 , mod ) % mod ) ; } public static void main ( String args [ ] ) throws IOException { int n = 2 , m = 7 , k = 1 ; System . out . println ( countWays ( n , m , k ) ) ; } }
Construct a Binary Tree from Postorder and Inorder | Java program for above approach ; Node class ; Constructor ; Tree building function ; Create Stack of type Node * ; Create HashSet of type Node * ; Initialise postIndex with n - 1 ; Initialise root with null ; Initialise node with NULL ; Run do - while loop ; Initialise node with new Node ( post [ p ] ) ; ; Check is root is equal to NULL ; If size of set is greater than 0 ; If st . peek ( ) is present in the set s ; If the stack is not empty and st . top ( ) . data is equal to in [ i ] ; Pop elements from stack ; If node not equal to NULL ; Return root ; For print preOrder Traversal ; Driver Code ; Function Call ; Function Call for preOrder
import java . io . * ; import java . util . * ; class GFG { static class Node { int data ; Node left , right ; Node ( int x ) { data = x ; left = right = null ; } } static Node buildTree ( int in [ ] , int post [ ] , int n ) { Stack < Node > st = new Stack < > ( ) ; HashSet < Node > s = new HashSet < > ( ) ; int postIndex = n - 1 ; Node root = null ; for ( int p = n - 1 , i = n - 1 ; p >= 0 { Node node = null ; do { node = new Node ( post [ p ] ) ; if ( root == null ) { root = node ; } if ( st . size ( ) > 0 ) { if ( s . contains ( st . peek ( ) ) ) { s . remove ( st . peek ( ) ) ; st . peek ( ) . left = node ; st . pop ( ) ; } else { st . peek ( ) . right = node ; } } st . push ( node ) ; } while ( post [ p -- ] != in [ i ] && p >= 0 ) ; node = null ; while ( st . size ( ) > 0 && i >= 0 && st . peek ( ) . data == in [ i ] ) { node = st . peek ( ) ; st . pop ( ) ; i -- ; } if ( node != null ) { s . add ( node ) ; st . push ( node ) ; } } return root ; } static void preOrder ( Node node ) { if ( node == null ) return ; System . out . printf ( " % d ▁ " , node . data ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void main ( String [ ] args ) { int in [ ] = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int post [ ] = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = in . length ; Node root = buildTree ( in , post , n ) ; System . out . print ( "Preorder of the constructed tree : NEW_LINE"); preOrder ( root ) ; } }
Mirror of matrix across diagonal | Simple Java program to find mirror of matrix across diagonal . ; for diagonal which start from at first row of matrix ; traverse all top right diagonal ; here we use stack for reversing the element of diagonal ; push all element back to matrix in reverse order ; do the same process for all the diagonal which start from last column ; here we use stack for reversing the elements of diagonal ; push all element back to matrix in reverse order ; Utility function to print a matrix ; Driver program to test above function
import java . util . * ; class GFG { static int MAX = 100 ; static void imageSwap ( int mat [ ] [ ] , int n ) { int row = 0 ; for ( int j = 0 ; j < n ; j ++ ) { Stack < Integer > s = new Stack < > ( ) ; int i = row , k = j ; while ( i < n && k >= 0 ) { s . push ( mat [ i ++ ] [ k -- ] ) ; } i = row ; k = j ; while ( i < n && k >= 0 ) { mat [ i ++ ] [ k -- ] = s . peek ( ) ; s . pop ( ) ; } } int column = n - 1 ; for ( int j = 1 ; j < n ; j ++ ) { Stack < Integer > s = new Stack < > ( ) ; int i = j , k = column ; while ( i < n && k >= 0 ) { s . push ( mat [ i ++ ] [ k -- ] ) ; } i = j ; k = column ; while ( i < n && k >= 0 ) { mat [ i ++ ] [ k -- ] = s . peek ( ) ; s . pop ( ) ; } } } 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 mat [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 ; imageSwap ( mat , n ) ; printMatrix ( mat , n ) ; } }
Mirror of matrix across diagonal | Efficient Java program to find mirror of matrix across diagonal . ; traverse a matrix and swap mat [ i ] [ j ] with mat [ j ] [ i ] ; Utility function to print a matrix ; driver program to test above function
import java . io . * ; class GFG { static int MAX = 100 ; static void imageSwap ( int mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j <= i ; j ++ ) mat [ i ] [ j ] = mat [ i ] [ j ] + mat [ j ] [ i ] - ( mat [ j ] [ i ] = mat [ i ] [ j ] ) ; } 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 mat [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 ; imageSwap ( mat , n ) ; printMatrix ( mat , n ) ; } }
Search in a row wise and column wise sorted matrix | Java program to search an element in row - wise and column - wise sorted matrix ; Searches the element x in mat [ ] [ ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; traverse through the matrix ; if the element is found ; Driver code
class GFG { static int search ( int [ ] [ ] mat , int n , int x ) { if ( n == 0 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) if ( mat [ i ] [ j ] == x ) { System . out . print ( " Element ▁ found ▁ at ▁ ( " + i + " , ▁ " + j + ")NEW_LINE"); return 1 ; } } System . out . print ( " ▁ Element ▁ not ▁ found " ) ; return 0 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; search ( mat , 4 , 29 ) ; } }
Search in a row wise and column wise sorted matrix | JAVA Code for Search in a row wise and column wise sorted matrix ; Searches the element x in mat [ ] [ ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for top right element ; if mat [ i ] [ j ] < x ; if ( i == n j == - 1 ) ; driver program to test above function
class GFG { private static void search ( int [ ] [ ] mat , int n , int x ) { int i = 0 , j = n - 1 ; while ( i < n && j >= 0 ) { if ( mat [ i ] [ j ] == x ) { System . out . print ( " n ▁ Found ▁ at ▁ " + i + " ▁ " + j ) ; return ; } if ( mat [ i ] [ j ] > x ) j -- ; else i ++ ; } System . out . print ( " n ▁ Element ▁ not ▁ found " ) ; return ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; search ( mat , 4 , 29 ) ; } }