codeparrot/xlcost-text-to-code huggingface dataset:
Check if a Binary Tree contains duplicate subtrees of size 2 or more | C ++ program to find if there is a duplicate sub - tree of size 2 or more . ; Structure for a binary tree node ; A utility function to create a new node ; This function returns empty string if tree contains a duplicate subtree of size 2 or more . ; If current node is NULL , return marker ; If left subtree has a duplicate subtree . ; Do same for right subtree ; Serialize current subtree ; If current subtree already exists in hash table . [ Note that size of a serialized tree with single node is 3 as it has two marker nodes . ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; const char MARKER = ' $ ' ; struct Node { char key ; Node * left , * right ; } ; Node * newNode ( char key ) { Node * node = new Node ; node -> key = key ; node -> left = node -> right = NULL ; return node ; } unordered_set < string > subtrees ; string dupSubUtil ( Node * root ) { string s = " " ; if ( root == NULL ) return s + MARKER ; string lStr = dupSubUtil ( root -> left ) ; if ( lStr . compare ( s ) == 0 ) return s ; string rStr = dupSubUtil ( root -> right ) ; if ( rStr . compare ( s ) == 0 ) return s ; s = s + root -> key + lStr + rStr ; if ( s . length ( ) > 3 && subtrees . find ( s ) != subtrees . end ( ) ) return " " ; subtrees . insert ( s ) ; return s ; } int main ( ) { Node * root = newNode ( ' A ' ) ; root -> left = newNode ( ' B ' ) ; root -> right = newNode ( ' C ' ) ; root -> left -> left = newNode ( ' D ' ) ; root -> left -> right = newNode ( ' E ' ) ; root -> right -> right = newNode ( ' B ' ) ; root -> right -> right -> right = newNode ( ' E ' ) ; root -> right -> right -> left = newNode ( ' D ' ) ; string str = dupSubUtil ( root ) ; ( str . compare ( " " ) == 0 ) ? cout << " ▁ Yes ▁ " : cout << " ▁ No ▁ " ; return 0 ; }
Count pairs of similar rectangles possible from a given array | C ++ Program for the hashmap Approach ; Get the count of all pairs of similar rectangles ; Initialize the result value and map to store the ratio to the rectangles ; Calculate the rectangular ratio and save them ; Calculate pairs of similar rectangles from its common ratio ; Driver code
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE using namespace std ; int getCount ( int rows , int columns , int sides [ ] [ 2 ] ) { int ans = 0 ; unordered_map < double , int > umap ; for ( int i = 0 ; i < rows ; i ++ ) { double ratio = ( double ) sides [ i ] [ 0 ] / sides [ i ] [ 1 ] ; umap [ ratio ] ++ ; } for ( auto x : umap ) { int value = x . second ; if ( value > 1 ) { ans += ( value * ( value - 1 ) ) / 2 ; } } return ans ; } int main ( ) { int sides [ 4 ] [ 2 ] = { { 4 , 8 } , { 10 , 20 } , { 15 , 30 } , { 3 , 6 } } ; int rows = 4 ; int columns = 2 ; cout << getCount ( rows , columns , sides ) ; return 0 ; }
Non | Simple CPP program to find first non - repeating element . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstNonRepeating ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && arr [ i ] == arr [ j ] ) break ; if ( j == n ) return arr [ i ] ; } return -1 ; } int main ( ) { int arr [ ] = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << firstNonRepeating ( arr , n ) ; return 0 ; }
Non | Efficient CPP program to find first non - repeating element . ; Insert all array elements in hash table ; Traverse array again and return first element with count 1. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstNonRepeating ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; for ( int i = 0 ; i < n ; i ++ ) if ( mp [ arr [ i ] ] == 1 ) return arr [ i ] ; return -1 ; } int main ( ) { int arr [ ] = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << firstNonRepeating ( arr , n ) ; return 0 ; }
Non | Efficient CPP program to print all non - repeating elements . ; Insert all array elements in hash table ; Traverse through map only and ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void firstNonRepeating ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; for ( auto x : mp ) if ( x . second == 1 ) cout << x . first << " ▁ " ; } int main ( ) { int arr [ ] = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; firstNonRepeating ( arr , n ) ; return 0 ; }
Minimum substring removals required to make all remaining characters of a string same | C ++ program to implement the above approach ; Function to count minimum operations required to make all characters equal by repeatedly removing substring ; Remove consecutive duplicate characters from str ; Stores length of the string ; Stores frequency of each distinct characters of the string str ; Iterate over all the characters of the string str ; Update frequency of str [ i ] ; Decrementing the frequency of the string str [ 0 ] ; Decrementing the frequency of the string str [ N - 1 ] ; Stores the required count ; Iterate over all characters of the string str ; Update ans ; Driver Code ; Given string
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperationNeeded ( string str ) { str = string ( str . begin ( ) , unique ( str . begin ( ) , str . end ( ) ) ) ; int N = str . length ( ) ; int res [ 256 ] = { 0 } ; for ( int i = 0 ; i < N ; ++ i ) { res [ str [ i ] ] += 1 ; } res [ str [ 0 ] ] -= 1 ; res [ str [ N - 1 ] ] -= 1 ; int ans = INT_MAX ; for ( int i = 0 ; i < N ; ++ i ) { ans = min ( ans , res [ str [ i ] ] ) ; } cout << ( ans + 1 ) << endl ; } int main ( ) { string str = " ABCDABCDABCDA " ; minOperationNeeded ( str ) ; return 0 ; }
Print all root to leaf paths of an N | C ++ program for the above approach ; Structure of an N ary tree node ; Parameterized Constructor ; Function to print the root to leaf path of the given N - ary Tree ; Print elements in the vector ; Utility function to print all root to leaf paths of an Nary Tree ; If root is null ; Insert current node 's  data into the vector ; If current node is a leaf node ; Print the path ; Pop the leaf node and return ; Recur for all children of the current node ; Recursive Function Call ; Function to print root to leaf path ; If root is null , return ; Stores the root to leaf path ; Utility function call ; Driver Code ; Given N - Ary tree ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; vector < Node * > child ; Node ( int x ) : data ( x ) { } } ; void printPath ( vector < int > vec ) { for ( int ele : vec ) { cout << ele << " ▁ " ; } cout << endl ; } void printAllRootToLeafPaths ( Node * root , vector < int > vec ) { if ( ! root ) return ; vec . push_back ( root -> data ) ; if ( root -> child . empty ( ) ) { printPath ( vec ) ; vec . pop_back ( ) ; return ; } for ( int i = 0 ; i < root -> child . size ( ) ; i ++ ) printAllRootToLeafPaths ( root -> child [ i ] , vec ) ; } void printAllRootToLeafPaths ( Node * root ) { if ( ! root ) return ; vector < int > vec ; printAllRootToLeafPaths ( root , vec ) ; } int main ( ) { Node * root = new Node ( 1 ) ; ( root -> child ) . push_back ( new Node ( 2 ) ) ; ( root -> child ) . push_back ( new Node ( 3 ) ) ; ( root -> child [ 0 ] -> child ) . push_back ( new Node ( 4 ) ) ; ( root -> child [ 1 ] -> child ) . push_back ( new Node ( 5 ) ) ; ( root -> child [ 1 ] -> child ) . push_back ( new Node ( 6 ) ) ; ( root -> child [ 1 ] -> child [ 1 ] -> child ) . push_back ( new Node ( 7 ) ) ; ( root -> child [ 1 ] -> child [ 1 ] -> child ) . push_back ( new Node ( 8 ) ) ; printAllRootToLeafPaths ( root ) ; return 0 ; }
Minimum removals to make a string concatenation of a substring of 0 s followed by a substring of 1 s | C ++ program for the above approach ; Function to count minimum removals required to make a given string concatenation of substring of 0 s followed by substring of 1 s ; Stores the length of the string ; Precompute the count of 0 s ; Check for the last character ; Traverse and update zeroCount array ; If current character is 0 , ; Update aCount [ i ] as aCount [ i + 1 ] + 1 ; Update as aCount [ i + 1 ] ; Keeps track of deleted 1 s ; Stores the count of removals ; Traverse the array ; If current character is 1 ; Update ans ; If all 1 s are deleted ; Return the minimum number of deletions ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int minimumDeletions ( string s ) { int n = s . size ( ) ; int zeroCount [ n ] ; zeroCount [ n - 1 ] = ( s [ n - 1 ] == '0' ) ? 1 : 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) zeroCount [ i ] = ( s [ i ] == '0' ) ? zeroCount [ i + 1 ] + 1 : zeroCount [ i + 1 ] ; int oneCount = 0 ; int ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { ans = min ( ans , oneCount + zeroCount [ i ] ) ; oneCount ++ ; } } ans = min ( ans , oneCount ) ; return ( ans == INT_MAX ) ? 0 : ans ; } int main ( ) { string stri = "00101101" ; cout << minimumDeletions ( stri ) << endl ; return 0 ; }
Pairs of Positive Negative values in an array | Simple CPP program to find pairs of positive and negative values present in an array . ; Print pair with negative and positive value ; For each element of array . ; Try to find the negative value of arr [ i ] from i + 1 to n ; If absolute values are equal print pair . ; If size of vector is 0 , therefore there is no element with positive negative value , print "0" ; Sort the vector ; Print the pair with negative positive value . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPairs ( int arr [ ] , int n ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( arr [ i ] ) == abs ( arr [ j ] ) ) v . push_back ( abs ( arr [ i ] ) ) ; if ( v . size ( ) == 0 ) return ; sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) cout << - v [ i ] << " ▁ " << v [ i ] ; } int main ( ) { int arr [ ] = { 4 , 8 , 9 , -4 , 1 , -1 , -8 , -9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPairs ( arr , n ) ; return 0 ; }
Check if an array can be divided into pairs whose sum is divisible by k | A C ++ program to check if arr [ 0. . n - 1 ] can be divided in pairs such that every pair is divisible by k . ; Returns true if arr [ 0. . n - 1 ] can be divided into pairs with sum divisible by k . ; An odd length array cannot be divided into pairs ; Create a frequency array to count occurrences of all remainders when divided by k . ; Count occurrences of all remainders ; Traverse input array and use freq [ ] to decide if given array can be divided in pairs ; Remainder of current element ; If remainder with current element divides k into two halves . ; Then there must be even occurrences of such remainder ; If remainder is 0 , then there must be two elements with 0 remainder ; Then there must be even occurrences of such remainder ; Else number of occurrences of remainder must be equal to number of occurrences of k - remainder ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canPairs ( int arr [ ] , int n , int k ) { if ( n & 1 ) return false ; unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ ( ( arr [ i ] % k ) + k ) % k ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { int rem = ( ( arr [ i ] % k ) + k ) % k ; if ( 2 * rem == k ) { if ( freq [ rem ] % 2 != 0 ) return false ; } else if ( rem == 0 ) { if ( freq [ rem ] & 1 ) return false ; } else if ( freq [ rem ] != freq [ k - rem ] ) return false ; } return true ; } int main ( ) { int arr [ ] = { 92 , 75 , 65 , 48 , 45 , 35 } ; int k = 10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; canPairs ( arr , n , k ) ? cout << " True " : cout << " False " ; return 0 ; }
Subarray with no pair sum divisible by K | CPP code to find the subarray with no pair sum divisible by K ; function to find the subarray with no pair sum divisible by k ; hash table to store the remainders obtained on dividing by K ; s : starting index of the current subarray , e : ending index of the current subarray , maxs : starting index of the maximum size subarray so far , maxe : ending index of the maximum size subarray so far ; insert the first element in the set ; Removing starting elements of current subarray while there is an element in set which makes a pair with mod [ i ] such that the pair sum is divisible . ; include the current element in the current subarray the ending index of the current subarray increments by one ; compare the size of the current subarray with the maximum size so far ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void subarrayDivisibleByK ( int arr [ ] , int n , int k ) { map < int , int > mp ; int s = 0 , e = 0 , maxs = 0 , maxe = 0 ; mp [ arr [ 0 ] % k ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { int mod = arr [ i ] % k ; while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) { mp [ arr [ s ] % k ] -- ; s ++ ; } mp [ mod ] ++ ; e ++ ; if ( ( e - s ) > ( maxe - maxs ) ) { maxe = e ; maxs = s ; } } cout << " The ▁ maximum ▁ size ▁ is ▁ " << maxe - maxs + 1 << " ▁ and ▁ " " the ▁ subarray ▁ is ▁ as ▁ follows STRNEWLINE " ; for ( int i = maxs ; i <= maxe ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int k = 3 ; int arr [ ] = { 5 , 10 , 15 , 20 , 25 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; subarrayDivisibleByK ( arr , n , k ) ; return 0 ; }
Find three element from different three arrays such that a + b + c = sum | C ++ program to find three element from different three arrays such that a + b + c is equal to given sum ; Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findTriplet ( int a1 [ ] , int a2 [ ] , int a3 [ ] , int n1 , int n2 , int n3 , int sum ) { for ( int i = 0 ; i < n1 ; i ++ ) for ( int j = 0 ; j < n2 ; j ++ ) for ( int k = 0 ; k < n3 ; k ++ ) if ( a1 [ i ] + a2 [ j ] + a3 [ k ] == sum ) return true ; return false ; } int main ( ) { int a1 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int a2 [ ] = { 2 , 3 , 6 , 1 , 2 } ; int a3 [ ] = { 3 , 2 , 4 , 5 , 6 } ; int sum = 9 ; int n1 = sizeof ( a1 ) / sizeof ( a1 [ 0 ] ) ; int n2 = sizeof ( a2 ) / sizeof ( a2 [ 0 ] ) ; int n3 = sizeof ( a3 ) / sizeof ( a3 [ 0 ] ) ; findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Find three element from different three arrays such that a + b + c = sum | C ++ program to find three element from different three arrays such that a + b + c is equal to given sum ; Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Store elements of first array in hash ; sum last two arrays element one by one ; Consider current pair and find if there is an element in a1 [ ] such that these three form a required triplet ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findTriplet ( int a1 [ ] , int a2 [ ] , int a3 [ ] , int n1 , int n2 , int n3 , int sum ) { unordered_set < int > s ; for ( int i = 0 ; i < n1 ; i ++ ) s . insert ( a1 [ i ] ) ; for ( int i = 0 ; i < n2 ; i ++ ) { for ( int j = 0 ; j < n3 ; j ++ ) { if ( s . find ( sum - a2 [ i ] - a3 [ j ] ) != s . end ( ) ) return true ; } } return false ; } int main ( ) { int a1 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int a2 [ ] = { 2 , 3 , 6 , 1 , 2 } ; int a3 [ ] = { 3 , 2 , 4 , 5 , 6 } ; int sum = 9 ; int n1 = sizeof ( a1 ) / sizeof ( a1 [ 0 ] ) ; int n2 = sizeof ( a2 ) / sizeof ( a2 [ 0 ] ) ; int n3 = sizeof ( a3 ) / sizeof ( a3 [ 0 ] ) ; findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Count quadruplets with sum K from given array | C ++ program for the above approach ; Function to return the number of quadruplets having given sum ; Initialize variables ; Initialize answer ; All possible first elements ; All possible second element ; Use map to find the fourth element ; All possible third elements ; Calculate number of valid 4 th elements ; Update the twice_count ; Unordered pairs ; Return answer ; Driver Code ; Given array arr [ ] ; Given sum S ; Function Call
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE using namespace std ; int countSum ( int a [ ] , int n , int sum ) { int i , j , k , l ; int count = 0 ; for ( i = 0 ; i < n - 3 ; i ++ ) { for ( j = i + 1 ; j < n - 2 ; j ++ ) { int req = sum - a [ i ] - a [ j ] ; unordered_map < int , int > m ; for ( k = j + 1 ; k < n ; k ++ ) m [ a [ k ] ] ++ ; int twice_count = 0 ; for ( k = j + 1 ; k < n ; k ++ ) { twice_count += m [ req - a [ k ] ] ; if ( req - a [ k ] == a [ k ] ) twice_count -- ; } count += twice_count / 2 ; } } return count ; } int main ( ) { int arr [ ] = { 4 , 5 , 3 , 1 , 2 , 4 } ; int S = 13 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSum ( arr , N , S ) ; return 0 ; }
Find four elements a , b , c and d in an array such that a + b = c + d | Find four different elements a , b , c and d of array such that a + b = c + d ; function to find a , b , c , d such that ( a + b ) = ( c + d ) ; Create an empty Hash to store mapping from sum to pair indexes ; Traverse through all possible pairs of arr [ ] ; If sum of current pair is not in hash , then store it and continue to next pair ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findPairs ( int arr [ ] , int n ) { map < int , pair < int , int > > Hash ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int sum = arr [ i ] + arr [ j ] ; if ( Hash . find ( sum ) == Hash . end ( ) ) Hash [ sum ] = make_pair ( i , j ) ; else { pair < int , int > pp = Hash [ sum ] ; cout << " ( " << arr [ pp . first ] << " , ▁ " << arr [ pp . second ] << " ) ▁ and ▁ ( " << arr [ i ] << " , ▁ " << arr [ j ] << " ) n " ; return true ; } } } cout << " No ▁ pairs ▁ found " ; return false ; } int main ( ) { int arr [ ] = { 3 , 4 , 7 , 1 , 2 , 9 , 8 } ; int n = sizeof arr / sizeof arr [ 0 ] ; findPairs ( arr , n ) ; return 0 ; }
Check if all subarrays contains at least one unique element | C ++ program for the above approach ; Function to check if all subarrays have at least one unique element ; Generate all subarray ; Store frequency of subarray 's elements ; Traverse the array over the range [ i , N ] ; Update frequency of current subarray in map ; Increment count ; Decrement count ; If all subarrays have at least 1 unique element ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string check ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { map < int , int > hm ; int count = 0 ; for ( int j = i ; j < n ; j ++ ) { hm [ arr [ j ] ] ++ ; if ( hm [ arr [ j ] ] == 1 ) count ++ ; if ( hm [ arr [ j ] ] == 2 ) count -- ; if ( count == 0 ) return " No " ; } } return " Yes " ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << check ( arr , N ) ; }
Count all disjoint pairs having absolute difference at least K from a given array | C ++ program for the above approach ; Function to count distinct pairs with absolute difference atleast K ; Track the element that have been paired ; Stores count of distinct pairs ; Pick all elements one by one ; If already visited ; If already visited ; If difference is at least K ; Mark element as visited and increment the count ; Print the final count ; Driver Code ; Given arr [ ] ; Size of array ; Given difference K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairsWithDiffK ( int arr [ ] , int N , int K ) { int vis [ N ] ; memset ( vis , 0 , sizeof ( vis ) ) ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( vis [ i ] == 1 ) continue ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( vis [ j ] == 1 ) continue ; if ( abs ( arr [ i ] - arr [ j ] ) >= K ) { count ++ ; vis [ i ] = 1 ; vis [ j ] = 1 ; break ; } } } cout << count << ' ▁ ' ; } int main ( ) { int arr [ ] = { 1 , 3 , 3 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; countPairsWithDiffK ( arr , N , K ) ; return 0 ; }
Check if two trees are Mirror | C ++ program to check if two trees are mirror of each other ; A binary tree node has data , pointer to left child and a pointer to right child ; Given two trees , return true if they are mirror of each other ; Base case : Both empty ; If only one is empty ; Both non - empty , compare them recursively Note that in recursive calls , we pass left of one tree and right of other tree ; Helper function that allocates a new node ; Driver program to test areMirror ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; int areMirror ( Node * a , Node * b ) { if ( a == NULL && b == NULL ) return true ; if ( a == NULL b == NULL ) return false ; return a -> data == b -> data && areMirror ( a -> left , b -> right ) && areMirror ( a -> right , b -> left ) ; } Node * newNode ( int data ) { Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int main ( ) { Node * a = newNode ( 1 ) ; Node * b = newNode ( 1 ) ; a -> left = newNode ( 2 ) ; a -> right = newNode ( 3 ) ; a -> left -> left = newNode ( 4 ) ; a -> left -> right = newNode ( 5 ) ; b -> left = newNode ( 3 ) ; b -> right = newNode ( 2 ) ; b -> right -> left = newNode ( 5 ) ; b -> right -> right = newNode ( 4 ) ; areMirror ( a , b ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Find the length of largest subarray with 0 sum | C ++ program to find the length of largest subarray with 0 sum ; Returns Length of the required subarray ; Map to store the previous sums ; Initialize the sum of elements ; Initialize result ; Traverse through the given array ; Add current element to sum ; Look for this sum in Hash table ; Else insert this sum with index in hash table ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLen ( int arr [ ] , int n ) { unordered_map < int , int > presum ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ; if ( sum == 0 ) max_len = i + 1 ; if ( presum . find ( sum ) != presum . end ( ) ) { max_len = max ( max_len , i - presum [ sum ] ) ; } else { presum [ sum ] = i ; } } return max_len ; } int main ( ) { int arr [ ] = { 15 , -2 , 2 , -8 , 1 , 7 , 10 , 23 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Length ▁ of ▁ the ▁ longest ▁ 0 ▁ sum ▁ subarray ▁ is ▁ " << maxLen ( arr , n ) ; return 0 ; }
Print alternate elements of an array | C ++ program to implement the above approach ; Function to print Alternate elements of the given array ; Print elements at odd positions ; Print elements of array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAlter ( int arr [ ] , int N ) { for ( int currIndex = 0 ; currIndex < N ; currIndex += 2 ) { cout << arr [ currIndex ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printAlter ( arr , N ) ; }