codeparrot/xlcost-text-to-code huggingface dataset:
Shortest path in a Binary Maze | C ++ program to find the shortest path between a given source cell to a destination cell . ; To store matrix cell cordinates ; A Data Structure for queue used in BFS ; The cordinates of a cell ; cell 's distance of from the source ; check whether given cell ( row , col ) is a valid cell or not . ; return true if row number and column number is in range ; These arrays are used to get row and column numbers of 4 neighbours of a given cell ; function to find the shortest path between a given source cell to a destination cell . ; check source and destination cell of the matrix have value 1 ; Mark the source cell as visited ; Create a queue for BFS ; Distance of source cell is 0 ; Enqueue source cell ; Do a BFS starting from source cell ; If we have reached the destination cell , we are done ; Otherwise dequeue the front cell in the queue and enqueue its adjacent cells ; if adjacent cell is valid , has path and not visited yet , enqueue it . ; mark cell as visited and enqueue it ; Return - 1 if destination cannot be reached ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ROW  9 NEW_LINE #define COL  10 NEW_LINE struct Point { int x ; int y ; } ; struct queueNode { Point pt ; int dist ; } ; bool isValid ( int row , int col ) { return ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) ; } int rowNum [ ] = { -1 , 0 , 0 , 1 } ; int colNum [ ] = { 0 , -1 , 1 , 0 } ; int BFS ( int mat [ ] [ COL ] , Point src , Point dest ) { if ( ! mat [ src . x ] [ src . y ] ! mat [ dest . x ] [ dest . y ] ) return -1 ; bool visited [ ROW ] [ COL ] ; memset ( visited , false , sizeof visited ) ; visited [ src . x ] [ src . y ] = true ; queue < queueNode > q ; queueNode s = { src , 0 } ; q . push ( s ) ; while ( ! q . empty ( ) ) { queueNode curr = q . front ( ) ; Point pt = curr . pt ; if ( pt . x == dest . x && pt . y == dest . y ) return curr . dist ; q . pop ( ) ; for ( int i = 0 ; i < 4 ; i ++ ) { int row = pt . x + rowNum [ i ] ; int col = pt . y + colNum [ i ] ; if ( isValid ( row , col ) && mat [ row ] [ col ] && ! visited [ row ] [ col ] ) { visited [ row ] [ col ] = true ; queueNode Adjcell = { { row , col } , curr . dist + 1 } ; q . push ( Adjcell ) ; } } } return -1 ; } int main ( ) { int mat [ ROW ] [ COL ] = { { 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 } , { 1 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 } , { 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 } , { 1 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 } , { 1 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 } } ; Point source = { 0 , 0 } ; Point dest = { 3 , 4 } ; int dist = BFS ( mat , source , dest ) ; if ( dist != -1 ) cout << " Shortest ▁ Path ▁ is ▁ " << dist ; else cout << " Shortest ▁ Path ▁ doesn ' t ▁ exist " ; return 0 ; }
Convert a given Binary Tree to Doubly Linked List | Set 1 | A C ++ program for in - place conversion of Binary Tree to DLL ; A binary tree node has data , and left and right pointers ; This is the core function to convert Tree to list . This function follows steps 1 and 2 of the above algorithm ; Base case ; Convert the left subtree and link to root ; Convert the left subtree ; Find inorder predecessor . After this loop , left will point to the inorder predecessor ; Make root as next of the predecessor ; Make predecssor as previous of root ; Convert the right subtree and link to root ; Convert the right subtree ; Find inorder successor . After this loop , right will point to the inorder successor ; Make root as previous of successor ; Make successor as next of root ; The main function that first calls bintree2listUtil ( ) , then follows step 3 of the above algorithm ; Base case ; Convert to DLL using bintree2listUtil ( ) ; bintree2listUtil ( ) returns root node of the converted DLL . We need pointer to the leftmost node which is head of the constructed DLL , so move to the leftmost node ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Function to print nodes in a given doubly linked list ; Driver code ; Let us create the tree shown in above diagram ; Convert to DLL ; Print the converted list
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; } ; node * bintree2listUtil ( node * root ) { if ( root == NULL ) return root ; if ( root -> left != NULL ) { node * left = bintree2listUtil ( root -> left ) ; for ( ; left -> right != NULL ; left = left -> right ) ; left -> right = root ; root -> left = left ; } if ( root -> right != NULL ) { node * right = bintree2listUtil ( root -> right ) ; for ( ; right -> left != NULL ; right = right -> left ) ; right -> left = root ; root -> right = right ; } return root ; } node * bintree2list ( node * root ) { if ( root == NULL ) return root ; root = bintree2listUtil ( root ) ; while ( root -> left != NULL ) root = root -> left ; return ( root ) ; } node * newNode ( int data ) { node * new_node = new node ( ) ; new_node -> data = data ; new_node -> left = new_node -> right = NULL ; return ( new_node ) ; } void printList ( node * node ) { while ( node != NULL ) { cout << node -> data << " ▁ " ; node = node -> right ; } } int main ( ) { node * root = newNode ( 10 ) ; root -> left = newNode ( 12 ) ; root -> right = newNode ( 15 ) ; root -> left -> left = newNode ( 25 ) ; root -> left -> right = newNode ( 30 ) ; root -> right -> left = newNode ( 36 ) ; node * head = bintree2list ( root ) ; printList ( head ) ; return 0 ; }
A Boolean Matrix Question | C ++ Code For A Boolean Matrix Question ; Initialize all values of row [ ] as 0 ; Initialize all values of col [ ] as 0 ; Store the rows and columns to be marked as 1 in row [ ] and col [ ] arrays respectively ; Modify the input matrix mat [ ] using the above constructed row [ ] and col [ ] arrays ; A utility function to print a 2D matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define R  3 NEW_LINE #define C  4 NEW_LINE void modifyMatrix ( bool mat [ R ] [ C ] ) { bool row [ R ] ; bool col [ C ] ; int i , j ; for ( i = 0 ; i < R ; i ++ ) { row [ i ] = 0 ; } for ( i = 0 ; i < C ; i ++ ) { col [ i ] = 0 ; } for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { row [ i ] = 1 ; col [ j ] = 1 ; } } } for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { if ( row [ i ] == 1 col [ j ] == 1 ) { mat [ i ] [ j ] = 1 ; } } } } void printMatrix ( bool mat [ R ] [ C ] ) { int i , j ; for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { cout << mat [ i ] [ j ] ; } cout << endl ; } } int main ( ) { bool mat [ R ] [ C ] = { { 1 , 0 , 0 , 1 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } } ; cout << " Input ▁ Matrix ▁ STRNEWLINE " ; printMatrix ( mat ) ; modifyMatrix ( mat ) ; printf ( " Matrix ▁ after ▁ modification ▁ STRNEWLINE " ) ; printMatrix ( mat ) ; return 0 ; }
A Boolean Matrix Question |  ; variables to check if there are any 1 in first row and column ; updating the first row and col if 1 is encountered ; Modify the input matrix mat [ ] using the first row and first column of Matrix mat ; modify first row if there was any 1 ; modify first col if there was any 1 ; A utility function to print a 2D matrix ; Driver function to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define R  3 NEW_LINE #define C  4 NEW_LINE void modifyMatrix ( int mat [ R ] [ C ] ) { bool row_flag = false ; bool col_flag = false ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( i == 0 && mat [ i ] [ j ] == 1 ) row_flag = true ; if ( j == 0 && mat [ i ] [ j ] == 1 ) col_flag = true ; if ( mat [ i ] [ j ] == 1 ) { mat [ 0 ] [ j ] = 1 ; mat [ i ] [ 0 ] = 1 ; } } } for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 1 ; j < C ; j ++ ) { if ( mat [ 0 ] [ j ] == 1 mat [ i ] [ 0 ] == 1 ) { mat [ i ] [ j ] = 1 ; } } } if ( row_flag == true ) { for ( int i = 0 ; i < C ; i ++ ) { mat [ 0 ] [ i ] = 1 ; } } if ( col_flag == true ) { for ( int i = 0 ; i < R ; i ++ ) { mat [ i ] [ 0 ] = 1 ; } } } void printMatrix ( int mat [ R ] [ C ] ) { for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { cout << mat [ i ] [ j ] ; } cout << " STRNEWLINE " ; } } int main ( ) { int mat [ R ] [ C ] = { { 1 , 0 , 0 , 1 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } } ; cout << " Input ▁ Matrix ▁ : STRNEWLINE " ; printMatrix ( mat ) ; modifyMatrix ( mat ) ; cout << " Matrix ▁ After ▁ Modification ▁ : STRNEWLINE " ; printMatrix ( mat ) ; return 0 ; }
Given a Boolean Matrix , find k such that all elements in k ' th ▁ row ▁ are ▁ 0 ▁ and ▁ k ' th column are 1. | C ++ program to find i such that all entries in i ' th ▁ row ▁ are ▁ 0 ▁ and ▁ all ▁ entries ▁ in ▁ i ' t column are 1 ; Start from top - most rightmost corner ( We could start from other corners also ) ; Initialize result ; Find the index ( This loop runs at most 2 n times , we either increment row number or decrement column number ) ; If current element is 0 , then this row may be a solution ; Check for all elements in this row ; If all values are 0 , then store this row as result ; We reach here if we found a 1 in current row , so this row cannot be a solution , increment row number ; If current element is 1 ; Check for all elements in this column ; If all elements are 1 ; We reach here if we found a 0 in current column , so this column cannot be a solution , increment column number ; If we could not find result in above loop , then result doesn 't exist ; Check if above computed res is valid ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; #define n  5 NEW_LINE int find ( bool arr [ n ] [ n ] ) { int i = 0 , j = n - 1 ; int res = -1 ; while ( i < n && j >= 0 ) { if ( arr [ i ] [ j ] == 0 ) { while ( j >= 0 && ( arr [ i ] [ j ] == 0 i == j ) ) j -- ; if ( j == -1 ) { res = i ; break ; } else i ++ ; } else { while ( i < n && ( arr [ i ] [ j ] == 1 i == j ) ) i ++ ; if ( i == n ) { res = j ; break ; } else j -- ; } } if ( res == -1 ) return res ; for ( int i = 0 ; i < n ; i ++ ) if ( res != i && arr [ i ] [ res ] != 1 ) return -1 ; for ( int j = 0 ; j < n ; j ++ ) if ( res != j && arr [ res ] [ j ] != 0 ) return -1 ; return res ; } int main ( ) { bool mat [ n ] [ n ] = { { 0 , 0 , 1 , 1 , 0 } , { 0 , 0 , 0 , 1 , 0 } , { 1 , 1 , 1 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 } , { 1 , 1 , 1 , 1 , 1 } } ; cout << find ( mat ) ; return 0 ; }
Print unique rows in a given boolean matrix | Given a binary matrix of M X N of integers , you need to return only unique rows of binary array ; The main function that prints all unique rows in a given matrix . ; Traverse through the matrix ; check if there is similar column is already printed , i . e if i and jth column match . ; if no row is similar ; print the row ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ROW  4 NEW_LINE #define COL  5 NEW_LINE void findUniqueRows ( int M [ ROW ] [ COL ] ) { for ( int i = 0 ; i < ROW ; i ++ ) { int flag = 0 ; for ( int j = 0 ; j < i ; j ++ ) { flag = 1 ; for ( int k = 0 ; k <= COL ; k ++ ) if ( M [ i ] [ k ] != M [ j ] [ k ] ) flag = 0 ; if ( flag == 1 ) break ; } if ( flag == 0 ) { for ( int j = 0 ; j < COL ; j ++ ) cout << M [ i ] [ j ] << " ▁ " ; cout << endl ; } } } int main ( ) { int M [ ROW ] [ COL ] = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 0 , 0 } } ; findUniqueRows ( M ) ; return 0 ; }
Print unique rows in a given boolean matrix | Given a binary matrix of M X N of integers , you need to return only unique rows of binary array ; A Trie node ; Only two children needed for 0 and 1 ; A utility function to allocate memory for a new Trie node ; Inserts a new matrix row to Trie . If row is already present , then returns 0 , otherwise insets the row and return 1 ; base case ; Recur if there are more entries in this row ; If all entries of this row are processed ; unique row found , return 1 ; duplicate row found , return 0 ; A utility function to print a row ; The main function that prints all unique rows in a given matrix . ; create an empty Trie ; Iterate through all rows ; insert row to TRIE ; unique row found , print it ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ROW  4 NEW_LINE #define COL  5 NEW_LINE class Node { public : bool isEndOfCol ; Node * child [ 2 ] ; } ; Node * newNode ( ) { Node * temp = new Node ( ) ; temp -> isEndOfCol = 0 ; temp -> child [ 0 ] = temp -> child [ 1 ] = NULL ; return temp ; } bool insert ( Node * * root , int ( * M ) [ COL ] , int row , int col ) { if ( * root == NULL ) * root = newNode ( ) ; if ( col < COL ) return insert ( & ( ( * root ) -> child [ M [ row ] [ col ] ] ) , M , row , col + 1 ) ; else { if ( ! ( ( * root ) -> isEndOfCol ) ) return ( * root ) -> isEndOfCol = 1 ; return 0 ; } } void printRow ( int ( * M ) [ COL ] , int row ) { int i ; for ( i = 0 ; i < COL ; ++ i ) cout << M [ row ] [ i ] << " ▁ " ; cout << endl ; } void findUniqueRows ( int ( * M ) [ COL ] ) { Node * root = NULL ; int i ; for ( i = 0 ; i < ROW ; ++ i ) if ( insert ( & root , M , i , 0 ) ) printRow ( M , i ) ; } int main ( ) { int M [ ROW ] [ COL ] = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 0 , 0 } } ; findUniqueRows ( M ) ; return 0 ; }
Print unique rows in a given boolean matrix | C ++ code to print unique row in a given binary matrix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArray ( int arr [ ] [ 5 ] , int row , int col ) { unordered_set < string > uset ; for ( int i = 0 ; i < row ; i ++ ) { string s = " " ; for ( int j = 0 ; j < col ; j ++ ) s += to_string ( arr [ i ] [ j ] ) ; if ( uset . count ( s ) == 0 ) { uset . insert ( s ) ; cout << s << endl ; } } } int main ( ) { int arr [ ] [ 5 ] = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 0 , 1 } , { 1 , 1 , 1 , 0 , 0 } } ; printArray ( arr , 4 , 5 ) ; }
Find the largest rectangle of 1 's with swapping of columns allowed | C ++ program to find the largest rectangle of 1 's with swapping of columns allowed. ; Returns area of the largest rectangle of 1 's ; An auxiliary array to store count of consecutive 1 's  in every column. ; Step 1 : Fill the auxiliary array hist [ ] [ ] ; First row in hist [ ] [ ] is copy of first row in mat [ ] [ ] ; Fill remaining rows of hist [ ] [ ] ; Step 2 : Sort columns of hist [ ] [ ] in non - increasing order ; counting occurrence ; Traverse the count array from right side ; Step 3 : Traverse the sorted hist [ ] [ ] to find maximum area ; Since values are in decreasing order , The area ending with cell ( i , j ) can be obtained by multiplying column number with value of hist [ i ] [ j ] ; Driver program
#include <bits/stdc++.h> NEW_LINE #define R  3 NEW_LINE #define C  5 NEW_LINE using namespace std ; int maxArea ( bool mat [ R ] [ C ] ) { int hist [ R + 1 ] [ C + 1 ] ; for ( int i = 0 ; i < C ; i ++ ) { hist [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int j = 1 ; j < R ; j ++ ) hist [ j ] [ i ] = ( mat [ j ] [ i ] == 0 ) ? 0 : hist [ j - 1 ] [ i ] + 1 ; } for ( int i = 0 ; i < R ; i ++ ) { int count [ R + 1 ] = { 0 } ; for ( int j = 0 ; j < C ; j ++ ) count [ hist [ i ] [ j ] ] ++ ; int col_no = 0 ; for ( int j = R ; j >= 0 ; j -- ) { if ( count [ j ] > 0 ) { for ( int k = 0 ; k < count [ j ] ; k ++ ) { hist [ i ] [ col_no ] = j ; col_no ++ ; } } } } int curr_area , max_area = 0 ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { curr_area = ( j + 1 ) * hist [ i ] [ j ] ; if ( curr_area > max_area ) max_area = curr_area ; } } return max_area ; } int main ( ) { bool mat [ R ] [ C ] = { { 0 , 1 , 0 , 1 , 0 } , { 0 , 1 , 0 , 1 , 1 } , { 1 , 1 , 0 , 1 , 0 } } ; cout << " Area ▁ of ▁ the ▁ largest ▁ rectangle ▁ is ▁ " << maxArea ( mat ) ; return 0 ; }
Submatrix Sum Queries | C ++ program to compute submatrix query sum in O ( 1 ) time ; Function to preprcess input mat [ M ] [ N ] . This function mainly fills aux [ M ] [ N ] such that aux [ i ] [ j ] stores sum of elements from ( 0 , 0 ) to ( i , j ) ; Copy first row of mat [ ] [ ] to aux [ ] [ ] ; Do column wise sum ; Do row wise sum ; A O ( 1 ) time function to compute sum of submatrix between ( tli , tlj ) and ( rbi , rbj ) using aux [ ] [ ] which is built by the preprocess function ; result is now sum of elements between ( 0 , 0 ) and ( rbi , rbj ) ; Remove elements between ( 0 , 0 ) and ( tli - 1 , rbj ) ; Remove elements between ( 0 , 0 ) and ( rbi , tlj - 1 ) ; Add aux [ tli - 1 ] [ tlj - 1 ] as elements between ( 0 , 0 ) and ( tli - 1 , tlj - 1 ) are subtracted twice ; Driver program
#include <iostream> NEW_LINE using namespace std ; #define M  4 NEW_LINE #define N  5 NEW_LINE int preProcess ( int mat [ M ] [ N ] , int aux [ M ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) aux [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int i = 1 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) aux [ i ] [ j ] = mat [ i ] [ j ] + aux [ i - 1 ] [ j ] ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 1 ; j < N ; j ++ ) aux [ i ] [ j ] += aux [ i ] [ j - 1 ] ; } int sumQuery ( int aux [ M ] [ N ] , int tli , int tlj , int rbi , int rbj ) { int res = aux [ rbi ] [ rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 ] [ rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi ] [ tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 ] [ tlj - 1 ] ; return res ; } int main ( ) { int mat [ M ] [ N ] = { { 1 , 2 , 3 , 4 , 6 } , { 5 , 3 , 8 , 1 , 2 } , { 4 , 6 , 7 , 5 , 5 } , { 2 , 4 , 8 , 9 , 4 } } ; int aux [ M ] [ N ] ; preProcess ( mat , aux ) ; int tli = 2 , tlj = 2 , rbi = 3 , rbj = 4 ; cout << " Query1 : " tli = 0 , tlj = 0 , rbi = 1 , rbj = 1 ; cout << " Query2 : " tli = 1 , tlj = 2 , rbi = 3 , rbj = 3 ; cout << " Query3 : " return 0 ; }
Program for Rank of Matrix | C ++ program to find rank of a matrix ; function for exchanging two rows of a matrix ; Function to display a matrix ; function for finding rank of matrix ; Before we visit current row ' row ' , we make sure that mat [ row ] [ 0 ] , ... . mat [ row ] [ row - 1 ] are 0. Diagonal element is not zero ; This makes all entries of current column as 0 except entry ' mat [ row ] [ row ] ' ; Diagonal element is already zero . Two cases arise : 1 ) If there is a row below it with non - zero entry , then swap this row with that row and process that row 2 ) If all elements in current column below mat [ r ] [ row ] are 0 , then remvoe this column by swapping it with last column and reducing number of columns by 1. ; Find the non - zero element in current column ; Swap the row with non - zero element with this row . ; If we did not find any row with non - zero element in current columnm , then all values in this column are 0. ; Reduce number of columns ; Copy the last column here ; Process this row again ; Uncomment these lines to see intermediate results display ( mat , R , C ) ; printf ( " \n " ) ; ; function for displaying the matrix ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define R  3 NEW_LINE #define C  3 NEW_LINE void swap ( int mat [ R ] [ C ] , int row1 , int row2 , int col ) { for ( int i = 0 ; i < col ; i ++ ) { int temp = mat [ row1 ] [ i ] ; mat [ row1 ] [ i ] = mat [ row2 ] [ i ] ; mat [ row2 ] [ i ] = temp ; } } void display ( int mat [ R ] [ C ] , int row , int col ) ; int rankOfMatrix ( int mat [ R ] [ C ] ) { int rank = C ; for ( int row = 0 ; row < rank ; row ++ ) { if ( mat [ row ] [ row ] ) { for ( int col = 0 ; col < R ; col ++ ) { if ( col != row ) { double mult = ( double ) mat [ col ] [ row ] / mat [ row ] [ row ] ; for ( int i = 0 ; i < rank ; i ++ ) mat [ col ] [ i ] -= mult * mat [ row ] [ i ] ; } } } else { bool reduce = true ; for ( int i = row + 1 ; i < R ; i ++ ) { if ( mat [ i ] [ row ] ) { swap ( mat , row , i , rank ) ; reduce = false ; break ; } } if ( reduce ) { rank -- ; for ( int i = 0 ; i < R ; i ++ ) mat [ i ] [ row ] = mat [ i ] [ rank ] ; } row -- ; } } return rank ; } void display ( int mat [ R ] [ C ] , int row , int col ) { for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) printf ( " ▁ % d " , mat [ i ] [ j ] ) ; printf ( " STRNEWLINE " ) ; } } int main ( ) { int mat [ ] [ 3 ] = { { 10 , 20 , 10 } , { -20 , -30 , 10 } , { 30 , 50 , 0 } } ; printf ( " Rank ▁ of ▁ the ▁ matrix ▁ is ▁ : ▁ % d " , rankOfMatrix ( mat ) ) ; return 0 ; }
Maximum size rectangle binary sub | C ++ program to find largest rectangle with all 1 s in a binary matrix ; Finds the maximum area under the histogram represented by histogram . See below article for details . ; Create an empty stack . The stack holds indexes of hist [ ] array / The bars stored in stack are always in increasing order of their heights . ; Top of stack ; Initialize max area in current ; row ( or histogram ) Initialize area with current top ; Run through all bars of given histogram ( or row ) ; If this bar is higher than the bar on top stack , push it to stack ; If this bar is lower than top of stack , then calculate area of rectangle with stack top as the smallest ( or minimum height ) bar . ' i ' is ' right ▁ index ' for the top and element before top in stack is ' left ▁ index ' ; Now pop the remaining bars from stack and calculate area with every popped bar as the smallest bar ; Returns area of the largest rectangle with all 1 s in A [ ] [ ] ; Calculate area for first row and initialize it as result ; iterate over row to find maximum rectangular area considering each row as histogram ; if A [ i ] [ j ] is 1 then add A [ i - 1 ] [ j ] ; Update result if area with current row ( as last row ) of rectangle ) is more ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define R  4 NEW_LINE #define C  4 NEW_LINE int maxHist ( int row [ ] ) { stack < int > result ; int top_val ; int max_area = 0 ; int area = 0 ; int i = 0 ; while ( i < C ) { if ( result . empty ( ) || row [ result . top ( ) ] <= row [ i ] ) result . push ( i ++ ) ; else { top_val = row [ result . top ( ) ] ; result . pop ( ) ; area = top_val * i ; if ( ! result . empty ( ) ) area = top_val * ( i - result . top ( ) - 1 ) ; max_area = max ( area , max_area ) ; } } while ( ! result . empty ( ) ) { top_val = row [ result . top ( ) ] ; result . pop ( ) ; area = top_val * i ; if ( ! result . empty ( ) ) area = top_val * ( i - result . top ( ) - 1 ) ; max_area = max ( area , max_area ) ; } return max_area ; } int maxRectangle ( int A [ ] [ C ] ) { int result = maxHist ( A [ 0 ] ) ; for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) if ( A [ i ] [ j ] ) A [ i ] [ j ] += A [ i - 1 ] [ j ] ; result = max ( result , maxHist ( A [ i ] ) ) ; } return result ; } int main ( ) { int A [ ] [ C ] = { { 0 , 1 , 1 , 0 } , { 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 } , { 1 , 1 , 0 , 0 } , } ; cout << " Area ▁ of ▁ maximum ▁ rectangle ▁ is ▁ " << maxRectangle ( A ) ; return 0 ; }
Find sum of all elements in a matrix except the elements in row and / or column of given cell ? |  ; A structure to represent a cell index ; r is row , varies from 0 to R - 1 ; c is column , varies from 0 to C - 1 ; A simple solution to find sums for a given array of cell indexes ; Iterate through all cell indexes ; Compute sum for current cell index ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE #define R  3 NEW_LINE #define C  3 NEW_LINE using namespace std ; struct Cell { int r ; int c ; } ; void printSums ( int mat [ ] [ C ] , struct Cell arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 , r = arr [ i ] . r , c = arr [ i ] . c ; for ( int j = 0 ; j < R ; j ++ ) for ( int k = 0 ; k < C ; k ++ ) if ( j != r && k != c ) sum += mat [ j ] [ k ] ; cout << sum << endl ; } } int main ( ) { int mat [ ] [ C ] = { { 1 , 1 , 2 } , { 3 , 4 , 6 } , { 5 , 3 , 2 } } ; struct Cell arr [ ] = { { 0 , 0 } , { 1 , 1 } , { 0 , 1 } } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printSums ( mat , arr , n ) ; return 0 ; }