codeparrot/xlcost-text-to-code huggingface dataset:
Maximize Array sum by swapping at most K elements with another array | Java implementation to find maximum sum of array A by swapping at most K elements with array B ; Function to find the maximum sum ; If element of array a is smaller than that of array b , swap them . ; Find sum of resultant array ; Driver Code
import java . util . * ; class GFG { static void maximumSum ( int a [ ] , int b [ ] , int k , int n ) { int i , j ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; for ( i = 0 , j = n - 1 ; i < k ; i ++ , j -- ) { if ( a [ i ] < b [ j ] ) { int temp = a [ i ] ; a [ i ] = b [ j ] ; b [ j ] = temp ; } else break ; } int sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += a [ i ] ; System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int K = 1 ; int A [ ] = { 2 , 3 , 4 } ; int B [ ] = { 6 , 8 , 5 } ; int N = A . length ; maximumSum ( A , B , K , N ) ; } }
Maximize distinct elements by incrementing / decrementing an element or keeping it same | Java program to Maximize the count of distinct element ; Function that Maximize the count of distinct element ; sort thr array ; keeping track of previous change ; decrement is possible ; remain as it is ; increment is possible ; Driver Code
import java . util . * ; public class GFG { static int max_dist_ele ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int ans = 0 ; int prev = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prev < ( arr [ i ] - 1 ) ) { ans ++ ; prev = arr [ i ] - 1 ; } else if ( prev < ( arr [ i ] ) ) { ans ++ ; prev = arr [ i ] ; } else if ( prev < ( arr [ i ] + 1 ) ) { ans ++ ; prev = arr [ i ] + 1 ; } } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 1 , 8 , 8 , 8 , 9 , 9 } ; int n = arr . length ; System . out . println ( max_dist_ele ( arr , n ) ) ; } }
Find if array can be sorted by swaps limited to multiples of k |  ; CheckSort function To check if array can be sorted ; sortarr is sorted array of arr ; If k = 1 then ( always possible to sort ) swapping can easily give sorted array ; Comparing sortarray with array ; Element at index j must be in j = i + l * k form where i = 0 , 1 , 2 , 3. . . where l = 0 , 1 , 2 , 3 , . . n - 1 ; If element is present then swapped ; If element of sorted array does not found in its sequence then flag remain zero that means arr can not be sort after swapping ; If flag is 0 Not possible else Possible ; Driver code ; Size of step ; Array initialized ; Length of arr ; Calling function
import java . util . * ; class GFG { public static void CheckSort ( Vector < Integer > arr , int k , int n ) { Vector < Integer > sortarr = new Vector < Integer > ( ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { sortarr . add ( arr . get ( i ) ) ; } Collections . sort ( sortarr ) ; if ( k == 1 ) System . out . println ( " yes " ) ; else { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { flag = 0 ; for ( int j = i ; j < n ; j += k ) { if ( sortarr . get ( i ) == arr . get ( j ) ) { Collections . swap ( arr , i , j ) ; flag = 1 ; break ; } if ( j + k >= n ) break ; } if ( flag == 0 ) break ; } if ( flag == 0 ) System . out . println ( " Not ▁ possible ▁ to ▁ sort " ) ; else System . out . println ( " Possible ▁ to ▁ sort " ) ; } } public static void main ( String [ ] args ) { int k = 3 ; Vector < Integer > arr = new Vector < Integer > ( ) ; arr . add ( 1 ) ; arr . add ( 5 ) ; arr . add ( 6 ) ; arr . add ( 9 ) ; arr . add ( 2 ) ; arr . add ( 3 ) ; arr . add ( 5 ) ; arr . add ( 9 ) ; int n = arr . size ( ) ; CheckSort ( arr , k , n ) ; } }
Replace node with depth in a binary tree | Java program to replace every key value with its depth . ; A tree node structure ; Utility function to create a new Binary Tree node ; Helper function replaces the data with depth Note : Default value of level is 0 for root . ; Base Case ; Replace data with current depth ; A utility function to print inorder traversal of a Binary Tree ; Driver function to test above functions ; Constructing tree given in the above figure
class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static void replaceNode ( Node node , int level ) { if ( node == null ) return ; node . data = level ; replaceNode ( node . left , level + 1 ) ; replaceNode ( node . right , level + 1 ) ; } static void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String [ ] args ) { Node root = new Node ( ) ; root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 4 ) ; System . out . println ( " Before ▁ Replacing ▁ Nodes " ) ; printInorder ( root ) ; replaceNode ( root , 0 ) ; System . out . println ( ) ; System . out . println ( " After ▁ Replacing ▁ Nodes " ) ; printInorder ( root ) ; } }
Product of minimum edge weight between all pairs of a Tree | Java Implementation of above approach ; to store first vertex , second vertex and weight of the edge ; Function to return ( x ^ y ) mod p ; Declaring size array globally ; Initializing DSU data structure ; Function to find the root of ith node in the disjoint set ; Weighted union using Path Compression ; size of set A is small than size of set B ; size of set B is small than size of set A ; Function to add an edge in the tree ; Build the tree ; Function to return the required product ; Sorting the edges with respect to its weight ascending order ; Start iterating in decreasing order of weight ; Determine Current edge values ; Calculate root of each node and size of each set ; Using the formula ; Calculating final result ; Weighted union using Path Compression ; Driver code
import java . util . ArrayList ; import java . util . Collections ; public class Product { static class Edge implements Comparable < Edge > { int first , second , weight ; Edge ( int x , int y , int w ) { this . first = x ; s this . second = y ; this . weight = w ; } @ Override public int compareTo ( Edge edge ) { return this . weight - edge . weight ; } } static int mod = 1000000007 ; static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int size [ ] = new int [ 300005 ] ; static int freq [ ] = new int [ 300004 ] ; static ArrayList < Edge > edges = new ArrayList < Edge > ( ) ; static void initialize ( int Arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Arr [ i ] = i ; size [ i ] = 1 ; } } static int root ( int Arr [ ] , int i ) { while ( Arr [ i ] != i ) { i = Arr [ i ] ; } return i ; } static void weighted_union ( int Arr [ ] , int size [ ] , int A , int B ) { int root_A = root ( Arr , A ) ; int root_B = root ( Arr , B ) ; if ( size [ root_A ] < size [ root_B ] ) { Arr [ root_A ] = Arr [ root_B ] ; size [ root_B ] += size [ root_A ] ; } else { Arr [ root_B ] = Arr [ root_A ] ; size [ root_A ] += size [ root_B ] ; } } static void AddEdge ( int a , int b , int w ) { edges . add ( new Edge ( a , b , w ) ) ; } static void MakeTree ( ) { AddEdge ( 1 , 2 , 1 ) ; AddEdge ( 1 , 3 , 3 ) ; AddEdge ( 3 , 4 , 2 ) ; } static int MinProduct ( ) { int result = 1 ; Collections . sort ( edges ) ; for ( int i = edges . size ( ) - 1 ; i >= 0 ; i -- ) { int curr_weight = edges . get ( i ) . weight ; int Node1 = edges . get ( i ) . first ; int Node2 = edges . get ( i ) . second ; int Root1 = root ( freq , Node1 ) ; int Set1_size = size [ Root1 ] ; int Root2 = root ( freq , Node2 ) ; int Set2_size = size [ Root2 ] ; int prod = Set1_size * Set2_size ; int Product = power ( curr_weight , prod , mod ) ; result = ( ( result % mod ) * ( Product % mod ) ) % mod ; weighted_union ( freq , size , Node1 , Node2 ) ; } return result % mod ; } public static void main ( String [ ] args ) { int n = 4 ; initialize ( freq , n ) ; MakeTree ( ) ; System . out . println ( MinProduct ( ) ) ; } }
Check whether an array can be made strictly decreasing by modifying at most one element | Java implementation of the approach ; Function that returns true if the array can be made strictly decreasing with at most one change ; To store the number of modifications required to make the array strictly decreasing ; Check whether the last element needs to be modify or not ; Check whether the first element needs to be modify or not ; Loop from 2 nd element to the 2 nd last element ; Check whether arr [ i ] needs to be modified ; Modifying arr [ i ] ; Check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; If more than 1 modification is required ; Driver code
class GFG { public static boolean check ( int [ ] arr , int n ) { int modify = 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) { arr [ n - 1 ] = arr [ n - 2 ] - 1 ; modify ++ ; } if ( arr [ 0 ] <= arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] + 1 ; modify ++ ; } for ( int i = n - 2 ; i > 0 ; i -- ) { if ( ( arr [ i - 1 ] <= arr [ i ] && arr [ i + 1 ] <= arr [ i ] ) || ( arr [ i - 1 ] >= arr [ i ] && arr [ i + 1 ] >= arr [ i ] ) ) { arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 ; modify ++ ; if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i + 1 ] ) return false ; } } if ( modify > 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 11 , 3 } ; int n = arr . length ; if ( check ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Maximal Disjoint Intervals | Java implementation of the approach ; Function to sort the vector elements by second element of Pairs ; Function to find maximal disjoint set ; Sort the list of intervals ; First Interval will always be included in set ; End point of first interval ; Check if given interval overlap with previously included interval , if not then include this interval and update the end point of last added interval ; Driver code
import java . util . * ; class GFG { static class Pair implements Comparable < Pair > { int first , second ; Pair ( int f , int s ) { first = f ; second = s ; } @ Override public int compareTo ( Pair o ) { if ( this . second > o . second ) return 1 ; else if ( this . second == o . second ) return 0 ; return - 1 ; } } static void maxDisjointIntervals ( Pair [ ] list ) { Collections . sort ( Arrays . asList ( list ) ) ; System . out . print ( " [ " + list [ 0 ] . first + " , ▁ " + list [ 0 ] . second + " ] " + "NEW_LINE"); int r1 = list [ 0 ] . second ; for ( int i = 1 ; i < list . length ; i ++ ) { int l1 = list [ i ] . first ; int r2 = list [ i ] . second ; if ( l1 > r1 ) { System . out . print ( " [ " + l1 + " , ▁ " + r2 + " ] " + "NEW_LINE"); r1 = r2 ; } } } public static void main ( String [ ] args ) { int N = 4 ; Pair [ ] intervals = { new Pair ( 1 , 4 ) , new Pair ( 2 , 3 ) , new Pair ( 4 , 6 ) , new Pair ( 8 , 9 ) } ; maxDisjointIntervals ( intervals ) ; } }
Print k different sorted permutations of a given array | Java implementation of the approach ; Utility function to print the original indices of the elements of the array ; Used for sorting in ascending order ; Function to print the required permutations ; To keep track of original indices ; Sort the array ; Count the number of swaps that can be made ; Cannot generate 3 permutations ; Print the first permutation ; Find an index to swap and create second permutation ; Print the last permutation ; Driver code ; Function call
import java . util . * ; class GFG { static int next_pos = 1 ; static class pair { int first , second ; pair ( ) { first = 0 ; second = 0 ; } } static void printIndices ( int n , pair a [ ] ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] . second + " ▁ " ) ; System . out . println ( ) ; } static class sort implements Comparator < pair > { public int compare ( pair a , pair b ) { return a . first < b . first ? - 1 : 1 ; } } static void printPermutations ( int n , int a [ ] , int k ) { pair arr [ ] = new pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = new pair ( ) ; arr [ i ] . first = a [ i ] ; arr [ i ] . second = i ; } Arrays . sort ( arr , new sort ( ) ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] . first == arr [ i - 1 ] . first ) count ++ ; if ( count < k ) { System . out . print ( " - 1" ) ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) { printIndices ( n , arr ) ; for ( int j = next_pos ; j < n ; j ++ ) { if ( arr [ j ] . first == arr [ j - 1 ] . first ) { pair t = arr [ j ] ; arr [ j ] = arr [ j - 1 ] ; arr [ j - 1 ] = t ; next_pos = j + 1 ; break ; } } } printIndices ( n , arr ) ; } public static void main ( String arsg [ ] ) { int a [ ] = { 1 , 3 , 3 , 1 } ; int n = a . length ; int k = 3 ; printPermutations ( n , a , k ) ; } }
Maximum width of a binary tree | A binary tree node has data , pointer to left child and a pointer to right child ; Function to get the maximum width of a binary tree ; Get width of each level and compare the width with maximum width so far ; Get width of a given level ; Compute the " height " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the height of each subtree ; use the larger one ; Driver code ; Constructed binary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 ; Function call
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int getMaxWidth ( Node node ) { int maxWidth = 0 ; int width ; int h = height ( node ) ; int i ; for ( i = 1 ; i <= h ; i ++ ) { width = getWidth ( node , i ) ; if ( width > maxWidth ) maxWidth = width ; } return maxWidth ; } int getWidth ( Node node , int level ) { if ( node == null ) return 0 ; if ( level == 1 ) return 1 ; else if ( level > 1 ) return getWidth ( node . left , level - 1 ) + getWidth ( node . right , level - 1 ) ; return 0 ; } int height ( Node node ) { if ( node == null ) return 0 ; else { int lHeight = height ( node . left ) ; int rHeight = height ( node . right ) ; return ( lHeight > rHeight ) ? ( lHeight + 1 ) : ( rHeight + 1 ) ; } } 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 ( " Maximum ▁ width ▁ is ▁ " + tree . getMaxWidth ( tree . root ) ) ; } }
Count distinct elements in an array | Java Program to count Unique elements in Array ; This function prints all distinct elements ; Creates an empty hashset ; Traverse the input array ; If not present , then put it in hashtable and increment result ; Driver code
import java . util . * ; class GFG { public static int countDistinct ( int arr [ ] , int n ) { HashSet < Integer > hs = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { hs . add ( arr [ i ] ) ; } return hs . size ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 } ; System . out . println ( countDistinct ( arr , arr . length ) ) ; } }
In | Java program for the above approach ; Calculating next gap ; Function for swapping ; Merging the subarrays using shell sorting Time Complexity : O ( nlog n ) Space Complexity : O ( 1 ) ; merge sort makes log n recursive calls and each time calls merge ( ) which takes nlog n steps Time Complexity : O ( n * log n + 2 ( ( n / 2 ) * log ( n / 2 ) ) + 4 ( ( n / 4 ) * log ( n / 4 ) ) + ... . . + 1 ) Time Complexity : O ( logn * ( n * log n ) ) i . e . O ( n * ( logn ) ^ 2 ) Space Complexity : O ( 1 ) ; Calculating mid to slice the array in two halves ; Recursive calls to sort left and right subarrays ; Driver Code
import java . io . * ; import java . util . * ; class InPlaceMerge { private static int nextGap ( int gap ) { if ( gap <= 1 ) return 0 ; return ( int ) Math . ceil ( gap / 2.0 ) ; } private static void swap ( int [ ] nums , int i , int j ) { int temp = nums [ i ] ; nums [ i ] = nums [ j ] ; nums [ j ] = temp ; } private static void inPlaceMerge ( int [ ] nums , int start , int end ) { int gap = end - start + 1 ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( int i = start ; i + gap <= end ; i ++ ) { int j = i + gap ; if ( nums [ i ] > nums [ j ] ) swap ( nums , i , j ) ; } } } private static void mergeSort ( int [ ] nums , int s , int e ) { if ( s == e ) return ; int mid = ( s + e ) / 2 ; mergeSort ( nums , s , mid ) ; mergeSort ( nums , mid + 1 , e ) ; inPlaceMerge ( nums , s , e ) ; } public static void main ( String [ ] args ) { int [ ] nums = new int [ ] { 12 , 11 , 13 , 5 , 6 , 7 } ; mergeSort ( nums , 0 , nums . length - 1 ) ; System . out . println ( Arrays . toString ( nums ) ) ; } }
Rearrange an array to maximize i * arr [ i ] | Java program for the Optimal Solution ; Function to calculate the maximum points earned by making an optimal selection on the given array ; Sorting the array ; Variable to store the total points earned ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int findOptimalSolution ( int [ ] a , int N ) { Arrays . sort ( a ) ; int points = 0 ; for ( int i = 0 ; i < N ; i ++ ) { points += a [ i ] * i ; } return points ; } public static void main ( String args [ ] ) { int [ ] a = { 1 , 4 , 2 , 3 , 9 } ; int N = a . length ; System . out . println ( findOptimalSolution ( a , N ) ) ; } }
Minimum number of towers required such that every house is in the range of at least one tower | Java implementation of above approach ; Function to count the number of tower ; first we sort the house numbers ; for count number of towers ; for iterate all houses ; count number of towers ; find find the middle location ; traverse till middle location ; this is point to middle house where we insert the tower ; now find the last location ; traverse till last house of the range ; return the number of tower ; Driver code ; given elements ; print number of towers
import java . util . Arrays ; public class Improve { static int number_of_tower ( int house [ ] , int range , int n ) { Arrays . sort ( house ) ; int numOfTower = 0 ; int i = 0 ; while ( i < n ) { numOfTower ++ ; int loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; -- i ; loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; } return numOfTower ; } public static void main ( String args [ ] ) { int house [ ] = { 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 } ; int range = 2 ; int n = house . length ; System . out . println ( number_of_tower ( house , range , n ) ) ; } }
Check if the characters of a given string are in alphabetical order | Java implementation of above approach ; Function that checks whether the string is in alphabetical order or not ; length of the string ; create a character array of the length of the string ; assign the string to character array ; sort the character array ; check if the character array is equal to the string or not ; Driver Code ; check whether the string is in alphabetical order or not
import java . util . Arrays ; public class GFG { static boolean isAlphabaticOrder ( String s ) { int n = s . length ( ) ; char c [ ] = new char [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = s . charAt ( i ) ; } Arrays . sort ( c ) ; for ( int i = 0 ; i < n ; i ++ ) if ( c [ i ] != s . charAt ( i ) ) return false ; return true ; } public static void main ( String args [ ] ) { String s = " aabbbcc " ; if ( isAlphabaticOrder ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Sort first k values in ascending order and remaining n | Java program to sort first k elements in increasing order and remaining n - k elements in decreasing ; function to sort the array ; Sort first k elements in ascending order ; Sort remaining n - k elements in descending order ; Driver code ; Our arr contains 8 elements
import java . util . * ; public class SortExample { static void printOrder ( Integer [ ] arr , int k ) { int n = arr . length ; Arrays . sort ( arr , 0 , k ) ; Arrays . sort ( arr , k , n , Collections . reverseOrder ( ) ) ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , - 1 } ; int k = 4 ; printOrder ( arr , k ) ; System . out . printf ( " % s " , Arrays . toString ( arr ) ) ; } }
Maximum width of a binary tree | Java program to calculate maximum width of a binary tree using queue ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to find the maximum width of the tree using level order traversal ; Base case ; Initialize result ; Do Level order traversal keeping track of number of nodes at every level ; Get the size of queue when the level order traversal for one level finishes ; Update the maximum node count value ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue left and right children of dequeued node ; Driver code ; Function call
import java . util . LinkedList ; import java . util . Queue ; public class maxwidthusingqueue { static class node { int data ; node left , right ; public node ( int data ) { this . data = data ; } } static int maxwidth ( node root ) { if ( root == null ) return 0 ; int maxwidth = 0 ; Queue < node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { int count = q . size ( ) ; maxwidth = Math . max ( maxwidth , count ) ; while ( count -- > 0 ) { node temp = q . remove ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } } } return maxwidth ; } public static void main ( String [ ] args ) { 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 . right = new node ( 8 ) ; root . right . right . left = new node ( 6 ) ; root . right . right . right = new node ( 7 ) ; System . out . println ( " Maximum ▁ width ▁ = ▁ " + maxwidth ( root ) ) ; } }
Merging and Sorting Two Unsorted Stacks | Java program to merge to unsorted stacks into a third stack in sorted way . ; This is the temporary stack ; Sorts input stack and returns sorted stack . ; pop out the first element ; while temporary stack is not empty and top of stack is greater than temp ; pop from temporary stack and push it to the input stack ; push temp in temporary of stack ; Push contents of both stacks in result ; Sort the result stack . ; main function
import java . io . * ; import java . util . * ; public class GFG { static Stack < Integer > res = new Stack < Integer > ( ) ; static Stack < Integer > tmpStack = new Stack < Integer > ( ) ; static void sortStack ( Stack < Integer > input ) { while ( input . size ( ) != 0 ) { int tmp = input . peek ( ) ; input . pop ( ) ; while ( tmpStack . size ( ) != 0 && tmpStack . peek ( ) > tmp ) { input . push ( tmpStack . peek ( ) ) ; tmpStack . pop ( ) ; } tmpStack . push ( tmp ) ; } } static void sortedMerge ( Stack < Integer > s1 , Stack < Integer > s2 ) { while ( s1 . size ( ) != 0 ) { res . push ( s1 . peek ( ) ) ; s1 . pop ( ) ; } while ( s2 . size ( ) != 0 ) { res . push ( s2 . peek ( ) ) ; s2 . pop ( ) ; } sortStack ( res ) ; } public static void main ( String args [ ] ) { Stack < Integer > s1 = new Stack < Integer > ( ) ; Stack < Integer > s2 = new Stack < Integer > ( ) ; s1 . push ( 34 ) ; s1 . push ( 3 ) ; s1 . push ( 31 ) ; s2 . push ( 1 ) ; s2 . push ( 12 ) ; s2 . push ( 23 ) ; sortedMerge ( s1 , s2 ) ; System . out . println ( " Sorted ▁ and ▁ merged ▁ stack ▁ : " ) ; while ( tmpStack . size ( ) != 0 ) { System . out . print ( tmpStack . peek ( ) + " ▁ " ) ; tmpStack . pop ( ) ; } } }