codeparrot/xlcost-text-to-code huggingface dataset:
Boundary elements of a Matrix | C ++ program to print boundary element of matrix . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; void printBoundary ( int a [ ] [ MAX ] , int m , int n ) { for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == 0 j == 0 i == n - 1 j == n - 1 ) cout << a [ i ] [ j ] << " ▁ " ; else cout << " ▁ " << " ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { int a [ 4 ] [ MAX ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printBoundary ( a , 4 , 4 ) ; return 0 ; }
Boundary elements of a Matrix | C ++ program to find sum of boundary elements of matrix . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; int getBoundarySum ( int a [ ] [ MAX ] , int m , int n ) { long long int sum = 0 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == 0 ) sum += a [ i ] [ j ] ; else if ( i == m - 1 ) sum += a [ i ] [ j ] ; else if ( j == 0 ) sum += a [ i ] [ j ] ; else if ( j == n - 1 ) sum += a [ i ] [ j ] ; } } return sum ; } int main ( ) { int a [ ] [ MAX ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; long long int sum = getBoundarySum ( a , 4 , 4 ) ; cout << " Sum ▁ of ▁ boundary ▁ elements ▁ is ▁ " << sum ; return 0 ; }
Print a matrix in a spiral form starting from a point | C ++ program to print a matrix in spiral form . ; Driver code ; Function calling
#include <iostream> NEW_LINE using namespace std ; const int MAX = 100 ; void printSpiral ( int mat [ ] [ MAX ] , int r , int c ) { int i , a = 0 , b = 2 ; int low_row = ( 0 > a ) ? 0 : a ; int low_column = ( 0 > b ) ? 0 : b - 1 ; int high_row = ( ( a + 1 ) >= r ) ? r - 1 : a + 1 ; int high_column = ( ( b + 1 ) >= c ) ? c - 1 : b + 1 ; while ( ( low_row > 0 - r && low_column > 0 - c ) ) { for ( i = low_column + 1 ; i <= high_column && i < c && low_row >= 0 ; ++ i ) cout << mat [ low_row ] [ i ] << " ▁ " ; low_row -= 1 ; for ( i = low_row + 2 ; i <= high_row && i < r && high_column < c ; ++ i ) cout << mat [ i ] [ high_column ] << " ▁ " ; high_column += 1 ; for ( i = high_column - 2 ; i >= low_column && i >= 0 && high_row < r ; -- i ) cout << mat [ high_row ] [ i ] << " ▁ " ; high_row += 1 ; for ( i = high_row - 2 ; i > low_row && i >= 0 && low_column >= 0 ; -- i ) cout << mat [ i ] [ low_column ] << " ▁ " ; low_column -= 1 ; } cout << endl ; } int main ( ) { int mat [ ] [ MAX ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int r = 3 , c = 3 ; printSpiral ( mat , r , c ) ; }
Find difference between sums of two diagonals | C ++ program to find the difference between the sum of diagonal . ; Initialize sums of diagonals ; finding sum of primary diagonal ; finding sum of secondary diagonal ; Absolute difference of the sums across the diagonals ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  100 NEW_LINE using namespace std ; int difference ( int arr [ ] [ MAX ] , int n ) { int d1 = 0 , d2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) d1 += arr [ i ] [ j ] ; if ( i == n - j - 1 ) d2 += arr [ i ] [ j ] ; } } return abs ( d1 - d2 ) ; } int main ( ) { int n = 3 ; int arr [ ] [ MAX ] = { { 11 , 2 , 4 } , { 4 , 5 , 6 } , { 10 , 8 , -12 } } ; cout << difference ( arr , n ) ; return 0 ; }
Find difference between sums of two diagonals | C ++ program to find the difference between the sum of diagonal . ; Initialize sums of diagonals ; Absolute difference of the sums across the diagonals ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  100 NEW_LINE using namespace std ; int difference ( int arr [ ] [ MAX ] , int n ) { int d1 = 0 , d2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { d1 += arr [ i ] [ i ] ; d2 += arr [ i ] [ n - i - 1 ] ; } return abs ( d1 - d2 ) ; } int main ( ) { int n = 3 ; int arr [ ] [ MAX ] = { { 11 , 2 , 4 } , { 4 , 5 , 6 } , { 10 , 8 , -12 } } ; cout << difference ( arr , n ) ; return 0 ; }
Circular Matrix ( Construct a matrix with numbers 1 to m * n in spiral way ) | C ++ program to fill a matrix with values from 1 to n * n in spiral fashion . ; Fills a [ m ] [ n ] with values from 1 to m * n in spiral fashion . ; Initialize value to be filled in matrix ; k - starting row index m - ending row index l - starting column index n - ending column index ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; void spiralFill ( int m , int n , int a [ ] [ MAX ] ) { int val = 1 ; int k = 0 , l = 0 ; while ( k < m && l < n ) { for ( int i = l ; i < n ; ++ i ) a [ k ] [ i ] = val ++ ; k ++ ; for ( int i = k ; i < m ; ++ i ) a [ i ] [ n - 1 ] = val ++ ; n -- ; if ( k < m ) { for ( int i = n - 1 ; i >= l ; -- i ) a [ m - 1 ] [ i ] = val ++ ; m -- ; } if ( l < n ) { for ( int i = m - 1 ; i >= k ; -- i ) a [ i ] [ l ] = val ++ ; l ++ ; } } } int main ( ) { int m = 4 , n = 4 ; int a [ MAX ] [ MAX ] ; spiralFill ( m , n , a ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) cout << a [ i ] [ j ] << " ▁ " ; cout << endl ; } return 0 ; }
Prufer Code to Tree Creation | C ++ program to construct tree from given Prufer Code ; Prints edges of tree represented by give Prufer code ; Initialize the array of vertices ; Number of occurrences of vertex in code ; Find the smallest label not present in prufer [ ] . ; If j + 1 is not present in prufer set ; Remove from Prufer set and print pair . ; For the last element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printTreeEdges ( int prufer [ ] , int m ) { int vertices = m + 2 ; int vertex_set [ vertices ] ; for ( int i = 0 ; i < vertices ; i ++ ) vertex_set [ i ] = 0 ; for ( int i = 0 ; i < vertices - 2 ; i ++ ) vertex_set [ prufer [ i ] - 1 ] += 1 ; cout << " The edge set E ( G ) is : " int j = 0 ; for ( int i = 0 ; i < vertices - 2 ; i ++ ) { for ( j = 0 ; j < vertices ; j ++ ) { if ( vertex_set [ j ] == 0 ) { vertex_set [ j ] = -1 ; cout << " ( " << ( j + 1 ) << " , ▁ " << prufer [ i ] << " ) ▁ " ; vertex_set [ prufer [ i ] - 1 ] -- ; break ; } } } j = 0 ; for ( int i = 0 ; i < vertices ; i ++ ) { if ( vertex_set [ i ] == 0 && j == 0 ) { cout << " ( " << ( i + 1 ) << " , ▁ " ; j ++ ; } else if ( vertex_set [ i ] == 0 && j == 1 ) cout << ( i + 1 ) << " ) STRNEWLINE " ; } } int main ( ) { int prufer [ ] = { 4 , 1 , 3 , 4 } ; int n = sizeof ( prufer ) / sizeof ( prufer [ 0 ] ) ; printTreeEdges ( prufer , n ) ; return 0 ; }
Maximum and Minimum in a square matrix . | C ++ program for finding maximum and minimum in a matrix . ; Finds maximum and minimum in arr [ 0. . n - 1 ] [ 0. . n - 1 ] using pair wise comparisons ; Traverses rows one by one ; Compare elements from beginning and end of current row ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE void maxMin ( int arr [ ] [ MAX ] , int n ) { int min = INT_MAX ; int max = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= n / 2 ; j ++ ) { if ( arr [ i ] [ j ] > arr [ i ] [ n - j - 1 ] ) { if ( min > arr [ i ] [ n - j - 1 ] ) min = arr [ i ] [ n - j - 1 ] ; if ( max < arr [ i ] [ j ] ) max = arr [ i ] [ j ] ; } else { if ( min > arr [ i ] [ j ] ) min = arr [ i ] [ j ] ; if ( max < arr [ i ] [ n - j - 1 ] ) max = arr [ i ] [ n - j - 1 ] ; } } } cout << " Maximum ▁ = ▁ " << max << " , ▁ Minimum ▁ = ▁ " << min ; } int main ( ) { int arr [ MAX ] [ MAX ] = { 5 , 9 , 11 , 25 , 0 , 14 , 21 , 6 , 4 } ; maxMin ( arr , 3 ) ; return 0 ; }
Print matrix in antispiral form | C ++ program to print matrix in anti - spiral form ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define R  4 NEW_LINE #define C  5 NEW_LINE void antiSpiralTraversal ( int m , int n , int a [ R ] [ C ] ) { int i , k = 0 , l = 0 ; stack < int > stk ; while ( k <= m && l <= n ) { for ( i = l ; i <= n ; ++ i ) stk . push ( a [ k ] [ i ] ) ; k ++ ; for ( i = k ; i <= m ; ++ i ) stk . push ( a [ i ] [ n ] ) ; n -- ; if ( k <= m ) { for ( i = n ; i >= l ; -- i ) stk . push ( a [ m ] [ i ] ) ; m -- ; } if ( l <= n ) { for ( i = m ; i >= k ; -- i ) stk . push ( a [ i ] [ l ] ) ; l ++ ; } } while ( ! stk . empty ( ) ) { cout << stk . top ( ) << " ▁ " ; stk . pop ( ) ; } } int main ( ) { int mat [ R ] [ C ] = { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 10 } , { 11 , 12 , 13 , 14 , 15 } , { 16 , 17 , 18 , 19 , 20 } } ; antiSpiralTraversal ( R - 1 , C - 1 , mat ) ; return 0 ; }
Minimum operations required to set all elements of binary matrix | C ++ program to find minimum operations required to set all the element of binary matrix ; Return minimum operation required to make all 1 s . ; check if this cell equals 0 ; increase the number of moves ; flip from this cell to the start point ; flip the cell ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define N  5 NEW_LINE #define M  5 NEW_LINE using namespace std ; int minOperation ( bool arr [ N ] [ M ] ) { int ans = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] == 0 ) { ans ++ ; for ( int k = 0 ; k <= i ; k ++ ) { for ( int h = 0 ; h <= j ; h ++ ) { if ( arr [ k ] [ h ] == 1 ) arr [ k ] [ h ] = 0 ; else arr [ k ] [ h ] = 1 ; } } } } } return ans ; } int main ( ) { bool mat [ N ] [ M ] = { 0 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; cout << minOperation ( mat ) << endl ; return 0 ; }
C Program To Check whether Matrix is Skew Symmetric or not | C program to check whether given matrix is skew - symmetric or not ; Utility function to create transpose matrix ; Utility function to check skew - symmetric matrix condition ; Utility function to print a matrix ; Driver program to test above functions ; Function create transpose matrix ; Check whether matrix is skew - symmetric or not
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE #define ROW  3 NEW_LINE #define COL  3 NEW_LINE void transpose ( int transpose_matrix [ ROW ] [ COL ] , int matrix [ ROW ] [ COL ] ) { for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) transpose_matrix [ j ] [ i ] = matrix [ i ] [ j ] ; } bool check ( int transpose_matrix [ ROW ] [ COL ] , int matrix [ ROW ] [ COL ] ) { for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) if ( matrix [ i ] [ j ] != - transpose_matrix [ i ] [ j ] ) return false ; return true ; } void printMatrix ( int matrix [ ROW ] [ COL ] ) { for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) printf ( " % d ▁ " , matrix [ i ] [ j ] ) ; printf ( " STRNEWLINE " ) ; } } int main ( ) { int matrix [ ROW ] [ COL ] = { { 0 , 5 , -4 } , { -5 , 0 , 1 } , { 4 , -1 , 0 } , } ; int transpose_matrix [ ROW ] [ COL ] ; transpose ( transpose_matrix , matrix ) ; printf ( " Transpose ▁ matrix : ▁ STRNEWLINE " ) ; printMatrix ( transpose_matrix ) ; if ( check ( transpose_matrix , matrix ) ) printf ( " Skew ▁ Symmetric ▁ Matrix " ) ; else printf ( " Not ▁ Skew ▁ Symmetric ▁ Matrix " ) ; return 0 ; }
Sum of matrix element where each elements is integer division of row and column | C ++ program to find sum of matrix element where each element is integer division of row and column . ; Return sum of matrix element where each element is division of its corresponding row and column . ; For each column . ; count the number of elements of each column . Initialize to i - 1 because number of zeroes are i - 1. ; For multiply ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int n ) { int ans = 0 , temp = 0 , num ; for ( int i = 1 ; i <= n && temp < n ; i ++ ) { temp = i - 1 ; num = 1 ; while ( temp < n ) { if ( temp + i <= n ) ans += ( i * num ) ; else ans += ( ( n - temp ) * num ) ; temp += i ; num ++ ; } } return ans ; } int main ( ) { int N = 2 ; cout << findSum ( N ) << endl ; return 0 ; }
Find number of transformation to make two Matrix Equal | C ++ program to find number of countOpsation to make two matrix equals ; Update matrix A [ ] [ ] so that only A [ ] [ ] has to be countOpsed ; Check necessary condition for condition for existence of full countOpsation ; If countOpsation is possible calculate total countOpsation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000 ; int countOps ( int A [ ] [ MAX ] , int B [ ] [ MAX ] , int m , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) A [ i ] [ j ] -= B [ i ] [ j ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 1 ; j < m ; j ++ ) if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [ 0 ] != 0 ) return -1 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += abs ( A [ i ] [ 0 ] ) ; for ( int j = 0 ; j < m ; j ++ ) result += abs ( A [ 0 ] [ j ] - A [ 0 ] [ 0 ] ) ; return ( result ) ; } int main ( ) { int A [ MAX ] [ MAX ] = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int B [ MAX ] [ MAX ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; cout << countOps ( A , B , 3 , 3 ) ; return 0 ; }
Construct the full k | C ++ program to build full k - ary tree from its preorder traversal and to print the postorder traversal of the tree . ; Structure of a node of an n - ary tree ; Utility function to create a new tree node with k children ; Function to build full k - ary tree ; For null tree ; For adding k children to a node ; Check if ind is in range of array Check if height of the tree is greater than 1 ; Recursively add each child ; Function to find the height of the tree ; Function to print postorder traversal of the tree ; Driver program to implement full k - ary tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; vector < Node * > child ; } ; Node * newNode ( int value ) { Node * nNode = new Node ; nNode -> key = value ; return nNode ; } Node * BuildKaryTree ( int A [ ] , int n , int k , int h , int & ind ) { if ( n <= 0 ) return NULL ; Node * nNode = newNode ( A [ ind ] ) ; if ( nNode == NULL ) { cout << " Memory ▁ error " << endl ; return NULL ; } for ( int i = 0 ; i < k ; i ++ ) { if ( ind < n - 1 && h > 1 ) { ind ++ ; nNode -> child . push_back ( BuildKaryTree ( A , n , k , h - 1 , ind ) ) ; } else { nNode -> child . push_back ( NULL ) ; } } return nNode ; } Node * BuildKaryTree ( int * A , int n , int k , int ind ) { int height = ( int ) ceil ( log ( ( double ) n * ( k - 1 ) + 1 ) / log ( ( double ) k ) ) ; return BuildKaryTree ( A , n , k , height , ind ) ; } void postord ( Node * root , int k ) { if ( root == NULL ) return ; for ( int i = 0 ; i < k ; i ++ ) postord ( root -> child [ i ] , k ) ; cout << root -> key << " ▁ " ; } int main ( ) { int ind = 0 ; int k = 3 , n = 10 ; int preorder [ ] = { 1 , 2 , 5 , 6 , 7 , 3 , 8 , 9 , 10 , 4 } ; Node * root = BuildKaryTree ( preorder , n , k , ind ) ; cout << " Postorder ▁ traversal ▁ of ▁ constructed " " ▁ full ▁ k - ary ▁ tree ▁ is : ▁ " ; postord ( root , k ) ; cout << endl ; return 0 ; }
Form coils in a matrix | C ++ program to print 2 coils of a 4 n x 4 n matrix . ; Print coils in a matrix of size 4 n x 4 n ; Number of elements in each coil ; Let us fill elements in coil 1. ; First element of coil1 4 * n * 2 * n + 2 * n ; ; Fill remaining m - 1 elements in coil1 [ ] ; Fill elements of current step from down to up ; Next element from current element ; Fill elements of current step from up to down . ; get coil2 from coil1 ; Print both coils ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printCoils ( int n ) { int m = 8 * n * n ; int coil1 [ m ] ; coil1 [ 0 ] = 8 * n * n + 2 * n ; int curr = coil1 [ 0 ] ; int nflg = 1 , step = 2 ; int index = 1 ; while ( index < m ) { for ( int i = 0 ; i < step ; i ++ ) { curr = coil1 [ index ++ ] = ( curr - 4 * n * nflg ) ; if ( index >= m ) break ; } if ( index >= m ) break ; for ( int i = 0 ; i < step ; i ++ ) { curr = coil1 [ index ++ ] = curr + nflg ; if ( index >= m ) break ; } nflg = nflg * ( -1 ) ; step += 2 ; } int coil2 [ m ] ; for ( int i = 0 ; i < 8 * n * n ; i ++ ) coil2 [ i ] = 16 * n * n + 1 - coil1 [ i ] ; cout << " Coil ▁ 1 ▁ : ▁ " ; for ( int i = 0 ; i < 8 * n * n ; i ++ ) cout << coil1 [ i ] << " ▁ " ; cout << " Coil 2 : " for ( int i = 0 ; i < 8 * n * n ; i ++ ) cout << coil2 [ i ] << " ▁ " ; } int main ( ) { int n = 1 ; printCoils ( n ) ; return 0 ; }
Sum of matrix in which each element is absolute difference of its row and column numbers | C ++ program to find sum of matrix in which each element is absolute difference of its corresponding row and column number row . ; Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Generate matrix ; Compute sum ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int n ) { int arr [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = abs ( i - j ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += arr [ i ] [ j ] ; return sum ; } int main ( ) { int n = 3 ; cout << findSum ( n ) << endl ; return 0 ; }
Sum of matrix in which each element is absolute difference of its row and column numbers | C ++ program to find sum of matrix in which each element is absolute difference of its corresponding row and column number row . ; Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += i * ( n - i ) ; return 2 * sum ; } int main ( ) { int n = 3 ; cout << findSum ( n ) << endl ; return 0 ; }
Sum of matrix in which each element is absolute difference of its row and column numbers | C ++ program to find sum of matrix in which each element is absolute difference of its corresponding row and column number row . ; Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int n ) { n -- ; int sum = 0 ; sum += ( n * ( n + 1 ) ) / 2 ; sum += ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; return sum ; } int main ( ) { int n = 3 ; cout << findSum ( n ) << endl ; return 0 ; }
Sum of both diagonals of a spiral odd | C ++ program to find sum of diagonals of spiral matrix ; function returns sum of diagonals ; as order should be only odd we should pass only odd - integers ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int spiralDiaSum ( int n ) { if ( n == 1 ) return 1 ; return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) ) ; } int main ( ) { int n = 7 ; cout << spiralDiaSum ( n ) ; return 0 ; }
Find perimeter of shapes formed with 1 s in binary matrix | C ++ program to find perimeter of area coverede by 1 in 2D matrix consisits of 0 ' s ▁ and ▁ 1' s . ; Find the number of covered side for mat [ i ] [ j ] . ; UP ; LEFT ; DOWN ; RIGHT ; Returns sum of perimeter of shapes formed with 1 s ; Traversing the matrix and finding ones to calculate their contribution . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define R  3 NEW_LINE #define C  5 NEW_LINE int numofneighbour ( int mat [ ] [ C ] , int i , int j ) { int count = 0 ; if ( i > 0 && mat [ i - 1 ] [ j ] ) count ++ ; if ( j > 0 && mat [ i ] [ j - 1 ] ) count ++ ; if ( i < R - 1 && mat [ i + 1 ] [ j ] ) count ++ ; if ( j < C - 1 && mat [ i ] [ j + 1 ] ) count ++ ; return count ; } int findperimeter ( int mat [ R ] [ C ] ) { int perimeter = 0 ; for ( int i = 0 ; i < R ; i ++ ) for ( int j = 0 ; j < C ; j ++ ) if ( mat [ i ] [ j ] ) perimeter += ( 4 - numofneighbour ( mat , i , j ) ) ; return perimeter ; } int main ( ) { int mat [ R ] [ C ] = { 0 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , } ; cout << findperimeter ( mat ) << endl ; return 0 ; }
Construct Binary Tree from String with bracket representation | C ++ program to construct a binary tree from the given string ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node ; This function is here just to test ; function to return the index of close parenthesis ; Inbuilt stack ; if open parenthesis , push it ; if close parenthesis ; if stack is empty , this is the required index ; if not found return - 1 ; function to construct tree from string ; Base case ; new root ; if next char is ' ( ' find the index of its complement ' ) ' ; if index found ; call for left subtree ; call for right subtree ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * node = ( Node * ) malloc ( sizeof ( Node ) ) ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } void preOrder ( Node * node ) { if ( node == NULL ) return ; printf ( " % d ▁ " , node -> data ) ; preOrder ( node -> left ) ; preOrder ( node -> right ) ; } int findIndex ( string str , int si , int ei ) { if ( si > ei ) return -1 ; stack < char > s ; for ( int i = si ; i <= ei ; i ++ ) { if ( str [ i ] == ' ( ' ) s . push ( str [ i ] ) ; else if ( str [ i ] == ' ) ' ) { if ( s . top ( ) == ' ( ' ) { s . pop ( ) ; if ( s . empty ( ) ) return i ; } } } return -1 ; } Node * treeFromString ( string str , int si , int ei ) { if ( si > ei ) return NULL ; Node * root = newNode ( str [ si ] - '0' ) ; int index = -1 ; if ( si + 1 <= ei && str [ si + 1 ] == ' ( ' ) index = findIndex ( str , si + 1 , ei ) ; if ( index != -1 ) { root -> left = treeFromString ( str , si + 2 , index - 1 ) ; root -> right = treeFromString ( str , index + 2 , ei - 1 ) ; } return root ; } int main ( ) { string str = "4(2(3 ) ( 1 ) ) (6(5 ) ) " ; Node * root = treeFromString ( str , 0 , str . length ( ) - 1 ) ; preOrder ( root ) ; }
Continuous Tree | CPP Code to check if the tree is continuous or not ; Node structure ; Function to check if the tree is continuous or not ; If root is Null then tree isn 't Continuous ; BFS Traversal ; Move to left child ; if difference between parent and child is equal to 1 then do continue otherwise make flag = 0 and break ; Move to right child ; if difference between parent and child is equal to 1 then do continue otherwise make flag = 0 and break ; Driver Code ; Constructing the Tree ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int val ; node * left ; node * right ; node ( ) : val ( 0 ) , left ( nullptr ) , right ( nullptr ) { } node ( int x ) : val ( x ) , left ( nullptr ) , right ( nullptr ) { } node ( int x , node * left , node * right ) : val ( x ) , left ( left ) , right ( right ) { } } ; bool continuous ( struct node * root ) { if ( root == NULL ) return false ; int flag = 1 ; queue < struct node * > Q ; Q . push ( root ) ; node * temp ; while ( ! Q . empty ( ) ) { temp = Q . front ( ) ; Q . pop ( ) ; if ( temp -> left ) { if ( abs ( temp -> left -> val - temp -> val ) == 1 ) Q . push ( temp -> left ) ; else { flag = 0 ; break ; } } if ( temp -> right ) { if ( abs ( temp -> right -> val - temp -> val ) == 1 ) Q . push ( temp -> right ) ; else { flag = 0 ; break ; } } } if ( flag ) return true ; else return false ; } int main ( ) { struct node * root = new node ( 3 ) ; root -> left = new node ( 2 ) ; root -> right = new node ( 4 ) ; root -> left -> left = new node ( 1 ) ; root -> left -> right = new node ( 3 ) ; root -> right -> right = new node ( 5 ) ; if ( continuous ( root ) ) cout << " True STRNEWLINE " ; else cout << " False STRNEWLINE " ; return 0 ; }