codeparrot/xlcost-text-to-code huggingface dataset:
Construct a Binary Tree from Postorder and Inorder | C ++ program for above approach ; Node class ; Constructor ; Tree building function ; Create Stack of type Node * ; Create Set 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 . top ( ) 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; Node ( int x ) { data = x ; left = right = NULL ; } } ; Node * buildTree ( int in [ ] , int post [ ] , int n ) { stack < Node * > st ; set < Node * > s ; 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 . find ( st . top ( ) ) != s . end ( ) ) { s . erase ( st . top ( ) ) ; st . top ( ) -> left = node ; st . pop ( ) ; } else { st . top ( ) -> right = node ; } } st . push ( node ) ; } while ( post [ p -- ] != in [ i ] && p >= 0 ) ; node = NULL ; while ( st . size ( ) > 0 && i >= 0 && st . top ( ) -> data == in [ i ] ) { node = st . top ( ) ; st . pop ( ) ; i -- ; } if ( node != NULL ) { s . insert ( node ) ; st . push ( node ) ; } } return root ; } void preOrder ( Node * node ) { if ( node == NULL ) return ; printf ( " % d ▁ " , node -> data ) ; preOrder ( node -> left ) ; preOrder ( node -> right ) ; } int main ( ) { int in [ ] = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int post [ ] = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = sizeof ( in ) / sizeof ( in [ 0 ] ) ; Node * root = buildTree ( in , post , n ) ; cout << " Preorder ▁ of ▁ the ▁ constructed STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL tree ▁ : ▁ STRNEWLINE " ; preOrder ( root ) ; return 0 ; }
Mirror of matrix across diagonal | Simple CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; void imageSwap ( int mat [ ] [ MAX ] , int n ) { int row = 0 ; for ( int j = 0 ; j < n ; j ++ ) { stack < int > s ; 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 . top ( ) ; s . pop ( ) ; } } int column = n - 1 ; for ( int j = 1 ; j < n ; j ++ ) { stack < int > s ; 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 . top ( ) ; s . pop ( ) ; } } } void printMatrix ( int mat [ ] [ MAX ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) cout << mat [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int mat [ ] [ MAX ] = { { 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 ) ; return 0 ; }
Mirror of matrix across diagonal | Efficient CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; void imageSwap ( int mat [ ] [ MAX ] , 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 ] ) ; } void printMatrix ( int mat [ ] [ MAX ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) cout << mat [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int mat [ ] [ MAX ] = { { 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 ) ; return 0 ; }
Find all rectangles filled with 0 | C ++ program for the above approach ; flag to check column edge case , initializing with 0 ; flag to check row edge case , initializing with 0 ; loop breaks where first 1 encounters ; set the flag ; pass because already processed ; loop breaks where first 1 encounters ; set the flag ; fill rectangle elements with any number so that we can exclude next time ; when end point touch the boundary ; when end point touch the boundary ; retrieving the column size of array ; output array where we are going to store our output ; It will be used for storing start and end location in the same index ; storing initial position of rectangle ; will be used for the last position ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findend ( int i , int j , vector < vector < int > > & a , vector < vector < int > > & output , int index ) { int x = a . size ( ) ; int y = a [ 0 ] . size ( ) ; int flagc = 0 ; int flagr = 0 ; int n , m ; for ( m = i ; m < x ; m ++ ) { if ( a [ m ] [ j ] == 1 ) { flagr = 1 ; break ; } if ( a [ m ] [ j ] == 5 ) continue ; for ( n = j ; n < y ; n ++ ) { if ( a [ m ] [ n ] == 1 ) { flagc = 1 ; break ; } a [ m ] [ n ] = 5 ; } } if ( flagr == 1 ) output [ index ] . push_back ( m - 1 ) ; else output [ index ] . push_back ( m ) ; if ( flagc == 1 ) output [ index ] . push_back ( n - 1 ) ; else output [ index ] . push_back ( n ) ; } void get_rectangle_coordinates ( vector < vector < int > > a ) { int size_of_array = a . size ( ) ; vector < vector < int > > output ; int index = -1 ; for ( int i = 0 ; i < size_of_array ; i ++ ) { for ( int j = 0 ; j < a [ 0 ] . size ( ) ; j ++ ) { if ( a [ i ] [ j ] == 0 ) { output . push_back ( { i , j } ) ; index = index + 1 ; findend ( i , j , a , output , index ) ; } } } cout << " [ " ; int aa = 2 , bb = 0 ; for ( auto i : output ) { bb = 3 ; cout << " [ " ; for ( int j : i ) { if ( bb ) cout << j << " , ▁ " ; else cout << j ; bb -- ; } cout << " ] " ; if ( aa ) cout << " , ▁ " ; aa -- ; } cout << " ] " ; } int main ( ) { vector < vector < int > > tests = { { 1 , 1 , 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 0 , 0 , 0 , 1 } , { 1 , 0 , 1 , 0 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 1 , 1 , 1 } , { 1 , 0 , 1 , 0 , 0 , 0 , 0 } , { 1 , 1 , 1 , 0 , 0 , 0 , 1 } , { 1 , 1 , 1 , 1 , 1 , 1 , 1 } } ; get_rectangle_coordinates ( tests ) ; return 0 ; }
Search in a row wise and column wise sorted matrix | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int search ( int mat [ 4 ] [ 4 ] , 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 ) { cout << " Element ▁ found ▁ at ▁ ( " << i << " , ▁ " << j << " ) STRNEWLINE " ; return 1 ; } } cout << " n ▁ Element ▁ not ▁ found " ; return 0 ; } int main ( ) { int mat [ 4 ] [ 4 ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; search ( mat , 4 , 29 ) ; return 0 ; }
Search in a row wise and column wise sorted matrix | C ++ 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 ; set indexes for top right element ; Check if mat [ i ] [ j ] < x ; if ( i == n j == - 1 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int search ( int mat [ 4 ] [ 4 ] , int n , int x ) { if ( n == 0 ) return -1 ; int smallest = mat [ 0 ] [ 0 ] , largest = mat [ n - 1 ] [ n - 1 ] ; if ( x < smallest x > largest ) return -1 ; int i = 0 , j = n - 1 ; while ( i < n && j >= 0 ) { if ( mat [ i ] [ j ] == x ) { cout << " n ▁ Found ▁ at ▁ " << i << " , ▁ " << j ; return 1 ; } if ( mat [ i ] [ j ] > x ) j -- ; else i ++ ; } cout << " n ▁ Element ▁ not ▁ found " ; return 0 ; } int main ( ) { int mat [ 4 ] [ 4 ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; search ( mat , 4 , 29 ) ; return 0 ; }
Zigzag ( or diagonal ) traversal of Matrix |  ; we will use a 2D vector to store the diagonals of our array the 2D vector will have ( n + m - 1 ) rows that is equal to the number of diagnols ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE #define R  5 NEW_LINE #define C  4 NEW_LINE using namespace std ; void diagonalOrder ( int arr [ ] [ C ] , int n , int m ) { vector < vector < int > > ans ( n + m - 1 ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { ans [ i + j ] . push_back ( arr [ j ] [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { for ( int j = 0 ; j < ans [ i ] . size ( ) ; j ++ ) cout << ans [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int n = 5 , m = 4 ; int arr [ ] [ C ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } , { 17 , 18 , 19 , 20 } , } ; diagonalOrder ( arr , n , m ) ; return 0 ; }
Minimum cost to sort a matrix of numbers from 0 to n ^ 2 | C ++ implementation to find the total energy required to rearrange the numbers ; function to find the total energy required to rearrange the numbers ; nested loops to access the elements of the given matrix ; store quotient ; final destination location ( i_des , j_des ) of the element mat [ i ] [ j ] is being calculated ; energy required for the movement of the element mat [ i ] [ j ] is calculated and then accumulated in the ' tot _ energy ' ; required total energy ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  100 NEW_LINE int calculateEnergy ( int mat [ SIZE ] [ SIZE ] , int n ) { int i_des , j_des , q ; int tot_energy = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { q = mat [ i ] [ j ] / n ; i_des = q ; j_des = mat [ i ] [ j ] - ( n * q ) ; tot_energy += abs ( i_des - i ) + abs ( j_des - j ) ; } } return tot_energy ; } int main ( ) { int mat [ SIZE ] [ SIZE ] = { { 4 , 7 , 0 , 3 } , { 8 , 5 , 6 , 1 } , { 9 , 11 , 10 , 2 } , { 15 , 13 , 14 , 12 } } ; int n = 4 ; cout << " Total ▁ energy ▁ required ▁ = ▁ " << calculateEnergy ( mat , n ) << " ▁ units " ; return 0 ; }
Unique cells in a binary matrix | C ++ program to count unique cells in a matrix ; Returns true if mat [ i ] [ j ] is unique ; checking in row calculating sumrow will be moving column wise ; checking in column calculating sumcol will be moving row wise ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; bool isUnique ( int mat [ ] [ MAX ] , int i , int j , int n , int m ) { int sumrow = 0 ; for ( int k = 0 ; k < m ; k ++ ) { sumrow += mat [ i ] [ k ] ; if ( sumrow > 1 ) return false ; } int sumcol = 0 ; for ( int k = 0 ; k < n ; k ++ ) { sumcol += mat [ k ] [ j ] ; if ( sumcol > 1 ) return false ; } return true ; } int countUnique ( int mat [ ] [ MAX ] , int n , int m ) { int uniquecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i ] [ j ] && isUnique ( mat , i , j , n , m ) ) uniquecount ++ ; return uniquecount ; } int main ( ) { int mat [ ] [ MAX ] = { { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 1 , 0 , 0 , 1 } } ; cout << countUnique ( mat , 3 , 4 ) ; return 0 ; }
Unique cells in a binary matrix | Efficient C ++ program to count unique cells in a binary matrix ; Count number of 1 s in each row and in each column ; Using above count arrays , find cells ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; int countUnique ( int mat [ ] [ MAX ] , int n , int m ) { int rowsum [ n ] , colsum [ m ] ; memset ( colsum , 0 , sizeof ( colsum ) ) ; memset ( rowsum , 0 , sizeof ( rowsum ) ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i ] [ j ] ) { rowsum [ i ] ++ ; colsum [ j ] ++ ; } int uniquecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i ] [ j ] && rowsum [ i ] == 1 && colsum [ j ] == 1 ) uniquecount ++ ; return uniquecount ; } int main ( ) { int mat [ ] [ MAX ] = { { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 1 , 0 , 0 , 1 } } ; cout << countUnique ( mat , 3 , 4 ) ; return 0 ; }
Check if a given matrix is sparse or not | CPP code to check if a matrix is sparse . ; Count number of zeros in the matrix ; Driver Function
#include <iostream> NEW_LINE using namespace std ; const int MAX = 100 ; bool isSparse ( int array [ ] [ MAX ] , int m , int n ) { int counter = 0 ; for ( int i = 0 ; i < m ; ++ i ) for ( int j = 0 ; j < n ; ++ j ) if ( array [ i ] [ j ] == 0 ) ++ counter ; return ( counter > ( ( m * n ) / 2 ) ) ; } int main ( ) { int array [ ] [ MAX ] = { { 1 , 0 , 3 } , { 0 , 0 , 4 } , { 6 , 0 , 0 } } ; int m = 3 , n = 3 ; if ( isSparse ( array , m , n ) ) cout << " Yes " ; else cout << " No " ; }
Row | CPP program to find common elements in two diagonals . ; Returns count of row wise same elements in two diagonals of mat [ n ] [ n ] ; Driver Code
#include <iostream> NEW_LINE #define MAX  100 NEW_LINE using namespace std ; int countCommon ( int mat [ ] [ MAX ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( mat [ i ] [ i ] == mat [ i ] [ n - i - 1 ] ) res ++ ; return res ; } int main ( ) { int mat [ ] [ MAX ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; cout << countCommon ( mat , 3 ) ; return 0 ; }
Check if sums of i |  ; Function to check the if sum of a row is same as corresponding column ; Driver Code ; number of rows ; number of columns
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; bool areSumSame ( int a [ ] [ MAX ] , int n , int m ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum1 = 0 , sum2 = 0 ; for ( int j = 0 ; j < m ; j ++ ) { sum1 += a [ i ] [ j ] ; sum2 += a [ j ] [ i ] ; } if ( sum1 == sum2 ) return true ; } return false ; } int main ( ) { int n = 4 ; int m = 4 ; int M [ n ] [ MAX ] = { { 1 , 2 , 3 , 4 } , { 9 , 5 , 3 , 1 } , { 0 , 3 , 5 , 6 } , { 0 , 4 , 5 , 6 } } ; cout << areSumSame ( M , n , m ) << " STRNEWLINE " ; return 0 ; }
Creating a tree with Left | C ++ program to create a tree with left child right sibling representation . ; Creating new Node ; Adds a sibling to a list with starting with n ; Add child Node to a Node ; Check if child list is not empty . ; Traverses tree in depth first order ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; struct Node * child ; } ; Node * newNode ( int data ) { Node * newNode = new Node ; newNode -> next = newNode -> child = NULL ; newNode -> data = data ; return newNode ; } Node * addSibling ( Node * n , int data ) { if ( n == NULL ) return NULL ; while ( n -> next ) n = n -> next ; return ( n -> next = newNode ( data ) ) ; } Node * addChild ( Node * n , int data ) { if ( n == NULL ) return NULL ; if ( n -> child ) return addSibling ( n -> child , data ) ; else return ( n -> child = newNode ( data ) ) ; } void traverseTree ( Node * root ) { if ( root == NULL ) return ; while ( root ) { cout << " ▁ " << root -> data ; if ( root -> child ) traverseTree ( root -> child ) ; root = root -> next ; } } int main ( ) { Node * root = newNode ( 10 ) ; Node * n1 = addChild ( root , 2 ) ; Node * n2 = addChild ( root , 3 ) ; Node * n3 = addChild ( root , 4 ) ; Node * n4 = addChild ( n3 , 6 ) ; Node * n5 = addChild ( root , 5 ) ; Node * n6 = addChild ( n5 , 7 ) ; Node * n7 = addChild ( n5 , 8 ) ; Node * n8 = addChild ( n5 , 9 ) ; traverseTree ( root ) ; return 0 ; }
Find row number of a binary matrix having maximum number of 1 s | CPP program to find row with maximum 1 in row sorted binary matrix ; function for finding row with maximum 1 ; find left most position of 1 in a row find 1 st zero in a row ; driver program
#include <bits/stdc++.h> NEW_LINE #define N  4 NEW_LINE using namespace std ; void findMax ( int arr [ ] [ N ] ) { int row = 0 , i , j ; for ( i = 0 , j = N - 1 ; i < N ; i ++ ) { while ( arr [ i ] [ j ] == 1 && j >= 0 ) { row = i ; j -- ; } } cout << " Row ▁ number ▁ = ▁ " << row + 1 ; cout << " , ▁ MaxCount ▁ = ▁ " << N - 1 - j ; } int main ( ) { int arr [ N ] [ N ] = { 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 } ; findMax ( arr ) ; return 0 ; }
Find if a 2 | C ++ program for the above approach ; function which tells all cells are visited or not ; starting cell values ; if we get { 0 , 0 } before the end of loop then returns false . Because it means we didn 't traverse all the cells ; If found cycle then return false ; Update startx and starty values to next cell values ; finally if we reach our goal then returns true ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isAllCellTraversed ( vector < vector < pair < int , int > > > grid , int n , int m ) { bool visited [ n ] [ m ] ; int total = n * m ; int startx = grid [ 0 ] [ 0 ] . first ; int starty = grid [ 0 ] [ 0 ] . second ; for ( int i = 0 ; i < total - 2 ; i ++ ) { if ( grid [ startx ] [ starty ] . first == -1 and grid [ startx ] [ starty ] . second == -1 ) return false ; if ( visited [ startx ] [ starty ] == true ) return false ; visited [ startx ] [ starty ] = true ; int x = grid [ startx ] [ starty ] . first ; int y = grid [ startx ] [ starty ] . second ; startx = x ; starty = y ; } if ( grid [ startx ] [ starty ] . first == -1 and grid [ startx ] [ starty ] . second == -1 ) return true ; return false ; } int main ( ) { vector < vector < pair < int , int > > > cell ( 3 , vector < pair < int , int > > ( 2 ) ) ; cell [ 0 ] [ 0 ] = { 0 , 1 } ; cell [ 0 ] [ 1 ] = { 2 , 0 } ; cell [ 1 ] [ 0 ] = { -1 , -1 } ; cell [ 1 ] [ 1 ] = { 1 , 0 } ; cell [ 2 ] [ 0 ] = { 2 , 1 } ; cell [ 2 ] [ 1 ] = { 1 , 1 } ; if ( ! isAllCellTraversed ( cell , 3 , 2 ) ) cout << " true " ; else cout << " false " ; return 0 ; }
Print all palindromic paths from top left to bottom right in a matrix | C ++ program to print all palindromic paths from top left to bottom right in a grid . ; i and j are row and column indexes of current cell ( initially these are 0 and 0 ) . ; If we have not reached bottom right corner , keep exlporing ; If we reach bottom right corner , we check if if the path used is palindrome or not . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  4 NEW_LINE bool isPalin ( string str ) { int len = str . length ( ) / 2 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] != str [ str . length ( ) - i - 1 ] ) return false ; } return true ; } void palindromicPath ( string str , char a [ ] [ N ] , int i , int j , int m , int n ) { if ( j < m - 1 i < n - 1 ) { if ( i < n - 1 ) palindromicPath ( str + a [ i ] [ j ] , a , i + 1 , j , m , n ) ; if ( j < m - 1 ) palindromicPath ( str + a [ i ] [ j ] , a , i , j + 1 , m , n ) ; } else { str = str + a [ n - 1 ] [ m - 1 ] ; if ( isPalin ( str ) ) cout << ( str ) << endl ; } } int main ( ) { char arr [ ] [ N ] = { { ' a ' , ' a ' , ' a ' , ' b ' } , { ' b ' , ' a ' , ' a ' , ' a ' } , { ' a ' , ' b ' , ' b ' , ' a ' } } ; string str = " " ; palindromicPath ( str , arr , 0 , 0 , 4 , 3 ) ; return 0 ; }
Possible moves of knight | CPP program to find number of possible moves of knight ; To calculate possible moves ; All possible moves of a knight ; Check if each possible move is valid or not ; Position of knight after move ; count valid moves ; Return number of possible moves ; Driver program to check findPossibleMoves ( )
#include <bits/stdc++.h> NEW_LINE #define n  4 NEW_LINE #define m  4 NEW_LINE using namespace std ; int findPossibleMoves ( int mat [ n ] [ m ] , int p , int q ) { int X [ 8 ] = { 2 , 1 , -1 , -2 , -2 , -1 , 1 , 2 } ; int Y [ 8 ] = { 1 , 2 , 2 , 1 , -1 , -2 , -2 , -1 } ; int count = 0 ; for ( int i = 0 ; i < 8 ; i ++ ) { int x = p + X [ i ] ; int y = q + Y [ i ] ; if ( x >= 0 && y >= 0 && x < n && y < m && mat [ x ] [ y ] == 0 ) count ++ ; } return count ; } int main ( ) { int mat [ n ] [ m ] = { { 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 1 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 1 } } ; int p = 2 , q = 2 ; cout << findPossibleMoves ( mat , p , q ) ; return 0 ; }
Efficiently compute sums of diagonals of a matrix | A simple C ++ program to find sum of diagonals ; Condition for principal diagonal ; Condition for secondary diagonal ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; void printDiagonalSums ( int mat [ ] [ MAX ] , int n ) { int principal = 0 , secondary = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) principal += mat [ i ] [ j ] ; if ( ( i + j ) == ( n - 1 ) ) secondary += mat [ i ] [ j ] ; } } cout << " Principal ▁ Diagonal : " << principal << endl ; cout << " Secondary ▁ Diagonal : " << secondary << endl ; } int main ( ) { int a [ ] [ MAX ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printDiagonalSums ( a , 4 ) ; return 0 ; }
Efficiently compute sums of diagonals of a matrix | An efficient C ++ program to find sum of diagonals ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; void printDiagonalSums ( int mat [ ] [ MAX ] , int n ) { int principal = 0 , secondary = 0 ; for ( int i = 0 ; i < n ; i ++ ) { principal += mat [ i ] [ i ] ; secondary += mat [ i ] [ n - i - 1 ] ; } cout << " Principal ▁ Diagonal : " << principal << endl ; cout << " Secondary ▁ Diagonal : " << secondary << endl ; } int main ( ) { int a [ ] [ MAX ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printDiagonalSums ( a , 4 ) ; return 0 ; }
Creating a tree with Left | C ++ program to create a tree with left child right sibling representation . ; Creating new Node ; Adds a sibling to a list with starting with n ; Add child Node to a Node ; Check if child list is not empty . ; Traverses tree in level order ; Corner cases ; Create a queue and enque root ; Take out an item from the queue ; Print next level of taken out item and enque next level 's children ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; struct Node * child ; } ; Node * newNode ( int data ) { Node * newNode = new Node ; newNode -> next = newNode -> child = NULL ; newNode -> data = data ; return newNode ; } Node * addSibling ( Node * n , int data ) { if ( n == NULL ) return NULL ; while ( n -> next ) n = n -> next ; return ( n -> next = newNode ( data ) ) ; } Node * addChild ( Node * n , int data ) { if ( n == NULL ) return NULL ; if ( n -> child ) return addSibling ( n -> child , data ) ; else return ( n -> child = newNode ( data ) ) ; } void traverseTree ( Node * root ) { if ( root == NULL ) return ; cout << root -> data << " ▁ " ; if ( root -> child == NULL ) return ; queue < Node * > q ; Node * curr = root -> child ; q . push ( curr ) ; while ( ! q . empty ( ) ) { curr = q . front ( ) ; q . pop ( ) ; while ( curr != NULL ) { cout << curr -> data << " ▁ " ; if ( curr -> child != NULL ) { q . push ( curr -> child ) ; } curr = curr -> next ; } } } int main ( ) { Node * root = newNode ( 10 ) ; Node * n1 = addChild ( root , 2 ) ; Node * n2 = addChild ( root , 3 ) ; Node * n3 = addChild ( root , 4 ) ; Node * n4 = addChild ( n3 , 6 ) ; Node * n5 = addChild ( root , 5 ) ; Node * n6 = addChild ( n5 , 7 ) ; Node * n7 = addChild ( n5 , 8 ) ; Node * n8 = addChild ( n5 , 9 ) ; traverseTree ( root ) ; return 0 ; }