codeparrot/xlcost-text-to-code huggingface dataset:
Minimize sum of product of same | Java Program to implement the above approach ; Function to print the arrays ; Function to reverse the subarray ; Function to calculate the minimum product of same - indexed elements of two given arrays ; Calculate initial product ; Traverse all odd length subarrays ; Remove the previous product ; Add the current product ; Check if current product is minimum or not ; Traverse all even length subarrays ; Remove the previous product ; Add to the current product ; Check if current product is minimum or not ; Update the pointers ; Reverse the subarray ; Print the subarray ; Driver Code
import java . io . * ; import java . util . * ; public class Main { static void print ( long a [ ] , long b [ ] ) { int minProd = 0 ; for ( int i = 0 ; i < a . length ; ++ i ) { System . out . print ( a [ i ] + " ▁ " ) ; } System . out . println ( ) ; for ( int i = 0 ; i < b . length ; ++ i ) { System . out . print ( b [ i ] + " ▁ " ) ; minProd += a [ i ] * b [ i ] ; } System . out . println ( ) ; System . out . println ( minProd ) ; } static void reverse ( int left , int right , long arr [ ] ) { while ( left < right ) { long temp = arr [ left ] ; arr [ left ] = arr [ right ] ; arr [ right ] = temp ; ++ left ; -- right ; } } static void minimumProdArray ( long a [ ] , long b [ ] , int l ) { long total = 0 ; for ( int i = 0 ; i < a . length ; ++ i ) { total += a [ i ] * b [ i ] ; } long min = Integer . MAX_VALUE ; int first = 0 ; int second = 0 ; for ( int i = 0 ; i < a . length ; ++ i ) { int left = i - 1 ; int right = i + 1 ; long total1 = total ; while ( left >= 0 && right < a . length ) { total1 -= a [ left ] * b [ left ] + a [ right ] * b [ right ] ; total1 += a [ left ] * b [ right ] + a [ right ] * b [ left ] ; if ( min >= total1 ) { min = total1 ; first = left ; second = right ; } -- left ; ++ right ; } } for ( int i = 0 ; i < a . length ; ++ i ) { int left = i ; int right = i + 1 ; long total1 = total ; while ( left >= 0 && right < a . length ) { total1 -= a [ left ] * b [ left ] + a [ right ] * b [ right ] ; total1 += a [ left ] * b [ right ] + a [ right ] * b [ left ] ; if ( min >= total1 ) { min = total1 ; first = left ; second = right ; } -- left ; ++ right ; } } if ( min < total ) { reverse ( first , second , a ) ; print ( a , b ) ; } else { print ( a , b ) ; } } public static void main ( String [ ] args ) { int n = 4 ; long a [ ] = { 2 , 3 , 1 , 5 } ; long b [ ] = { 8 , 2 , 4 , 3 } ; minimumProdArray ( a , b , n ) ; } }
Maximize cost of removing all occurrences of substrings " ab " and " ba " | Java program for the above approach : ; Function to find the maximum cost of removing substrings " ab " and " ba " from S ; MaxStr is the substring char array with larger cost ; MinStr is the substring char array with smaller cost ; ; Denotes larger point ; Denotes smaller point ; Stores cost scored ; Stack to keep track of characters ; Traverse the string ; If the substring is maxstr ; Pop from the stack ; Add maxp to cost ; Push the character to the stack ; Remaining string after removing maxstr ; Find remaining string ; Reversing the string retrieved from the stack ; Removing all occurences of minstr ; If the substring is minstr ; Pop from the stack ; Add minp to the cost ; Otherwise ; Return the maximum cost ; Driver Code ; Input String ; Costs
import java . util . * ; class GFG { public static int MaxCollection ( String S , int P , int Q ) { char maxstr [ ] = ( P >= Q ? " ab " : " ba " ) . toCharArray ( ) ; char minstr [ ] = ( P >= Q ? " ba " : " ab " ) . toCharArray ( ) ; int maxp = Math . max ( P , Q ) ; int minp = Math . min ( P , Q ) ; int cost = 0 ; Stack < Character > stack1 = new Stack < > ( ) ; char [ ] s = S . toCharArray ( ) ; for ( char ch : s ) { if ( ! stack1 . isEmpty ( ) && ( stack1 . peek ( ) == maxstr [ 0 ] && ch == maxstr [ 1 ] ) ) { stack1 . pop ( ) ; cost += maxp ; } else { stack1 . push ( ch ) ; } } StringBuilder sb = new StringBuilder ( ) ; while ( ! stack1 . isEmpty ( ) ) sb . append ( stack1 . pop ( ) ) ; sb = sb . reverse ( ) ; String remstr = sb . toString ( ) ; for ( char ch : remstr . toCharArray ( ) ) { if ( ! stack1 . isEmpty ( ) && ( stack1 . peek ( ) == minstr [ 0 ] && ch == minstr [ 1 ] ) ) { stack1 . pop ( ) ; cost += minp ; } else { stack1 . push ( ch ) ; } } return cost ; } public static void main ( String [ ] args ) { String S = " cbbaabbaab " ; int P = 6 ; int Q = 4 ; System . out . println ( MaxCollection ( S , P , Q ) ) ; } }
Vertical width of Binary tree | Set 2 | Java code to find the vertical width of a binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to fill hd in set . ; Third parameter is horizontal distance ; Driver program to test above functions ; Constructed bunary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7
import java . io . * ; import java . util . * ; class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void fillSet ( Node root , Set < Integer > set , int hd ) { if ( root == null ) return ; fillSet ( root . left , set , hd - 1 ) ; set . add ( hd ) ; fillSet ( root . right , set , hd + 1 ) ; } int verticalWidth ( Node root ) { Set < Integer > set = new HashSet < Integer > ( ) ; fillSet ( root , set , 0 ) ; return set . size ( ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 6 ) ; tree . root . right . right . right = new Node ( 7 ) ; System . out . println ( tree . verticalWidth ( tree . root ) ) ; } }
Maximum value of ( arr [ i ] * arr [ j ] ) + ( arr [ j ] | Java program for the above approach ; Function to find the value of the expression a * b + ( b - a ) ; Function to find the maximum value of the expression a * b + ( b - a ) possible for any pair ( a , b ) ; Sort the vector in ascending order ; Stores the maximum value ; Update ans by choosing the pair of the minimum and 2 nd minimum ; Update ans by choosing the pair of maximum and 2 nd maximum ; Return the value of ans ; Driver Code ; Given inputs
import java . util . * ; class GFG { static int calc ( int a , int b ) { return a * b + ( b - a ) ; } static int findMaximum ( int [ ] arr , int N ) { Arrays . sort ( arr ) ; int ans = ( int ) - 1e9 ; ans = Math . max ( ans , calc ( arr [ 0 ] , arr [ 1 ] ) ) ; ans = Math . max ( ans , calc ( arr [ N - 2 ] , arr [ N - 1 ] ) ) ; return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , - 47 , 12 } ; int N = arr . length ; System . out . println ( findMaximum ( arr , N ) ) ; } }
Minimize flips on K |  ; store previous flip events ; remove an item which is out range of window . ; In a window , if A [ i ] is a even number with even times fliped , it need to be fliped again . On other hand , if A [ i ] is a odd number with odd times fliped , it need to be fliped again . ; flip . offer ( i ) ; insert ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int minKBitFlips ( int [ ] A , int K ) { Queue < Integer > flip = new LinkedList < > ( ) ; int count = 0 ; for ( int i = 0 ; i < A . length ; i ++ ) { if ( ! flip . isEmpty ( ) && ( i - flip . peek ( ) >= K ) ) { flip . poll ( ) ; } if ( A [ i ] % 2 == flip . size ( ) % 2 ) { if ( i + K - 1 >= A . length ) { return - 1 ; } count ++ ; } } return count ; } public static void main ( String [ ] args ) { int [ ] A = { 0 , 1 , 0 } ; int K = 1 ; int ans = minKBitFlips ( A , K ) ; System . out . println ( ans ) ; } }
Minimize flips on K |  ; We 're flipping the subarray from A[i] to A[i+K-1]  ; If not flip entire array , impossible ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int minKBitFlips ( int [ ] A , int K ) { int [ ] temp = new int [ A . length ] ; int count = 0 ; int flip = 0 ; for ( int i = 0 ; i < A . length ; ++ i ) { flip ^= temp [ i ] ; if ( A [ i ] == flip ) { count ++ ; if ( i + K > A . length ) { return - 1 ; } flip ^= 1 ; if ( i + K < A . length ) { temp [ i + K ] ^= 1 ; } } } return count ; } public static void main ( String [ ] args ) { int [ ] A = { 0 , 1 , 0 } ; int K = 1 ; int ans = minKBitFlips ( A , K ) ; System . out . println ( ans ) ; } }
Generate an N | Java program for the above approach ; Function to calculate GCD of given array ; Utility function to check for all the combinations ; If a sequence of size N is obtained ; If gcd of current combination is K ; If current element from first array is divisible by K ; Recursively proceed further ; If current combination satisfies given condition ; Remove the element from the combination ; If current element from second array is divisible by K ; Recursively proceed further ; If current combination satisfies given condition ; Remove the element from the combination ; Function to check for all possible combinations ; Stores the subsequence ; If GCD of any sequence is not equal to K ; Driver Code ; Given arrays ; Given value of K ; Function call to generate a subsequence whose GCD is K
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int GCDArr ( ArrayList < Integer > a ) { int ans = a . get ( 0 ) ; for ( int val : a ) { ans = GCD ( ans , val ) ; } return ans ; } static boolean findSubseqUtil ( int a [ ] , int b [ ] , ArrayList < Integer > ans , int k , int i ) { if ( ans . size ( ) == a . length ) { if ( GCDArr ( ans ) == k ) { System . out . println ( ans ) ; return true ; } else { return false ; } } if ( a [ i ] % k == 0 ) { ans . add ( a [ i ] ) ; boolean temp = findSubseqUtil ( a , b , ans , k , i + 1 ) ; if ( temp == true ) return true ; ans . remove ( ans . size ( ) - 1 ) ; } if ( b [ i ] % k == 0 ) { ans . add ( b [ i ] ) ; boolean temp = findSubseqUtil ( a , b , ans , k , i + 1 ) ; if ( temp == true ) return true ; ans . remove ( ans . size ( ) - 1 ) ; } return false ; } static void findSubseq ( int A [ ] , int B [ ] , int K , int i ) { ArrayList < Integer > ans = new ArrayList < > ( ) ; boolean ret = findSubseqUtil ( A , B , ans , K , i ) ; if ( ret == false ) System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 3 , 6 , 2 , 9 } ; int B [ ] = { 21 , 7 , 14 , 12 , 28 } ; int K = 3 ; findSubseq ( A , B , K , 0 ) ; } }
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | Java program for the above approach ; Function to find the K - th element in the required permutation ; Stores the required permutation ; Insert all the even numbers less than or equal to N ; Now , insert all odd numbers less than or equal to N ; Print the Kth element ; Driver code ; functions call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findKthElement ( int N , int K ) { ArrayList < Integer > v = new ArrayList < > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { v . add ( i ) ; } } for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 != 0 ) { v . add ( i ) ; } } System . out . println ( v . get ( K - 1 ) ) ; } public static void main ( String [ ] args ) { int N = 10 , K = 3 ; findKthElement ( N , K ) ; } }
Maximize difference between odd and even indexed array elements by shift operations | Java program for the above approach ; Function to minimize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the minimum possible value ; Function to maximize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the maximum possible value ; Function to maximize the absolute difference between even and odd indexed array elements ; To calculate the difference of odd indexed elements and even indexed elements ; To calculate the difference between odd and even indexed array elements ; Print the maximum value ; Driver Code
import java . io . * ; class GFG { static int minimize ( int n ) { int optEle = n ; String strEle = Integer . toString ( n ) ; for ( int idx = 0 ; idx < strEle . length ( ) ; idx ++ ) { int temp = Integer . parseInt ( strEle . substring ( idx ) + strEle . substring ( 0 , idx ) ) ; optEle = Math . min ( optEle , temp ) ; } return optEle ; } static int maximize ( int n ) { int optEle = n ; String strEle = Integer . toString ( n ) ; for ( int idx = 0 ; idx < strEle . length ( ) ; idx ++ ) { int temp = Integer . parseInt ( strEle . substring ( idx ) + strEle . substring ( 0 , idx ) ) ; optEle = Math . max ( optEle , temp ) ; } return optEle ; } static void minimumDifference ( int [ ] arr ) { int caseOne = 0 ; int minVal = 0 ; int maxVal = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( i % 2 == 0 ) minVal += minimize ( arr [ i ] ) ; else maxVal += maximize ( arr [ i ] ) ; } caseOne = Math . abs ( maxVal - minVal ) ; int caseTwo = 0 ; minVal = 0 ; maxVal = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( i % 2 == 0 ) maxVal += maximize ( arr [ i ] ) ; else minVal += minimize ( arr [ i ] ) ; caseTwo = Math . abs ( maxVal - minVal ) ; } System . out . println ( Math . max ( caseOne , caseTwo ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 332 , 421 , 215 , 584 , 232 } ; minimumDifference ( arr ) ; } }
Minimize operations of removing 2 i | Java program to implement the above approach ; Function to find minimum count of steps required to remove all the array elements ; Stores minimum count of steps required to remove all the array elements ; Update N ; Traverse each bit of N ; If current bit is set ; Update cntStep ; Driver code
import java . util . * ; class GFG { static int minimumStepReqArr ( int [ ] arr , int N ) { int cntStep = 0 ; N += 1 ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( ( N & ( 1 << i ) ) != 0 ) { cntStep += 1 ; } } return cntStep ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . length ; System . out . println ( minimumStepReqArr ( arr , N ) ) ; } }
Minimize removal of alternating subsequences to empty given Binary String | Java program for the above approach ; Function to find the minimum number of operations required to empty the string ; Initialize variables ; Traverse the string ; If current character is 0 ; Update maximum consecutive 0 s and 1 s ; Print the minimum operation ; Driver Code ; input string ; length of string ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static void minOpsToEmptyString ( String S , int N ) { int one = 0 , zero = 0 ; int x0 = 0 , x1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '0' ) { x0 ++ ; x1 = 0 ; } else { x1 ++ ; x0 = 0 ; } zero = Math . max ( x0 , zero ) ; one = Math . max ( x1 , one ) ; } System . out . println ( Math . max ( one , zero ) ) ; } public static void main ( String [ ] args ) { String S = "0100100111" ; int N = S . length ( ) ; minOpsToEmptyString ( S , N ) ; } }
Swap upper and lower triangular halves of a given Matrix | Java program for the above approach ; Function to swap laterally inverted images of upper and lower triangular halves of a given matrix ; Store the matrix elements from upper & lower triangular halves ; Traverse the matrix mat [ ] [ ] ; Find the index ; If current element lies on the principal diagonal ; If current element lies below the principal diagonal ; If current element lies above the principal diagonal ; Traverse again to swap values ; Find the index ; Principal diagonal ; Below main diagonal ; Above main diagonal ; Traverse the matrix and print ; Driver Code ; Given Matrix mat [ ] [ ] ; Swap the upper and lower triangular halves
import java . io . * ; class GFG { static void ReverseSwap ( int [ ] [ ] mat , int n ) { int [ ] lowerEle = new int [ n ] ; int [ ] upperEle = new int [ n ] ; int index ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { index = Math . abs ( i - j ) ; if ( i == j ) { continue ; } else if ( j < i ) { lowerEle [ index ] = mat [ i ] [ j ] ; } else { upperEle [ index ] = mat [ i ] [ j ] ; } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { index = Math . abs ( i - j ) ; if ( i == j ) { continue ; } else if ( j < i ) { mat [ i ] [ j ] = upperEle [ index ] ; } else { mat [ i ] [ j ] = lowerEle [ index -- ] ; } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] mat = new int [ ] [ ] { { 1 , 2 } , { 4 , 5 } } ; int N = mat . length ; ReverseSwap ( mat , N ) ; } }
Minimize sum of K positive integers with given LCM | Java program to implement the above approach ; Function to find the prime power of X ; Stores prime powers of X ; Iterate over the range [ 2 , Math . sqrt ( X ) ] ; If X is divisible by i ; Stores prime power ; Calculate prime power of X ; Update X ; Update p ; Insert prime powers into primePow [ ] ; If X exceeds 1 ; Function to calculate the sum of array elements ; Stores sum of array elements ; Traverse the array ; Update sum ; Function to partition array into K groups such that sum of elements of the K groups is minimum ; If count of prime powers is equal to pos ; Stores minimum sum of of each partition of arr [ ] into K groups ; Traverse the array arr [ ] ; Include primePow [ pos ] into i - th groups ; Update res ; Remove factors [ pos ] from i - th groups ; Utility function to calculate minimum sum of K positive integers whose LCM is X ; Stores all prime powers of X ; Stores count of prime powers ; Stores minimum sum of K positive integers whose LCM is X ; If n is less than or equal to k ; Traverse primePow [ ] array ; Update sum ; Update sum ; arr [ i ] : Stores element in i - th group by partitioning the primePow [ ] array ; Update sum ; Driver Code
import java . util . * ; class GFG { static Vector < Integer > primePower ( int X ) { Vector < Integer > primePow = new Vector < Integer > ( ) ; for ( int i = 2 ; i * i <= X ; i ++ ) { if ( X % i == 0 ) { int p = 1 ; while ( X % i == 0 ) { X /= i ; p *= i ; } primePow . add ( p ) ; } } if ( X > 1 ) { primePow . add ( X ) ; } return primePow ; } static int getSum ( int [ ] ar ) { int sum = 0 ; for ( int i : ar ) { sum += i ; } return sum ; } static int getMinSum ( int pos , int [ ] arr , Vector < Integer > primePow ) { if ( pos == primePow . size ( ) ) { return getSum ( arr ) ; } int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < arr . length ; i ++ ) { arr [ i ] *= primePow . get ( pos ) ; res = Math . min ( res , getMinSum ( pos + 1 , arr , primePow ) ) ; arr [ i ] /= primePow . get ( pos ) ; } return res ; } static int minimumSumWithGivenLCM ( int k , int x ) { Vector < Integer > primePow = primePower ( x ) ; int n = primePow . size ( ) ; int sum = 0 ; if ( n <= k ) { for ( int i : primePow ) { sum += i ; } sum += k - n ; } else { int [ ] arr = new int [ k ] ; Arrays . fill ( arr , 1 ) ; sum = getMinSum ( 0 , arr , primePow ) ; } return sum ; } public static void main ( String [ ] args ) { int k = 3 , x = 210 ; System . out . print ( minimumSumWithGivenLCM ( k , x ) ) ; } }
Minimum removal of subsequences of distinct consecutive characters required to empty a given string | Java program to implement the above approach ; Function to count minimum operations required to make the string an empty string ; Stores count of 1 s by removing consecutive distinct subsequence ; Stores count of 0 s by removing consecutive distinct subsequence ; Stores length of str ; Traverse the string ; If current character is 0 ; Update cntOne ; Update cntZero ; If current character is 1 ; Update cntZero ; Update cntOne ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int findMinOperationsReqEmpStr ( String str ) { int cntOne = 0 ; int cntZero = 0 ; int N = str . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == '0' ) { if ( cntOne != 0 ) { cntOne -- ; } cntZero ++ ; } else { if ( cntZero != 0 ) { cntZero -- ; } cntOne ++ ; } } return ( cntOne + cntZero ) ; } public static void main ( String [ ] args ) { String str = "0100100111" ; System . out . print ( findMinOperationsReqEmpStr ( str ) ) ; } }
Minimum number of flips required such that the last cell of matrix can be reached from any other cell | Java program for the above approach ; Function to calculate the minimum number of flips required ; Dimensions of mat [ ] [ ] ; Initialize answer ; Count all ' D ' s in the last row ; Count all ' R ' s in the last column ; Print answer ; Driver Code ; Given matrix ; Function call ; Print answer
import java . io . * ; class GFG { public static int countChanges ( char mat [ ] [ ] ) { int n = mat . length ; int m = mat [ 0 ] . length ; int ans = 0 ; for ( int j = 0 ; j < m - 1 ; j ++ ) { if ( mat [ n - 1 ] [ j ] != ' R ' ) ans ++ ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( mat [ i ] [ m - 1 ] != ' D ' ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { char arr [ ] [ ] = { { ' R ' , ' R ' , ' R ' , ' D ' } , { ' D ' , ' D ' , ' D ' , ' R ' } , { ' R ' , ' D ' , ' R ' , ' F ' } } ; int cnt = countChanges ( arr ) ; System . out . println ( cnt ) ; } }
Check if a binary tree is sorted level | Java program to determine whether binary tree is level sorted or not . ; Structure of a tree node . ; Function to create new tree node . ; Function to determine if given binary tree is level sorted or not . ; to store maximum value of previous level . ; to store minimum value of current level . ; to store maximum value of current level . ; to store number of nodes in current level . ; queue to perform level order traversal . ; find number of nodes in current level . ; traverse current level and find minimum and maximum value of this level . ; if minimum value of this level is not greater than maximum value of previous level then given tree is not level sorted . ; maximum value of this level is previous maximum value for next level . ; Driver program ; 1 / 4 \ 6 / \ 8 9 / \ 12 10
import java . util . * ; class GfG { static class Node { int key ; Node left , right ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } static int isSorted ( Node root ) { int prevMax = Integer . MIN_VALUE ; int minval ; int maxval ; int levelSize ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { levelSize = q . size ( ) ; minval = Integer . MAX_VALUE ; maxval = Integer . MIN_VALUE ; while ( levelSize > 0 ) { root = q . peek ( ) ; q . remove ( ) ; levelSize -- ; minval = Math . min ( minval , root . key ) ; maxval = Math . max ( maxval , root . key ) ; if ( root . left != null ) q . add ( root . left ) ; if ( root . right != null ) q . add ( root . right ) ; } if ( minval <= prevMax ) return 0 ; prevMax = maxval ; } return 1 ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 4 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 8 ) ; root . left . right . right = newNode ( 9 ) ; root . left . right . left . left = newNode ( 12 ) ; root . left . right . right . right = newNode ( 10 ) ; if ( isSorted ( root ) == 1 ) System . out . println ( " Sorted " ) ; else System . out . println ( " Not ▁ sorted " ) ; } }