codeparrot/xlcost-text-to-code huggingface dataset:
Print nodes at k distance from root |  ; A binary tree node has data , pointer to left child and a pointer to right child ; Driver code ; Constructed binary tree is 1 / \ 2 3 / \ / 4 5 8
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; node ( int data ) { this -> data = data ; this -> left = NULL ; this -> right = NULL ; } } ; void printKDistant ( node * root , int k ) { if ( root == NULL k < 0 ) return ; if ( k == 0 ) { cout << root -> data << " ▁ " ; return ; } printKDistant ( root -> left , k - 1 ) ; printKDistant ( root -> right , k - 1 ) ; } int main ( ) { node * root = new node ( 1 ) ; root -> left = new node ( 2 ) ; root -> right = new node ( 3 ) ; root -> left -> left = new node ( 4 ) ; root -> left -> right = new node ( 5 ) ; root -> right -> left = new node ( 8 ) ; printKDistant ( root , 2 ) ; return 0 ; }
Print nodes at k distance from root | Iterative | CPP program to print all nodes of level k iterative approach ; Node of binary tree ; Function to add a new node ; Function to print nodes of given level ; extra NULL is pushed to keep track of all the nodes to be pushed before level is incremented by 1 ; print when level is equal to k ; break the loop if level exceeds the given level number ; Driver code ; create a binary tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * newnode = new Node ( ) ; newnode -> data = data ; newnode -> left = newnode -> right = NULL ; } bool printKDistant ( Node * root , int klevel ) { queue < Node * > q ; int level = 1 ; bool flag = false ; q . push ( root ) ; q . push ( NULL ) ; while ( ! q . empty ( ) ) { Node * temp = q . front ( ) ; if ( level == klevel && temp != NULL ) { flag = true ; cout << temp -> data << " ▁ " ; } q . pop ( ) ; if ( temp == NULL ) { if ( q . front ( ) ) q . push ( NULL ) ; level += 1 ; if ( level > klevel ) break ; } else { if ( temp -> left ) q . push ( temp -> left ) ; if ( temp -> right ) q . push ( temp -> right ) ; } } cout << endl ; return flag ; } int main ( ) { Node * root = newNode ( 20 ) ; root -> left = newNode ( 10 ) ; root -> right = newNode ( 30 ) ; root -> left -> left = newNode ( 5 ) ; root -> left -> right = newNode ( 15 ) ; root -> left -> right -> left = newNode ( 12 ) ; root -> right -> left = newNode ( 25 ) ; root -> right -> right = newNode ( 40 ) ; cout << " data ▁ at ▁ level ▁ 1 ▁ : ▁ " ; int ret = printKDistant ( root , 1 ) ; if ( ret == false ) cout << " Number ▁ exceeds ▁ total ▁ number ▁ of ▁ levels STRNEWLINE " ; cout << " data ▁ at ▁ level ▁ 2 ▁ : ▁ " ; ret = printKDistant ( root , 2 ) ; if ( ret == false ) cout << " Number ▁ exceeds ▁ total ▁ number ▁ of ▁ levels STRNEWLINE " ; cout << " data ▁ at ▁ level ▁ 3 ▁ : ▁ " ; ret = printKDistant ( root , 3 ) ; if ( ret == false ) cout << " Number ▁ exceeds ▁ total ▁ number ▁ of ▁ levels STRNEWLINE " ; cout << " data ▁ at ▁ level ▁ 6 ▁ : ▁ " ; ret = printKDistant ( root , 6 ) ; if ( ret == false ) cout << " Number ▁ exceeds ▁ total ▁ number ▁ of ▁ levels STRNEWLINE " ; return 0 ; }
Print all leaf nodes of a Binary Tree from left to right | C ++ program to print leaf nodes from left to right ; A Binary Tree Node ; function to print leaf nodes from left to right ; if node is null , return ; if node is leaf node , print its data ; if left child exists , check for leaf recursively ; if right child exists , check for leaf recursively ; Utility function to create a new tree node ; Driver program to test above functions ; Let us create binary tree shown in above diagram ; print leaf nodes of the given tree
#include <iostream> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; void printLeafNodes ( Node * root ) { if ( ! root ) return ; if ( ! root -> left && ! root -> right ) { cout << root -> data << " ▁ " ; return ; } if ( root -> left ) printLeafNodes ( root -> left ) ; if ( root -> right ) printLeafNodes ( root -> right ) ; } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> right -> left = newNode ( 5 ) ; root -> right -> right = newNode ( 8 ) ; root -> right -> left -> left = newNode ( 6 ) ; root -> right -> left -> right = newNode ( 7 ) ; root -> right -> right -> left = newNode ( 9 ) ; root -> right -> right -> right = newNode ( 10 ) ; printLeafNodes ( root ) ; return 0 ; }
Print all nodes at distance k from a given node |  ; A binary Tree node ; Recursive function to print all the nodes at distance k in the tree ( or subtree ) rooted with given root . See ; Base Case ; If we reach a k distant node , print it ; Recur for left and right subtrees ; Prints all nodes at distance k from a given target node . The k distant nodes may be upward or downward . This function Returns distance of root from target node , it returns - 1 if target node is not present in tree rooted with root . ; Base Case 1 : If tree is empty , return - 1 ; If target is same as root . Use the downward function to print all nodes at distance k in subtree rooted with target or root ; Recur for left subtree ; Check if target node was found in left subtree ; If root is at distance k from target , print root Note that dl is Distance of root 's left child from target ; Else go to right subtree and print all k - dl - 2 distant nodes Note that the right child is 2 edges away from left child ; Add 1 to the distance and return value for parent calls ; MIRROR OF ABOVE CODE FOR RIGHT SUBTREE Note that we reach here only when node was not found in left subtree ; If target was neither present in left nor in right subtree ; A utility function to create a new binary tree node ; Driver program to test above functions ; Let us construct the tree shown in above diagram
#include <iostream> NEW_LINE using namespace std ; struct node { int data ; struct node * left , * right ; } ; void printkdistanceNodeDown ( node * root , int k ) { if ( root == NULL k < 0 ) return ; if ( k == 0 ) { cout << root -> data << endl ; return ; } printkdistanceNodeDown ( root -> left , k - 1 ) ; printkdistanceNodeDown ( root -> right , k - 1 ) ; } int printkdistanceNode ( node * root , node * target , int k ) { if ( root == NULL ) return -1 ; if ( root == target ) { printkdistanceNodeDown ( root , k ) ; return 0 ; } int dl = printkdistanceNode ( root -> left , target , k ) ; if ( dl != -1 ) { if ( dl + 1 == k ) cout << root -> data << endl ; else printkdistanceNodeDown ( root -> right , k - dl - 2 ) ; return 1 + dl ; } int dr = printkdistanceNode ( root -> right , target , k ) ; if ( dr != -1 ) { if ( dr + 1 == k ) cout << root -> data << endl ; else printkdistanceNodeDown ( root -> left , k - dr - 2 ) ; return 1 + dr ; } return -1 ; } node * newnode ( int data ) { node * temp = new node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { node * root = newnode ( 20 ) ; root -> left = newnode ( 8 ) ; root -> right = newnode ( 22 ) ; root -> left -> left = newnode ( 4 ) ; root -> left -> right = newnode ( 12 ) ; root -> left -> right -> left = newnode ( 10 ) ; root -> left -> right -> right = newnode ( 14 ) ; node * target = root -> left -> right ; printkdistanceNode ( root , target , 2 ) ; return 0 ; }
Print all nodes that don 't have sibling | Program to find singles in a given binary tree ; A Binary Tree Node ; Function to print all non - root nodes that don 't have a sibling ; Base case ; If this is an internal node , recur for left and right subtrees ; If left child is NULL and right is not , print right child and recur for right child ; If right child is NULL and left is not , print left child and recur for left child ; Driver program to test above functions ; Let us create binary tree given in the above example
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { struct node * left , * right ; int key ; } ; node * newNode ( int key ) { node * temp = new node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return temp ; } void printSingles ( struct node * root ) { if ( root == NULL ) return ; if ( root -> left != NULL && root -> right != NULL ) { printSingles ( root -> left ) ; printSingles ( root -> right ) ; } else if ( root -> right != NULL ) { cout << root -> right -> key << " ▁ " ; printSingles ( root -> right ) ; } else if ( root -> left != NULL ) { cout << root -> left -> key << " ▁ " ; printSingles ( root -> left ) ; } } int main ( ) { node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> right = newNode ( 4 ) ; root -> right -> left = newNode ( 5 ) ; root -> right -> left -> left = newNode ( 6 ) ; printSingles ( root ) ; return 0 ; }
Print all nodes that are at distance k from a leaf node | Program to print all nodes which are at distance k from a leaf ; utility that allocates a new Node with the given key ; This function prints all nodes that are distance k from a leaf node path [ ] -- > Store ancestors of a node visited [ ] -- > Stores true if a node is printed as output . A node may be k distance away from many leaves , we want to print it once ; Base case ; append this Node to the path array ; it 's a leaf, so print the ancestor at distance k only  if the ancestor is not already printed  ; If not leaf node , recur for left and right subtrees ; Given a binary tree and a number k , print all nodes that are k distant from a leaf ; Driver code ; Let us create binary tree given in the above example
#include <iostream> NEW_LINE using namespace std ; #define MAX_HEIGHT  10000 NEW_LINE struct Node { int key ; Node * left , * right ; } ; Node * newNode ( int key ) { Node * node = new Node ; node -> key = key ; node -> left = node -> right = NULL ; return ( node ) ; } void kDistantFromLeafUtil ( Node * node , int path [ ] , bool visited [ ] , int pathLen , int k ) { if ( node == NULL ) return ; path [ pathLen ] = node -> key ; visited [ pathLen ] = false ; pathLen ++ ; if ( node -> left == NULL && node -> right == NULL && pathLen - k - 1 >= 0 && visited [ pathLen - k - 1 ] == false ) { cout << path [ pathLen - k - 1 ] << " ▁ " ; visited [ pathLen - k - 1 ] = true ; return ; } kDistantFromLeafUtil ( node -> left , path , visited , pathLen , k ) ; kDistantFromLeafUtil ( node -> right , path , visited , pathLen , k ) ; } void printKDistantfromLeaf ( Node * node , int k ) { int path [ MAX_HEIGHT ] ; bool visited [ MAX_HEIGHT ] = { false } ; kDistantFromLeafUtil ( node , path , visited , 0 , k ) ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; root -> right -> left -> right = newNode ( 8 ) ; cout << " Nodes ▁ at ▁ distance ▁ 2 ▁ are : ▁ " ; printKDistantfromLeaf ( root , 2 ) ; return 0 ; }
Print all nodes that are at distance k from a leaf node | C ++ program to print all nodes at a distance k from leaf ; A binary tree node ; Given a binary tree and a number k , print all nodes that are k distant from a leaf ; leaf node ; parent of left leaf ; parent of right leaf ; Driver code ; Let us construct the tree shown in above diagram
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> data = key ; temp -> left = temp -> right = NULL ; return temp ; } int printKDistantfromLeaf ( struct Node * node , int k ) { if ( node == NULL ) return -1 ; int lk = printKDistantfromLeaf ( node -> left , k ) ; int rk = printKDistantfromLeaf ( node -> right , k ) ; bool isLeaf = lk == -1 && lk == rk ; if ( lk == 0 || rk == 0 || ( isLeaf && k == 0 ) ) cout << ( " ▁ " ) << ( node -> data ) ; if ( isLeaf && k > 0 ) return k - 1 ; if ( lk > 0 && lk < k ) return lk - 1 ; if ( rk > 0 && rk < k ) return rk - 1 ; return -2 ; } int main ( ) { Node * root = NULL ; root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; root -> right -> left -> right = newNode ( 8 ) ; cout << ( " ▁ Nodes ▁ at ▁ distance ▁ 2 ▁ are ▁ : " ) << endl ; printKDistantfromLeaf ( root , 2 ) ; }
Print leftmost and rightmost nodes of a Binary Tree | C / C ++ program to print corner node at each level of binary tree ; A binary tree node has key , pointer to left child and a pointer to right child ; Function to print corner node at each level ; If the root is null then simply return ; star node is for keeping track of levels ; pushing root node and star node ; Do level order traversal using a single queue ; n denotes the size of the current level in the queue ; If it is leftmost corner value or rightmost corner value then print it ; push the left and right children of the temp node ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; struct Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } void printCorner ( Node * root ) { if ( root == NULL ) return ; queue < Node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { int n = q . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Node * temp = q . front ( ) ; q . pop ( ) ; if ( i == 0 i == n - 1 ) cout << temp -> key << " ▁ " ; if ( temp -> left ) q . push ( temp -> left ) ; if ( temp -> right ) q . push ( temp -> right ) ; } } } int main ( ) { Node * root = newNode ( 15 ) ; root -> left = newNode ( 10 ) ; root -> right = newNode ( 20 ) ; root -> left -> left = newNode ( 8 ) ; root -> left -> right = newNode ( 12 ) ; root -> right -> left = newNode ( 16 ) ; root -> right -> right = newNode ( 25 ) ; printCorner ( root ) ; return 0 ; }
Print Binary Tree in 2 | C ++ Program to print binary tree in 2D ; A binary tree node ; Constructor that allocates a new node with the given data and NULL left and right pointers . ; Function to print binary tree in 2D It does reverse inorder traversal ; Base case ; Increase distance between levels ; Process right child first ; Print current node after space count ; Process left child ; Wrapper over print2DUtil ( ) ; Pass initial space count as 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define COUNT  10 NEW_LINE class Node { public : int data ; Node * left , * right ; Node ( int data ) { this -> data = data ; this -> left = NULL ; this -> right = NULL ; } } ; void print2DUtil ( Node * root , int space ) { if ( root == NULL ) return ; space += COUNT ; print2DUtil ( root -> right , space ) ; cout << endl ; for ( int i = COUNT ; i < space ; i ++ ) cout << " ▁ " ; cout << root -> data << " STRNEWLINE " ; print2DUtil ( root -> left , space ) ; } void print2D ( Node * root ) { print2DUtil ( root , 0 ) ; } int main ( ) { Node * root = new Node ( 1 ) ; root -> left = new Node ( 2 ) ; root -> right = new Node ( 3 ) ; root -> left -> left = new Node ( 4 ) ; root -> left -> right = new Node ( 5 ) ; root -> right -> left = new Node ( 6 ) ; root -> right -> right = new Node ( 7 ) ; root -> left -> left -> left = new Node ( 8 ) ; root -> left -> left -> right = new Node ( 9 ) ; root -> left -> right -> left = new Node ( 10 ) ; root -> left -> right -> right = new Node ( 11 ) ; root -> right -> left -> left = new Node ( 12 ) ; root -> right -> left -> right = new Node ( 13 ) ; root -> right -> right -> left = new Node ( 14 ) ; root -> right -> right -> right = new Node ( 15 ) ; print2D ( root ) ; return 0 ; }
Print Left View of a Binary Tree | C ++ program to print left view of Binary Tree ; A utility function to create a new Binary Tree Node ; Recursive function to print left view of a binary tree . ; Base Case ; If this is the first Node of its level ; Recur for left subtree first , then right subtree ; A wrapper over leftViewUtil ( ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int item ) { struct Node * temp = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; temp -> data = item ; temp -> left = temp -> right = NULL ; return temp ; } void leftViewUtil ( struct Node * root , int level , int * max_level ) { if ( root == NULL ) return ; if ( * max_level < level ) { cout << root -> data << " ▁ " ; * max_level = level ; } leftViewUtil ( root -> left , level + 1 , max_level ) ; leftViewUtil ( root -> right , level + 1 , max_level ) ; } void leftView ( struct Node * root ) { int max_level = 0 ; leftViewUtil ( root , 1 , & max_level ) ; } int main ( ) { Node * root = newNode ( 10 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 7 ) ; root -> left -> right = newNode ( 8 ) ; root -> right -> right = newNode ( 15 ) ; root -> right -> left = newNode ( 12 ) ; root -> right -> right -> left = newNode ( 14 ) ; leftView ( root ) ; return 0 ; }
Print Left View of a Binary Tree | C ++ program to print left view of Binary Tree ; A Binary Tree Node ; Utility function to create a new tree node ; function to print left view of binary tree ; number of nodes at current level ; Traverse all nodes of current level ; Print the left most element at the level ; Add left node to queue ; Add right node to queue ; Driver code ; Let 's construct the tree as  shown in example
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } void printLeftView ( Node * root ) { if ( ! root ) return ; queue < Node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { int n = q . size ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { Node * temp = q . front ( ) ; q . pop ( ) ; if ( i == 1 ) cout << temp -> data << " ▁ " ; if ( temp -> left != NULL ) q . push ( temp -> left ) ; if ( temp -> right != NULL ) q . push ( temp -> right ) ; } } } int main ( ) { Node * root = newNode ( 10 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 7 ) ; root -> left -> right = newNode ( 8 ) ; root -> right -> right = newNode ( 15 ) ; root -> right -> left = newNode ( 12 ) ; root -> right -> right -> left = newNode ( 14 ) ; printLeftView ( root ) ; }
Reduce the given Array of [ 1 , N ] by rotating left or right based on given conditions | C ++ program for the above approach ; Function to find the last element left after performing N - 1 queries of type X ; Stores the next power of 2 ; Iterate until nextPower is at most N ; If X is equal to 1 ; Stores the power of 2 less than or equal to N ; Return the final value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int rotate ( int arr [ ] , int N , int X ) { long long int nextPower = 1 ; while ( nextPower <= N ) nextPower *= 2 ; if ( X == 1 ) return nextPower - N ; long long int prevPower = nextPower / 2 ; return 2 * ( N - prevPower ) + 1 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int X = 1 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << rotate ( arr , N , X ) ; return 0 ; }
Check whether Matrix T is a result of one or more 90 ° rotations of Matrix mat | C ++ program for the above approach ; Function to check whether another matrix can be created by rotating mat one or more times by 90 degrees ; If the dimensions of both the arrays don 't match ; Return false ; Map to store all rows , columns and their reversed versions ; Iterate in the range [ 0 , M - 1 ] ; Increment the frequency of the i 'th row by 1 ; Reverse the i 'th row ; Increment the frequency of the i 'th row by 1 ; Iterate in the range [ 0 , N - 1 ] ; Stores the i 'th column ; Iterate in the range [ 0 , M - 1 ] ; Increment the frequency of the i 'th column by 1 ; Reverse the i 'th column ; Increment the frequency of the i 'th column by 1 ; Iterate in the range [ 0 , M - 1 ] ; If frequency of the i 'th row  is more in T[][] than in the  mat[][]. ; Decrement the frequency of the i 'th row by 1 ; Iterate in the range [ 0 , N - 1 ] ; Stores the ith column ; Iterate in the range [ 0 , M - 1 ] ; If frequency of the i 'th column  is more in T[][] than in mat[][]. ; Decrement the frequency of the i 'th  column by 1 ; Return " Yes " ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findRotation ( vector < vector < int > > & mat , vector < vector < int > > & T ) { if ( T . size ( ) != mat . size ( ) || T [ 0 ] . size ( ) != mat [ 0 ] . size ( ) ) { return " No " ; } map < vector < int > , int > m ; for ( int i = 0 ; i < mat . size ( ) ; i ++ ) { m [ mat [ i ] ] += 1 ; reverse ( mat [ i ] . begin ( ) , mat [ i ] . end ( ) ) ; m [ mat [ i ] ] += 1 ; } for ( int i = 0 ; i < mat [ 0 ] . size ( ) ; i ++ ) { vector < int > r = { } ; for ( int j = 0 ; j < mat . size ( ) ; j ++ ) { r . push_back ( mat [ j ] [ i ] ) ; } m [ r ] += 1 ; reverse ( r . begin ( ) , r . end ( ) ) ; m [ r ] += 1 ; } for ( int i = 0 ; i < T . size ( ) ; i ++ ) { if ( m [ T [ i ] ] <= 0 ) { return " No " ; } m [ T [ i ] ] -= 1 ; } for ( int i = 0 ; i < T [ 0 ] . size ( ) ; i ++ ) { vector < int > r = { } ; for ( int j = 0 ; j < T . size ( ) ; j ++ ) { r . push_back ( T [ j ] [ i ] ) ; } if ( m [ r ] <= 0 ) { return " No " ; } m [ r ] -= 1 ; } return " Yes " ; } int main ( ) { vector < vector < int > > mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; vector < vector < int > > T = { { 3 , 6 , 9 } , { 2 , 5 , 8 } , { 1 , 4 , 7 } } ; cout << findRotation ( mat , T ) ; return 0 ; }
Maximum number of 0 s placed consecutively at the start and end in any rotation of a Binary String | C ++ program for the above approach ; Function to find the maximum sum of consecutive 0 s present at the start and end of a string present in any of the rotations of the given string ; Check if all the characters in the string are 0 ; Iterate over characters of the string ; If the frequency of '1' is 0 ; Print n as the result ; Concatenate the string with itself ; Stores the required result ; Generate all rotations of the string ; Store the number of consecutive 0 s at the start and end of the string ; Count 0 s present at the start ; Count 0 s present at the end ; Calculate the sum ; Update the overall maximum sum ; Print the result ; Driver Code ; Given string ; Store the size of the string
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaximumZeros ( string str , int n ) { int c0 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str [ i ] == '0' ) c0 ++ ; } if ( c0 == n ) { cout << n ; return ; } string s = str + str ; int mx = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int cs = 0 ; int ce = 0 ; for ( int j = i ; j < i + n ; ++ j ) { if ( s [ j ] == '0' ) cs ++ ; else break ; } for ( int j = i + n - 1 ; j >= i ; -- j ) { if ( s [ j ] == '0' ) ce ++ ; else break ; } int val = cs + ce ; mx = max ( val , mx ) ; } cout << mx ; } int main ( ) { string s = "1001" ; int n = s . size ( ) ; findMaximumZeros ( s , n ) ; return 0 ; }