codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Manhattan distances between repetitions in a String | Java program for the above approach ; Function to find the sum of the manhattan distances between same characters in string ; Vector to store indices for each unique character of the string ; Append the position of each character in their respective vectors ; Store the required result ; Iterate over all the characters ; Calculate sum of all elements present in the vector ; Traverse the current vector ; Find suffix [ i + 1 ] ; Adding distance of all pairs whose first element is i and second element belongs to [ i + 1 , n ] ; Print the result ; Driver code ; Given Input ; Function Call
import java . lang . * ; import java . util . * ; class GFG { static void SumofDistances ( String s ) { ArrayList < ArrayList < Integer > > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) v . add ( new ArrayList < > ( ) ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { v . get ( s . charAt ( i ) - ' a ' ) . add ( i ) ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < v . get ( i ) . size ( ) ; j ++ ) { sum += v . get ( i ) . get ( j ) ; } for ( int j = 0 ; j < v . get ( i ) . size ( ) ; j ++ ) { sum -= v . get ( i ) . get ( j ) ; ans += ( sum - ( v . get ( i ) . size ( ) - 1 - j ) * ( v . get ( i ) . get ( j ) ) ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String s = " ababa " ; SumofDistances ( s ) ; } }
Minimum prefixes required to be flipped to convert a Binary String to another | Java program for the above approach ; Function to count flips required to make Strings A and B equal ; Stores the count of the number of operations ; Stores the length of the chosen prefixes ; Stores if operations are performed even or odd times ; Traverse the given String ; If current characters in the two Strings are unequal ; If A [ i ] is not flipped ; Increment count of operations ; Insert the length of the chosen prefix ; Update invert to true ; If A [ i ] is flipped ; Increment count of operations ; Insert length of the chosen prefix ; Update invert to false ; Print the number of operations required ; Print the chosen prefix length in each operation ; Driver Code ; Given binary Strings
import java . util . * ; class GFG { static void findOperations ( String A , String B , int N ) { int operations = 0 ; Vector < Integer > ops = new Vector < > ( ) ; boolean invert = false ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A . charAt ( i ) != B . charAt ( i ) ) { if ( ! invert ) { operations ++ ; ops . add ( i + 1 ) ; invert = true ; } } else { if ( invert ) { operations ++ ; ops . add ( i + 1 ) ; invert = false ; } } } System . out . print ( operations + "NEW_LINE"); if ( operations != 0 ) { for ( int x : ops ) System . out . print ( x + " ▁ " ) ; } } public static void main ( String [ ] args ) { String A = "001" , B = "000" ; int N = A . length ( ) ; findOperations ( A , B , N ) ; } }
Maximize cost of forming a set of words using given set of characters | Java program for the above approach ; Function to find the maximum cost of any valid set of words formed by using the given letters ; Stores frequency of characters ; Find the maximum cost ; Utility function to find maximum cost of generating any possible subsets of strings ; Base Case ; Stores the current cost ; Stores the cost of by the current word ; Create a copy of the letterCounts array ; Traverse the current word ; Store the current index & check if its frequency is 0 or not ; If true , then update wordScore to - 1 ; Otherwise , add the cost of the current index to wordScore ; Decrease its frequency ; If wordScore > 0 , then recursively call for next index ; Find the cost by not including current word ; Return the maximum score ; Driver Code ; Given arrays ; Function Call
import java . io . * ; class GFG { public static int maxScoreWords ( String [ ] words , char [ ] letters , int [ ] score ) { int [ ] letterCounts = new int [ 26 ] ; for ( char letter : letters ) letterCounts [ letter - ' a ' ] ++ ; return helper ( words , 0 , letterCounts , score ) ; } public static int helper ( String [ ] words , int start , int [ ] letterCounts , int [ ] score ) { if ( start == words . length ) return 0 ; int currScore = 0 ; int wordScore = 0 ; int [ ] nextCounts = letterCounts . clone ( ) ; for ( int i = 0 ; i < words [ start ] . length ( ) ; ++ i ) { int idx = words [ start ] . charAt ( i ) - ' a ' ; if ( nextCounts [ idx ] == 0 ) { wordScore = - 1 ; break ; } wordScore += score [ idx ] ; nextCounts [ idx ] -- ; } if ( wordScore > 0 ) currScore = helper ( words , start + 1 , nextCounts , score ) + wordScore ; currScore = Math . max ( currScore , helper ( words , start + 1 , letterCounts , score ) ) ; return currScore ; } public static void main ( String [ ] args ) { String words [ ] = { " dog " , " cat " , " dad " , " good " } ; char letters [ ] = { ' a ' , ' a ' , ' c ' , ' d ' , ' d ' , ' d ' , ' g ' , ' o ' , ' o ' } ; int score [ ] = { 1 , 0 , 9 , 5 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ; System . out . println ( maxScoreWords ( words , letters , score ) ) ; } }
Modify string by sorting characters after removal of characters whose frequency is not equal to power of 2 | Java program for the above approach ; Function to remove all the characters from a string that whose frequencies are not equal to a perfect power of 2 ; Stores the frequency of each character in S ; Iterate over characters of string ; Update frequency of current character in the array freq [ ] ; Traverse the array freq [ ] ; Check if the i - th letter is absent from string S ; Calculate log of frequency of the current character in the string S ; Calculate power of 2 of lg ; Check if freq [ i ] is a power of 2 ; Print letter i + ' a ' freq [ i ] times ; Driver Code
class GFG { static void countFrequency ( String S , int N ) { int [ ] freq = new int [ 26 ] ; Array . Clear ( freq , 0 , freq . Length ) ; for ( int i = 0 ; i < N ; i ++ ) { freq [ ( int ) S . charAt ( i ) - ' a ' ] += 1 ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; int lg = ( int ) ( Math . log ( freq [ i ] ) / Math . log ( 2 ) ) ; int a = ( int ) Math . pow ( 2 , lg ) ; if ( a == freq [ i ] ) { while ( freq [ i ] > 0 ) { freq [ i ] -= 1 ; System . out . print ( ( char ) ( i + ' a ' ) ) ; } } } } public static void main ( String args [ ] ) { String S = " aaacbb " ; int N = S . length ( ) ; countFrequency ( S , N ) ; } }
Longest Common Subsequence ( LCS ) by repeatedly swapping characters of a string with characters of another string | Java program for the above approach ; Function to find the length of LCS possible by swapping any character of a string with that of another string ; Store the size of the strings ; Stores frequency of characters ; Iterate over characters of the string A ; Update frequency of character A [ i ] ; Iterate over characters of the string B ; Update frequency of character B [ i ] ; Store the count of all pairs of similar characters ; Traverse the array freq [ ] ; Update cnt ; Print the minimum of cnt , N and M ; Driver Code ; Given strings
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void lcsBySwapping ( String A , String B ) { int N = A . length ( ) ; int M = B . length ( ) ; int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ A . charAt ( i ) - ' a ' ] += 1 ; } for ( int i = 0 ; i < M ; i ++ ) { freq [ B . charAt ( i ) - ' a ' ] += 1 ; } int cnt = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { cnt += freq [ i ] / 2 ; } System . out . println ( Math . min ( cnt , Math . min ( N , M ) ) ) ; } public static void main ( String [ ] args ) { String A = " abdeff " ; String B = " abbet " ; lcsBySwapping ( A , B ) ; } }
Length of longest subsequence whose difference between maximum and minimum ASCII value of characters is exactly one | Java program for the above approach ; Function to find the maximum length of subsequence having difference of ASCII value of longest and smallest character as 1 ; Stores frequency of characters ; Iterate over characters of the string ; Stores the resultant length of subsequence ; Check if there exists any elements with ASCII value one less than character ch ; Size of current subsequence ; Update the value of ans ; Print the resultant count ; Driver Code
import java . util . * ; class GFG { static void maximumLengthSubsequence ( String str ) { HashMap < Character , Integer > mp = new HashMap < > ( ) ; for ( char ch : str . toCharArray ( ) ) { mp . put ( ch , mp . getOrDefault ( ch , 0 ) + 1 ) ; } int ans = 0 ; for ( char ch : str . toCharArray ( ) ) { if ( mp . containsKey ( ( char ) ( ch - 1 ) ) ) { int curr_max = mp . get ( ch ) + mp . get ( ( char ) ( ch - 1 ) ) ; ans = Math . max ( ans , curr_max ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " acbbebcg " ; maximumLengthSubsequence ( S ) ; } }
Minimum increments by 1 or K required to convert a string into another given string | Java program for the above approach ; Function to count minimum increments by 1 or K required to convert X to Y ; Convert String to character array ; Traverse the string X ; Case 1 ; Case 2 ; Add the difference / K to the count ; Add the difference % K to the count ; Case 3 ; Add the difference / K to the count ; Add the difference % K to the count ; Print the answer ; Driver Code
import java . io . * ; class GFG { public static void countOperations ( String X , String Y , int K ) { char ch1 [ ] = X . toCharArray ( ) ; char ch2 [ ] = Y . toCharArray ( ) ; int count = 0 ; for ( int i = 0 ; i < X . length ( ) ; i ++ ) { int c = 0 ; if ( ch1 [ i ] == ch2 [ i ] ) continue ; else if ( ch1 [ i ] < ch2 [ i ] ) { if ( ( ( int ) ch2 [ i ] - ( int ) ch1 [ i ] ) >= K ) { c = ( ( int ) ch2 [ i ] - ( int ) ch1 [ i ] ) / K ; } c += ( ( int ) ch2 [ i ] - ( int ) ch1 [ i ] ) % K ; } else { int t = 90 - ( int ) ch1 [ i ] ; t += ( int ) ch2 [ i ] - 65 + 1 ; if ( t >= K ) c = t / K ; c += ( t % K ) ; } count += c ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { String X = " ABCT " , Y = " PBDI " ; int K = 6 ; countOperations ( X , Y , K ) ; } }
Minimum number of swaps required such that a given substring consists of exactly K 1 s | Java program for the above approach ; Function to find the minimum number of swaps required such that the substring { s [ l ] , . . , s [ r ] } consists of exactly k 1 s ; Store the size of the string ; Store the total number of 1 s and 0 s in the entire string ; Traverse the string S to find the frequency of 1 and 0 ; Store the number of 1 s and 0 s in the substring s [ l , r ] ; Traverse the substring S [ l , r ] to find the frequency of 1 s and 0 s in it ; Store the count of 1 s and 0 s outside substring s [ l , r ] ; Check if the sum of the substring is at most K ; Store number of 1 s required ; Check if there are enough 1 s remaining to be swapped ; If the count of 1 s in the substring exceeds k ; Store the number of 0 s required ; Check if there are enough 0 s remaining to be swapped ; In all other cases , print - 1 ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int minimumSwaps ( String s , int l , int r , int k ) { int n = s . length ( ) ; int tot_ones = 0 , tot_zeros = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) tot_ones ++ ; else tot_zeros ++ ; } int ones = 0 , zeros = 0 , sum = 0 ; for ( int i = l - 1 ; i < r ; i ++ ) { if ( s . charAt ( i ) == '1' ) { ones ++ ; sum ++ ; } else zeros ++ ; } int rem_ones = tot_ones - ones ; int rem_zeros = tot_zeros - zeros ; if ( k >= sum ) { int rem = k - sum ; if ( zeros >= rem && rem_ones >= rem ) return rem ; } else if ( k < sum ) { int rem = sum - k ; if ( ones >= rem && rem_zeros >= rem ) return rem ; } return - 1 ; } public static void main ( String [ ] args ) { String S = "110011111000101" ; int L = 5 , R = 8 , K = 2 ; System . out . println ( minimumSwaps ( S , L , R , K ) ) ; } }
Check if given number contains only “ 01 ” and “ 10 ” as substring in its binary representation | Java Program to implement the above approach ; Function to generate all numbers having "01" and "10" as a substring ; Insert 2 and 5 ; Iterate till x is 10 ^ 15 ; Multiply x by 2 ; Update x as x * 2 + 1 ; Function to check if binary representation of N contains only "01" and "10" as substring ; Function Call to generate all such numbers ; Check if a number N exists in Ans [ ] or not ; If the number exists ; Driver Code
import java . util . * ; class GFG { static int N = 200000 ; static long Ans [ ] = new long [ 200000 ] ; static int index = 0 ; static void populateNumber ( ) { Ans [ index ++ ] = ( 2 ) ; Ans [ index ++ ] = ( 5 ) ; long x = 5 ; long inf = 1000000000001L ; while ( x < inf ) { x *= 2 ; Ans [ index ++ ] = ( x ) ; x = x * 2 + 1 ; Ans [ index ++ ] = ( x ) ; } } static void checkString ( int N ) { populateNumber ( ) ; for ( int i = 0 ; i < index ; i ++ ) { if ( Ans [ i ] == N ) { System . out . println ( " YES " ) ; return ; } } System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { N = 5 ; checkString ( N ) ; } }
Minimize length by removing subsequences forming valid parenthesis from a given string | Java program for the above approach ; Function to find the minimum count of remaining characters left into the string by removing the valid subsequences ; Length of the string ; Stores opening parenthesis ' ( ' of the given string ; Stores square parenthesis ' [ ' of the given string ; Stores count of opening parenthesis ' ( ' in valid subsequences ; Stores count of opening parenthesis ' [ ' in valid subsequences ; Iterate over each characters of S ; If current character is ' [ ' ; insert into stack ; If i is equal to ' ] ' ; If stack is not empty and top element of stack is ' [ ' ; Remove top element from stack ; Update squareCount ; If current character is ' ( ' ; Insert into stack ; If i is equal to ' ) ' ; If stack is not empty and top element of stack is ' ( ' ; Remove top element from stack ; Update roundCount ; Print the minimum number of remaining characters left into S ; Driver code ; input string ; function call
import java . io . * ; import java . util . Stack ; class GFG { public static void deleteSubseq ( String s ) { int N = s . length ( ) ; Stack < Character > roundStk = new Stack < > ( ) ; Stack < Character > squareStk = new Stack < > ( ) ; int roundCount = 0 ; int squareCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' [ ' ) { squareStk . push ( s . charAt ( i ) ) ; } else if ( s . charAt ( i ) == ' ] ' ) { if ( squareStk . size ( ) != 0 && squareStk . peek ( ) == ' [ ' ) { squareStk . pop ( ) ; squareCount += 1 ; } } else if ( s . charAt ( i ) == ' ( ' ) { roundStk . push ( s . charAt ( i ) ) ; } else { if ( roundStk . size ( ) != 0 && squareStk . peek ( ) == ' ( ' ) { squareStk . pop ( ) ; roundCount += 1 ; } } } System . out . println ( N - ( 2 * squareCount + 2 * roundCount ) ) ; } public static void main ( String [ ] args ) { String s = " [ ] ] ) ( [ " ; deleteSubseq ( s ) ; } }
Count minimum substring removals required to reduce string to a single distinct character | Java program for the above approach ; Function to find minimum removals required to convert given string to single distinct characters only ; Unordered map to store positions of characters X , Y and Z ; Update indices of X , Y , Z ; ; Stores the count of minimum removals ; Traverse the Map ; Count the number of removals required for current character ; Update the answer ; Print the answer ; Driver code ; Given string ; Size of string ; Function call
import java . util . * ; class GFG { static void minimumOperations ( String s , int n ) { HashMap < Character , List < Integer > > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( s . charAt ( i ) ) ) { mp . get ( s . charAt ( i ) ) . add ( i ) ; } else { mp . put ( s . charAt ( i ) , new ArrayList < Integer > ( Arrays . asList ( i ) ) ) ; } } int ans = Integer . MAX_VALUE ; for ( Map . Entry < Character , List < Integer > > x : mp . entrySet ( ) ) { int curr = 0 ; int prev = 0 ; boolean first = true ; for ( Integer index : ( x . getValue ( ) ) ) { if ( first ) { if ( index > 0 ) { curr ++ ; } prev = index ; first = false ; } else { if ( index != prev + 1 ) { curr ++ ; } prev = index ; } } if ( prev != n - 1 ) { curr ++ ; } ans = Math . min ( ans , curr ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String s = " YYXYZYXYZXY " ; int N = s . length ( ) ; minimumOperations ( s , N ) ; } }
Program to construct DFA accepting odd number of 0 s and odd number of 1 s | Java program for the above approach ; Function to check whether the given string is accepted by DFA or not ; Stores initial state of DFA ; Stores final state of DFA ; Stores previous state of DFA ; Iterate through the string ; Checking for all combinations ; Update the previous_state ; If final state is reached ; Otherwise ; Driver Code ; Given string ; Function Call
import java . util . * ; class GFG { static void checkValidDFA ( String s ) { int initial_state = 0 ; int final_state = 0 ; int previous_state = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( s . charAt ( i ) == '0' && previous_state == 0 ) || ( s . charAt ( i ) == '1' && previous_state == 3 ) ) { final_state = 1 ; } else if ( ( s . charAt ( i ) == '0' && previous_state == 3 ) || ( s . charAt ( i ) == '1' && previous_state == 0 ) ) { final_state = 2 ; } else if ( ( s . charAt ( i ) == '0' && previous_state == 1 ) || ( s . charAt ( i ) == '1' && previous_state == 2 ) ) { final_state = 0 ; } else if ( ( s . charAt ( i ) == '0' && previous_state == 2 ) || ( s . charAt ( i ) == '1' && previous_state == 1 ) ) { final_state = 3 ; } previous_state = final_state ; } if ( final_state == 3 ) { System . out . println ( " Accepted " ) ; } else { System . out . println ( " Not ▁ Accepted " ) ; } } public static void main ( String args [ ] ) { String s = "010011" ; checkValidDFA ( s ) ; } }
Difference between sums of odd level and even level nodes of a Binary Tree | Java program to find difference between sums of odd level and even level nodes of binary tree ; User defined node class ; Constructor to create a new tree node ; return difference of sums of odd level and even level ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check if level no . is even or odd and accordingly update the evenSum or oddSum ; check for left child ; check for right child ; Driver code ; construct a tree
import java . io . * ; import java . util . * ; class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = right = null ; } } class GFG { static int evenOddLevelDifference ( Node root ) { if ( root == null ) return 0 ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int level = 0 ; int evenSum = 0 , oddSum = 0 ; while ( q . size ( ) != 0 ) { int size = q . size ( ) ; level ++ ; while ( size > 0 ) { Node temp = q . remove ( ) ; if ( level % 2 == 0 ) evenSum += temp . data ; else oddSum += temp . data ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; size -- ; } } return ( oddSum - evenSum ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 5 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 6 ) ; root . left . left = new Node ( 1 ) ; root . left . right = new Node ( 4 ) ; root . left . right . left = new Node ( 3 ) ; root . right . right = new Node ( 8 ) ; root . right . right . right = new Node ( 9 ) ; root . right . right . left = new Node ( 7 ) ; System . out . println ( " diffence ▁ between ▁ sums ▁ is ▁ " + evenOddLevelDifference ( root ) ) ; } }
Difference between sums of odd level and even level nodes of a Binary Tree | A binary tree node ; The main function that return difference between odd and even level nodes ; Base case ; Difference for root is root 's data - difference for   left subtree - difference for right subtree ; Driver program to test above functions
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right ; } } class BinaryTree { Node root ; int getLevelDiff ( Node node ) { if ( node == null ) return 0 ; return node . data - getLevelDiff ( node . left ) - getLevelDiff ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . left . right . left = new Node ( 3 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . right = new Node ( 9 ) ; tree . root . right . right . left = new Node ( 7 ) ; System . out . println ( tree . getLevelDiff ( tree . root ) + " ▁ is ▁ the ▁ required ▁ difference " ) ; } }
Maximum spiral sum in Binary Tree | Java implementation to find maximum spiral sum ; A binary tree node has data , pointer to left child and a pointer to right child ; function to find the maximum sum contiguous subarray . implements kadane 's algorithm  ; to store the maximum value that is ending up to the current index ; to store the maximum value encountered so far ; traverse the array elements ; if max_ending_here < 0 , then it could not possibly contribute to the maximum sum further ; else add the value arr [ i ] to max_ending_here ; update max_so_far ; required maxium sum contiguous subarray value ; Function to find maximum spiral sum ; if tree is empty ; Create two stacks to store alternate levels For levels from right to left ; For levels from left to right ; ArrayList to store spiral order traversal of the binary tree ; Push first level to first stack ' s1' ; traversing tree in spiral form until there are elements in any one of the stacks ; traverse current level from s1 and push nodes of next level to s2 ; push temp - data to ' arr ' ; Note that right is pushed before left ; traverse current level from s2 and push nodes of next level to s1 ; push temp - data to ' arr ' ; Note that left is pushed before right ; required maximum spiral sum ; Driver program to test above
import java . util . ArrayList ; import java . util . Stack ; public class MaxSpiralSum { class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = right = null ; } } ; static int maxSum ( ArrayList < Integer > arr ) { int max_ending_here = Integer . MIN_VALUE ; int max_so_far = Integer . MIN_VALUE ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( max_ending_here < 0 ) max_ending_here = arr . get ( i ) ; else max_ending_here += arr . get ( i ) ; max_so_far = Math . max ( max_so_far , max_ending_here ) ; } return max_so_far ; } public static int maxSpiralSum ( Node root ) { if ( root == null ) return 0 ; Stack < Node > s1 = new Stack < > ( ) ; Stack < Node > s2 = new Stack < > ( ) ; ArrayList < Integer > arr = new ArrayList < > ( ) ; s1 . push ( root ) ; while ( ! s1 . isEmpty ( ) || ! s2 . isEmpty ( ) ) { while ( ! s1 . isEmpty ( ) ) { Node temp = s1 . pop ( ) ; arr . add ( temp . data ) ; if ( temp . right != null ) s2 . push ( temp . right ) ; if ( temp . left != null ) s2 . push ( temp . left ) ; } while ( ! s2 . isEmpty ( ) ) { Node temp = s2 . pop ( ) ; arr . add ( temp . data ) ; if ( temp . left != null ) s1 . push ( temp . left ) ; if ( temp . right != null ) s1 . push ( temp . right ) ; } } return maxSum ( arr ) ; } public static void main ( String args [ ] ) { Node root = new Node ( - 2 ) ; root . left = new Node ( - 3 ) ; root . right = new Node ( 4 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( - 2 ) ; root . right . right = new Node ( - 1 ) ; root . left . left . left = new Node ( - 3 ) ; root . right . right . right = new Node ( 2 ) ; System . out . print ( " Maximum ▁ Spiral ▁ Sum ▁ = ▁ " + maxSpiralSum ( root ) ) ; } }