codeparrot/xlcost-text-to-code huggingface dataset:
Special two digit numbers in a Binary Search Tree | A binary tree node ; Function to create a new node ; If the tree is empty , return a new , single node ; Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Function to find if number is special or not ; Check if number is two digit or not ; Function to count number of special two digit number ; Driver code ; Function call
class Node { int info ; Node left , right ; Node ( int d ) { info = d ; left = right = null ; } } class BinaryTree { static Node head ; static int count ; Node insert ( Node node , int info ) { if ( node == null ) { return ( new Node ( info ) ) ; } else { if ( info <= node . info ) { node . left = insert ( node . left , info ) ; } else { node . right = insert ( node . right , info ) ; } return node ; } } static int check ( int num ) { int sum = 0 , i = num , sum_of_digits , prod_of_digits ; if ( num < 10 num > 99 ) return 0 ; else { sum_of_digits = ( i % 10 ) + ( i / 10 ) ; prod_of_digits = ( i % 10 ) * ( i / 10 ) ; sum = sum_of_digits + prod_of_digits ; } if ( sum == num ) return 1 ; else return 0 ; } static void countSpecialDigit ( Node rt ) { int x ; if ( rt == null ) return ; else { x = check ( rt . info ) ; if ( x == 1 ) count = count + 1 ; countSpecialDigit ( rt . left ) ; countSpecialDigit ( rt . right ) ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; Node root = null ; root = tree . insert ( root , 50 ) ; tree . insert ( root , 29 ) ; tree . insert ( root , 59 ) ; tree . insert ( root , 19 ) ; tree . insert ( root , 53 ) ; tree . insert ( root , 556 ) ; tree . insert ( root , 56 ) ; tree . insert ( root , 94 ) ; tree . insert ( root , 13 ) ; countSpecialDigit ( root ) ; System . out . println ( count ) ; } }
Construct Special Binary Tree from given Inorder traversal | A binary tree node has data , pointer to left child and a pointer to right child ; Recursive function to construct binary of size len from Inorder traversal inorder [ ] . Initial values of start and end should be 0 and len - 1. ; Find index of the maximum element from Binary Tree ; Pick the maximum value and make it root ; If this is the only element in inorder [ start . . end ] , then return it ; Using index in Inorder traversal , construct left and right subtress ; Function to find index of the maximum value in arr [ start ... end ] ; This funtcion is here just to test buildTree ( ) ; first recur on left child ; then print the data of node ; now recur on right child ; Driver code ; Assume that inorder traversal of following tree is given 40 / \ 10 30 / \ 5 28 ; Let us test the built tree by printing Inorder traversal
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; Node buildTree ( int inorder [ ] , int start , int end , Node node ) { if ( start > end ) return null ; int i = max ( inorder , start , end ) ; node = new Node ( inorder [ i ] ) ; if ( start == end ) return node ; node . left = buildTree ( inorder , start , i - 1 , node . left ) ; node . right = buildTree ( inorder , i + 1 , end , node . right ) ; return node ; } int max ( int arr [ ] , int strt , int end ) { int i , max = arr [ strt ] , maxind = strt ; for ( i = strt + 1 ; i <= end ; i ++ ) { if ( arr [ i ] > max ) { max = arr [ i ] ; maxind = i ; } } return maxind ; } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; int inorder [ ] = new int [ ] { 5 , 10 , 40 , 30 , 28 } ; int len = inorder . length ; Node mynode = tree . buildTree ( inorder , 0 , len - 1 , tree . root ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree ▁ is ▁ " ) ; tree . printInorder ( mynode ) ; } }
Continuous Tree | Java program to check if a tree is continuous or not ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Function to check tree is continuous or not ; if next node is empty then return true ; if current node is leaf node then return true because it is end of root to leaf path ; If left subtree is empty , then only check right ; If right subtree is empty , then only check left ; If both left and right subtrees are not empty , check everything ; Driver program to test mirror ( )
import java . util . * ; class solution { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static boolean treeContinuous ( Node ptr ) { if ( ptr == null ) return true ; if ( ptr . left == null && ptr . right == null ) return true ; if ( ptr . left == null ) return ( Math . abs ( ptr . data - ptr . right . data ) == 1 ) && treeContinuous ( ptr . right ) ; if ( ptr . right == null ) return ( Math . abs ( ptr . data - ptr . left . data ) == 1 ) && treeContinuous ( ptr . left ) ; return Math . abs ( ptr . data - ptr . left . data ) == 1 && Math . abs ( ptr . data - ptr . right . data ) == 1 && treeContinuous ( ptr . left ) && treeContinuous ( ptr . right ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 4 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 3 ) ; root . right . right = newNode ( 5 ) ; if ( treeContinuous ( root ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Sum of middle row and column in Matrix | java program to find sum of middle row and column in matrix ; loop for sum of row ; loop for sum of column ; Driver function
import java . io . * ; class GFG { static int MAX = 100 ; static void middlesum ( int mat [ ] [ ] , int n ) { int row_sum = 0 , col_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) row_sum += mat [ n / 2 ] [ i ] ; System . out . println ( " Sum ▁ of ▁ middle ▁ row ▁ = ▁ " + row_sum ) ; for ( int i = 0 ; i < n ; i ++ ) col_sum += mat [ i ] [ n / 2 ] ; System . out . println ( " Sum ▁ of ▁ middle ▁ column ▁ = ▁ " + col_sum ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 2 , 5 , 7 } , { 3 , 7 , 2 } , { 5 , 6 , 9 } } ; middlesum ( mat , 3 ) ; } }
Row | Java program showing time difference in row major and column major access ; taking MAX 10000 so that time difference can be shown ; accessing element row wise ; accessing element column wise ; Driver code ; Time taken by row major order ; Time taken by column major order
import java . time . Duration ; import java . time . Instant ; import java . util . * ; class GFG { static int MAX = 10000 ; static int [ ] [ ] arr = new int [ MAX ] [ MAX ] ; static void rowMajor ( ) { int i , j ; for ( i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) { arr [ i ] [ j ] ++ ; } } } static void colMajor ( ) { int i , j ; for ( i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) { arr [ j ] [ i ] ++ ; } } } public static void main ( String [ ] args ) { Instant start = Instant . now ( ) ; rowMajor ( ) ; Instant end = Instant . now ( ) ; System . out . println ( " Row ▁ major ▁ access ▁ time ▁ : ▁ " + Duration . between ( start , end ) ) ; start = Instant . now ( ) ; colMajor ( ) ; end = Instant . now ( ) ; System . out . printf ( " Column ▁ major ▁ access ▁ time ▁ : ▁ " + Duration . between ( start , end ) ) ; } }
Rotate the matrix right by K times | Java program to rotate a matrix right by k times ; size of matrix ; function to rotate matrix by k times ; temporary array of size M ; within the size of matrix ; copy first M - k elements to temporary array ; copy the elements from k to end to starting ; copy elements from temporary array to end ; function to display the matrix ; Driver code ; rotate matrix by k ; display rotated matrix
class GFG { static final int M = 3 ; static final int N = 3 ; static void rotateMatrix ( int matrix [ ] [ ] , int k ) { int temp [ ] = new int [ M ] ; k = k % M ; for ( int i = 0 ; i < N ; i ++ ) { for ( int t = 0 ; t < M - k ; t ++ ) temp [ t ] = matrix [ i ] [ t ] ; for ( int j = M - k ; j < M ; j ++ ) matrix [ i ] [ j - M + k ] = matrix [ i ] [ j ] ; for ( int j = k ; j < M ; j ++ ) matrix [ i ] [ j ] = temp [ j - k ] ; } } static void displayMatrix ( int matrix [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 12 , 23 , 34 } , { 45 , 56 , 67 } , { 78 , 89 , 91 } } ; int k = 2 ; rotateMatrix ( matrix , k ) ; displayMatrix ( matrix ) ; } }
Program to check Involutory Matrix | Java Program to implement involutory matrix . ; Function for matrix multiplication . ; Function to check involutory matrix . ; multiply function call . ; Driver function . ; Function call . If function return true then if part will execute otherwise else part will execute .
import java . io . * ; class GFG { static int N = 3 ; static void multiply ( int mat [ ] [ ] , int res [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { res [ i ] [ j ] = 0 ; for ( int k = 0 ; k < N ; k ++ ) res [ i ] [ j ] += mat [ i ] [ k ] * mat [ k ] [ j ] ; } } } static boolean InvolutoryMatrix ( int mat [ ] [ ] ) { int res [ ] [ ] = new int [ N ] [ N ] ; multiply ( mat , res ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j && res [ i ] [ j ] != 1 ) return false ; if ( i != j && res [ i ] [ j ] != 0 ) return false ; } } return true ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 0 } , { 0 , - 1 , 0 } , { 0 , 0 , - 1 } } ; if ( InvolutoryMatrix ( mat ) ) System . out . println ( " Involutory ▁ Matrix " ) ; else System . out . println ( " Not ▁ Involutory ▁ Matrix " ) ; } }
Interchange elements of first and last rows in matrix | Java code to swap the element of first and last row and display the result ; swapping of element between first and last rows ; Driver code ; input in the array ; printing the interchanged matrix
import java . io . * ; public class Interchange { static void interchangeFirstLast ( int m [ ] [ ] ) { int rows = m . length ; for ( int i = 0 ; i < m [ 0 ] . length ; i ++ ) { int t = m [ 0 ] [ i ] ; m [ 0 ] [ i ] = m [ rows - 1 ] [ i ] ; m [ rows - 1 ] [ i ] = t ; } } public static void main ( String args [ ] ) throws IOException { int m [ ] [ ] = { { 8 , 9 , 7 , 6 } , { 4 , 7 , 6 , 5 } , { 3 , 2 , 1 , 8 } , { 9 , 9 , 7 , 7 } } ; interchangeFirstLast ( m ) ; for ( int i = 0 ; i < m . length ; i ++ ) { for ( int j = 0 ; j < m [ 0 ] . length ; j ++ ) System . out . print ( m [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } }
Construct Binary Tree from given Parent Array representation | A binary tree node ; Creates a node with key as ' i ' . If i is root , then it changes root . If parent of i is not created , then it creates parent first ; If this node is already created ; Create a new node and set created [ i ] ; If ' i ' is root , change root pointer and return ; If parent is not created , then create parent first ; Find parent pointer ; If this is first child of parent ; If second child ; Creates tree from parent [ 0. . n - 1 ] and returns root of the created tree ; Create an array created [ ] to keep track of created nodes , initialize all entries as NULL ; For adding new line in a program ; Utility function to do inorder traversal ; Driver method
class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class BinaryTree { Node root ; void createNode ( int parent [ ] , int i , Node created [ ] ) { if ( created [ i ] != null ) return ; created [ i ] = new Node ( i ) ; if ( parent [ i ] == - 1 ) { root = created [ i ] ; return ; } if ( created [ parent [ i ] ] == null ) createNode ( parent , parent [ i ] , created ) ; Node p = created [ parent [ i ] ] ; if ( p . left == null ) p . left = created [ i ] ; else p . right = created [ i ] ; } Node createTree ( int parent [ ] , int n ) { Node [ ] created = new Node [ n ] ; for ( int i = 0 ; i < n ; i ++ ) created [ i ] = null ; for ( int i = 0 ; i < n ; i ++ ) createNode ( parent , i , created ) ; return root ; } void newLine ( ) { System . out . println ( " " ) ; } void inorder ( Node node ) { if ( node != null ) { inorder ( node . left ) ; System . out . print ( node . key + " ▁ " ) ; inorder ( node . right ) ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; int parent [ ] = new int [ ] { - 1 , 0 , 0 , 1 , 1 , 3 , 5 } ; int n = parent . length ; Node node = tree . createTree ( parent , n ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ constructed ▁ tree ▁ " ) ; tree . inorder ( node ) ; tree . newLine ( ) ; } }
Row wise sorting in 2D array | Java code to sort 2D matrix row - wise ; loop for rows of matrix ; loop for column of matrix ; loop for comparison and swapping ; swapping of elements ; printing the sorted matrix ; driver code
import java . io . * ; public class Sort2DMatrix { static int sortRowWise ( int m [ ] [ ] ) { for ( int i = 0 ; i < m . length ; i ++ ) { for ( int j = 0 ; j < m [ i ] . length ; j ++ ) { for ( int k = 0 ; k < m [ i ] . length - j - 1 ; k ++ ) { if ( m [ i ] [ k ] > m [ i ] [ k + 1 ] ) { int t = m [ i ] [ k ] ; m [ i ] [ k ] = m [ i ] [ k + 1 ] ; m [ i ] [ k + 1 ] = t ; } } } } for ( int i = 0 ; i < m . length ; i ++ ) { for ( int j = 0 ; j < m [ i ] . length ; j ++ ) System . out . print ( m [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } return 0 ; } public static void main ( String args [ ] ) { int m [ ] [ ] = { { 9 , 8 , 7 , 1 } , { 7 , 3 , 0 , 2 } , { 9 , 5 , 3 , 2 } , { 6 , 3 , 1 , 2 } } ; sortRowWise ( m ) ; } }
Row wise sorting in 2D array | Java code to sort 2D matrix row - wise ; One by one sort individual rows . ; printing the sorted matrix ; driver code
import java . io . * ; import java . util . Arrays ; public class Sort2DMatrix { static int sortRowWise ( int m [ ] [ ] ) { for ( int i = 0 ; i < m . length ; i ++ ) Arrays . sort ( m [ i ] ) ; for ( int i = 0 ; i < m . length ; i ++ ) { for ( int j = 0 ; j < m [ i ] . length ; j ++ ) System . out . print ( m [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } return 0 ; } public static void main ( String args [ ] ) { int m [ ] [ ] = { { 9 , 8 , 7 , 1 } , { 7 , 3 , 0 , 2 } , { 9 , 5 , 3 , 2 } , { 6 , 3 , 1 , 2 } } ; sortRowWise ( m ) ; } }
Program for Markov matrix | Java code to check Markov Matrix ; outer loop to access rows and inner to access columns ; Find sum of current row ; Driver Code ; Matrix to check ; calls the function check ( )
import java . io . * ; public class markov { static boolean checkMarkov ( double m [ ] [ ] ) { for ( int i = 0 ; i < m . length ; i ++ ) { double sum = 0 ; for ( int j = 0 ; j < m [ i ] . length ; j ++ ) sum = sum + m [ i ] [ j ] ; if ( sum != 1 ) return false ; } return true ; } public static void main ( String args [ ] ) { double m [ ] [ ] = { { 0 , 0 , 1 } , { 0.5 , 0 , 0.5 } , { 1 , 0 , 0 } } ; if ( checkMarkov ( m ) ) System . out . println ( " ▁ yes ▁ " ) ; else System . out . println ( " ▁ no ▁ " ) ; } }
Program to check diagonal matrix and scalar matrix | Program to check matrix is diagonal matrix or not . ; Function to check matrix is diagonal matrix or not . ; condition to check other elements except main diagonal are zero or not . ; Driver function
import java . io . * ; class GFG { static int N = 4 ; static boolean isDiagonalMatrix ( int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; return true ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 4 , 0 , 0 , 0 } , { 0 , 7 , 0 , 0 } , { 0 , 0 , 5 , 0 } , { 0 , 0 , 0 , 1 } } ; if ( isDiagonalMatrix ( mat ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Program to check diagonal matrix and scalar matrix | Program to check matrix is scalar matrix or not . ; Function to check matrix is scalar matrix or not . ; Check all elements except main diagonal are zero or not . ; Check all diagonal elements are same or not . ; Driver function ; Function call
import java . io . * ; class GFG { static int N = 4 ; static boolean isScalarMatrix ( int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; for ( int i = 0 ; i < N - 1 ; i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ; return true ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 2 , 0 , 0 , 0 } , { 0 , 2 , 0 , 0 } , { 0 , 0 , 2 , 0 } , { 0 , 0 , 0 , 2 } } ; if ( isScalarMatrix ( mat ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Construct a Binary Tree from Postorder and Inorder | A binary tree node has data , pointer to left child and a pointer to right child ; Recursive function to construct binary of size n from Inorder traversal in [ ] and Postrder 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 Postrder 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 , construct left and right subtress ; Function to find index of value in arr [ start ... end ] The function assumes that value is postsent in in [ ] ; This funtcion is here just to test ; Driver Code
class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node buildUtil ( int in [ ] , int post [ ] , int inStrt , int inEnd , int postStrt , int postEnd ) { if ( inStrt > inEnd ) return null ; Node node = new Node ( post [ postEnd ] ) ; if ( inStrt == inEnd ) return node ; int iIndex = search ( in , inStrt , inEnd , node . data ) ; node . left = buildUtil ( in , post , inStrt , iIndex - 1 , postStrt , postStrt - inStrt + iIndex - 1 ) ; node . right = buildUtil ( in , post , iIndex + 1 , inEnd , postEnd - inEnd + iIndex , postEnd - 1 ) ; return node ; } int search ( int arr [ ] , int strt , int end , int value ) { int i ; for ( i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) break ; } return i ; } void preOrder ( Node node ) { if ( node == null ) return ; System . out . print ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; int in [ ] = new int [ ] { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int post [ ] = new int [ ] { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = in . length ; Node root = tree . buildUtil ( in , post , 0 , n - 1 , 0 , n - 1 ) ; System . out . println ( " Preorder ▁ of ▁ the ▁ constructed ▁ tree ▁ : ▁ " ) ; tree . preOrder ( root ) ; } }
Check given matrix is magic square or not | JAVA program to check whether a given matrix is magic matrix or not ; Returns true if mat [ ] [ ] is magic square , else returns false . ; calculate the sum of the prime diagonal ; the secondary diagonal ; For sums of Rows ; check if every row sum is equal to prime diagonal sum ; For sums of Columns ; check if every column sum is equal to prime diagonal sum ; driver program to test above function
import java . io . * ; class GFG { static int N = 3 ; static boolean isMagicSquare ( int mat [ ] [ ] ) { int sum = 0 , sum2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum = sum + mat [ i ] [ i ] ; for ( int i = 0 ; i < N ; i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ; if ( sum != sum2 ) return false ; for ( int i = 0 ; i < N ; i ++ ) { int rowSum = 0 ; for ( int j = 0 ; j < N ; j ++ ) rowSum += mat [ i ] [ j ] ; if ( rowSum != sum ) return false ; } for ( int i = 0 ; i < N ; i ++ ) { int colSum = 0 ; for ( int j = 0 ; j < N ; j ++ ) colSum += mat [ j ] [ i ] ; if ( sum != colSum ) return false ; } return true ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 2 , 7 , 6 } , { 9 , 5 , 1 } , { 4 , 3 , 8 } } ; if ( isMagicSquare ( mat ) ) System . out . println ( " Magic ▁ Square " ) ; else System . out . println ( " Not ▁ a ▁ magic " + " ▁ Square " ) ; } }
Count sub | Java implementation to count sub - matrices having sum divisible by the value ' k ' ; function to count all sub - arrays divisible by k ; create auxiliary hash array to count frequency of remainders ; Traverse original array and compute cumulative sum take remainder of this current cumulative sum and increase count by 1 for this remainder in mod [ ] array ; as the sum can be negative , taking modulo twice ; Initialize result ; Traverse mod [ ] ; If there are more than one prefix subarrays with a particular mod value . ; add the subarrays starting from the arr [ i ] which are divisible by k itself ; function to count all sub - matrices having sum divisible by the value ' k ' ; Variable to store the final output ; Set the left column ; Initialize all elements of temp as 0 ; Set the right column for the left column set by outer loop ; Calculate sum between current left and right for every row ' i ' ; Count number of subarrays in temp [ ] having sum divisible by ' k ' and then add it to ' tot _ count ' ; required count of sub - matrices having sum divisible by ' k ' ; Driver code
import java . util . * ; class GFG { static final int SIZE = 10 ; static int subCount ( int arr [ ] , int n , int k ) { int mod [ ] = new int [ k ] ; Arrays . fill ( mod , 0 ) ; int cumSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumSum += arr [ i ] ; mod [ ( ( cumSum % k ) + k ) % k ] ++ ; } int result = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( mod [ i ] > 1 ) result += ( mod [ i ] * ( mod [ i ] - 1 ) ) / 2 ; result += mod [ 0 ] ; return result ; } static int countSubmatrix ( int mat [ ] [ ] , int n , int k ) { int tot_count = 0 ; int left , right , i ; int temp [ ] = new int [ n ] ; for ( left = 0 ; left < n ; left ++ ) { Arrays . fill ( temp , 0 ) ; for ( right = left ; right < n ; right ++ ) { for ( i = 0 ; i < n ; ++ i ) temp [ i ] += mat [ i ] [ right ] ; tot_count += subCount ( temp , n , k ) ; } } return tot_count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 5 , - 1 , 6 } , { - 2 , 3 , 8 } , { 7 , 4 , - 9 } } ; int n = 3 , k = 4 ; System . out . print ( " Count ▁ = ▁ " + countSubmatrix ( mat , n , k ) ) ; } }