codeparrot/xlcost-text-to-code huggingface dataset:
Find next right node of a given key | Set 2 | Java program to find next right of a given key using preorder traversal ; A Binary Tree Node ; Utility function to create a new tree node ; Function to find next node for given node in same level in a binary tree by using pre - order traversal ; return null if tree is empty ; if desired node is found , set value [ 0 ] to current level ; if value [ 0 ] is already set , then current node is the next right node ; recurse for left subtree by increasing level by 1 ; if node is found in left subtree , return it ; recurse for right subtree by increasing level by 1 ; Function to find next node of given node in the same level in given binary tree ; A utility function to test above functions ; Driver program to test above functions ; Let us create binary tree given in the above example
import java . util . * ; class GfG { static class Node { Node left , right ; int key ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } static Node nextRightNode ( Node root , int k , int level , int [ ] value ) { if ( root == null ) return null ; if ( root . key == k ) { value [ 0 ] = level ; return null ; } else if ( value [ 0 ] != 0 ) { if ( level == value [ 0 ] ) return root ; } Node leftNode = nextRightNode ( root . left , k , level + 1 , value ) ; if ( leftNode != null ) return leftNode ; return nextRightNode ( root . right , k , level + 1 , value ) ; } static Node nextRightNodeUtil ( Node root , int k ) { int [ ] v = new int [ 1 ] ; v [ 0 ] = 0 ; return nextRightNode ( root , k , 1 , v ) ; } static void test ( Node root , int k ) { Node nr = nextRightNodeUtil ( root , k ) ; if ( nr != null ) System . out . println ( " Next ▁ Right ▁ of ▁ " + k + " ▁ is ▁ " + nr . key ) ; else System . out . println ( " No ▁ next ▁ right ▁ node ▁ found ▁ for ▁ " + k ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 6 ) ; root . right . right = newNode ( 5 ) ; root . left . left = newNode ( 8 ) ; root . left . right = newNode ( 4 ) ; test ( root , 10 ) ; test ( root , 2 ) ; test ( root , 6 ) ; test ( root , 5 ) ; test ( root , 8 ) ; test ( root , 4 ) ; } }
Top three elements in binary tree | Java program to find largest three elements in a binary tree . ; Helper function that allocates a new Node with the given data and null left and right pointers . ; function to find three largest element ; if data is greater than first large number update the top three list ; if data is greater than second large number and not equal to first update the bottom two list ; if data is greater than third large number and not equal to first & second update the third highest list ; driver function
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static int first , second , third ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static void threelargest ( Node root ) { if ( root == null ) return ; if ( root . data > first ) { third = second ; second = first ; first = root . data ; } else if ( root . data > second && root . data != first ) { third = second ; second = root . data ; } else if ( root . data > third && root . data != first && root . data != second ) third = root . data ; threelargest ( root . left ) ; threelargest ( root . right ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; first = 0 ; second = 0 ; third = 0 ; threelargest ( root ) ; System . out . print ( " three ▁ largest ▁ elements ▁ are ▁ " + first + " ▁ " + second + " ▁ " + third ) ; } }
Find maximum ( or minimum ) in Binary Tree | A binary tree node ; Constructor that allocates a new node with the given data and NULL left and right pointers . ; Returns the max value in a binary tree ; Base case ; Return maximum of 3 values : 1 ) Root 's data 2) Max in Left Subtree 3) Max in right subtree ; Driver code ; Function call
class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; static int findMax ( Node node ) { if ( node == null ) return Integer . MIN_VALUE ; int res = node . data ; int lres = findMax ( node . left ) ; int rres = findMax ( node . right ) ; if ( lres > res ) res = lres ; if ( rres > res ) res = rres ; return res ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 2 ) ; tree . root . left = new Node ( 7 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . left . right . left = new Node ( 1 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . root . right . right = new Node ( 9 ) ; tree . root . right . right . left = new Node ( 4 ) ; System . out . println ( " Maximum ▁ element ▁ is ▁ " + tree . findMax ( tree . root ) ) ; } }
Find maximum ( or minimum ) in Binary Tree | Returns the min value in a binary tree
static int findMin ( Node node ) { if ( node == null ) return Integer . MAX_VALUE ; int res = node . data ; int lres = findMin ( node . left ) ; int rres = findMin ( node . right ) ; if ( lres < res ) res = lres ; if ( rres < res ) res = rres ; return res ; }
Extract Leaves of a Binary Tree in a Doubly Linked List | A binay tree node ; Binary Tree class ; The main function that links the list list to be traversed ; Utility function for printing tree in In - Order . ; Prints the DLL in both forward and reverse directions . ; Driver program to test above functions
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; right = left = null ; } } public class BinaryTree { Node root ; Node head ; Node prev ; public Node extractLeafList ( Node root ) { if ( root == null ) return null ; if ( root . left == null && root . right == null ) { if ( head == null ) { head = root ; prev = root ; } else { prev . right = root ; root . left = prev ; prev = root ; } return null ; } root . left = extractLeafList ( root . left ) ; root . right = extractLeafList ( root . right ) ; return root ; } void inorder ( Node node ) { if ( node == null ) return ; inorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; inorder ( node . right ) ; } public void printDLL ( Node head ) { Node last = null ; while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; last = head ; head = head . right ; } } 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 ( 6 ) ; tree . root . left . left . left = new Node ( 7 ) ; tree . root . left . left . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 9 ) ; tree . root . right . right . right = new Node ( 10 ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ given ▁ tree ▁ is ▁ : ▁ " ) ; tree . inorder ( tree . root ) ; tree . extractLeafList ( tree . root ) ; System . out . println ( " " ) ; System . out . println ( " Extracted ▁ double ▁ link ▁ list ▁ is ▁ : ▁ " ) ; tree . printDLL ( tree . head ) ; System . out . println ( " " ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ modified ▁ tree ▁ is ▁ : ▁ " ) ; tree . inorder ( tree . root ) ; } }
Inorder Successor of a node in Binary Tree | structure of a Binary Node . ; class to find inorder successor of a node ; to change previous node ; function to find inorder successor of a node ; Case1 : If right child is not NULL ; Case2 : If root data is equal to search node ; Driver program to test above functions ; Let 's construct the binary tree   as shown in above diagram  ; Case 1 ; Case 2 ; Case 3
class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } class InorderSuccessor { Node root ; static class PreviousNode { Node pNode ; PreviousNode ( ) { pNode = null ; } } private void inOrderSuccessorOfBinaryTree ( Node root , PreviousNode pre , int searchNode ) { if ( root . right != null ) inOrderSuccessorOfBinaryTree ( root . right , pre , searchNode ) ; if ( root . data == searchNode ) System . out . println ( " inorder ▁ successor ▁ of ▁ " + searchNode + " ▁ is : ▁ " + ( pre . pNode != null ? pre . pNode . data : " null " ) ) ; pre . pNode = root ; if ( root . left != null ) inOrderSuccessorOfBinaryTree ( root . left , pre , searchNode ) ; } public static void main ( String [ ] args ) { InorderSuccessor tree = new InorderSuccessor ( ) ; 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 ( 6 ) ; tree . inOrderSuccessorOfBinaryTree ( tree . root , new PreviousNode ( ) , 3 ) ; tree . inOrderSuccessorOfBinaryTree ( tree . root , new PreviousNode ( ) , 4 ) ; tree . inOrderSuccessorOfBinaryTree ( tree . root , new PreviousNode ( ) , 6 ) ; } }
Graph and its representations | Java code to demonstrate Graph representation using ArrayList in Java ; A utility function to add an edge in an undirected graph ; A utility function to print the adjacency list representation of graph ; Driver Code ; Creating a graph with 5 vertices ; Adding edges one by one
import java . util . * ; class Graph { static void addEdge ( ArrayList < ArrayList < Integer > > adj , int u , int v ) { adj . get ( u ) . add ( v ) ; adj . get ( v ) . add ( u ) ; } static void printGraph ( ArrayList < ArrayList < Integer > > adj ) { for ( int i = 0 ; i < adj . size ( ) ; i ++ ) { System . out . println ( " Adjacency list of vertex " + i); System . out . print ( " head " ) ; for ( int j = 0 ; j < adj . get ( i ) . size ( ) ; j ++ ) { System . out . print ( " ▁ - > ▁ " + adj . get ( i ) . get ( j ) ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int V = 5 ; ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( V ) ; for ( int i = 0 ; i < V ; i ++ ) adj . add ( new ArrayList < Integer > ( ) ) ; addEdge ( adj , 0 , 1 ) ; addEdge ( adj , 0 , 4 ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 1 , 4 ) ; addEdge ( adj , 2 , 3 ) ; addEdge ( adj , 3 , 4 ) ; printGraph ( adj ) ; } }
Graph representations using set and hash | A Java program to demonstrate adjacency list using HashMap and TreeSet representation of graphs using sets ; TreeSet is used to get clear understand of graph . ; Adds an edge to an undirected graph ; Add an edge from src to dest into the set ; Since graph is undirected , add an edge from dest to src into the set ; A utility function to print the graph ; Searches for a given edge in the graph ; Driver code ; Create the graph given in the above figure ; Print the adjacency list representation of the above graph ; Search the given edge in the graph
import java . util . * ; class Graph { HashMap < Integer , TreeSet < Integer > > graph ; static int v ; public Graph ( ) { graph = new HashMap < > ( ) ; for ( int i = 0 ; i < v ; i ++ ) { graph . put ( i , new TreeSet < > ( ) ) ; } } public void addEdge ( int src , int dest ) { graph . get ( src ) . add ( dest ) ; graph . get ( dest ) . add ( src ) ; } public void printGraph ( ) { for ( int i = 0 ; i < v ; i ++ ) { System . out . println ( " Adjacency ▁ list ▁ of ▁ vertex ▁ " + i ) ; Iterator set = graph . get ( i ) . iterator ( ) ; while ( set . hasNext ( ) ) System . out . print ( set . next ( ) + " ▁ " ) ; System . out . println ( ) ; System . out . println ( ) ; } } public void searchEdge ( int src , int dest ) { Iterator set = graph . get ( src ) . iterator ( ) ; if ( graph . get ( src ) . contains ( dest ) ) System . out . println ( " Edge ▁ from ▁ " + src + " ▁ to ▁ " + dest + " ▁ found " ) ; else System . out . println ( " Edge ▁ from ▁ " + src + " ▁ to ▁ " + dest + " ▁ not ▁ found " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { v = 5 ; Graph graph = new Graph ( ) ; graph . addEdge ( 0 , 1 ) ; graph . addEdge ( 0 , 4 ) ; graph . addEdge ( 1 , 2 ) ; graph . addEdge ( 1 , 3 ) ; graph . addEdge ( 1 , 4 ) ; graph . addEdge ( 2 , 3 ) ; graph . addEdge ( 3 , 4 ) ; graph . printGraph ( ) ; graph . searchEdge ( 2 , 1 ) ; graph . searchEdge ( 0 , 3 ) ; } }
Find n | Java program for nth nodes of inorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Given a binary tree , print its nth nodes of inorder ; first recur on left child ; when count = n then print element ; now recur on right child ; Driver program to test above functions
import java . util . * ; class Solution { static int count = 0 ; static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static void NthInorder ( Node node , int n ) { if ( node == null ) return ; if ( count <= n ) { NthInorder ( node . left , n ) ; count ++ ; if ( count == n ) System . out . printf ( " % d ▁ " , node . data ) ; NthInorder ( node . right , n ) ; } } public static void main ( String args [ ] ) { Node root = newNode ( 10 ) ; root . left = newNode ( 20 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 40 ) ; root . left . right = newNode ( 50 ) ; int n = 4 ; NthInorder ( root , n ) ; } }
Level of Each node in a Tree from source node ( using BFS ) | Java Program to determine level of each node and print level ; function to determine level of each node starting from x using BFS ; array to store level of each node ; create a queue ; enqueue element x ; initialize level of source node to 0 ; marked it as visited ; do until queue is empty ; get the first element of queue ; dequeue element ; traverse neighbors of node x ; b is neighbor of node x ; if b is not marked already ; enqueue b in queue ; level of b is level of x + 1 ; mark b ; display all nodes and their levels ; Driver Code ; adjacency graph for tree ; call levels function with source as 0
import java . util . * ; class GFG { static void printLevels ( Vector < Vector < Integer > > graph , int V , int x ) { int level [ ] = new int [ V ] ; boolean marked [ ] = new boolean [ V ] ; Queue < Integer > que = new LinkedList < Integer > ( ) ; que . add ( x ) ; level [ x ] = 0 ; marked [ x ] = true ; while ( que . size ( ) > 0 ) { x = que . peek ( ) ; que . remove ( ) ; for ( int i = 0 ; i < graph . get ( x ) . size ( ) ; i ++ ) { int b = graph . get ( x ) . get ( i ) ; if ( ! marked [ b ] ) { que . add ( b ) ; level [ b ] = level [ x ] + 1 ; marked [ b ] = true ; } } } System . out . println ( " Nodes " + " ▁ " + " Level " ) ; for ( int i = 0 ; i < V ; i ++ ) System . out . println ( " ▁ " + i + " ▁ - - > ▁ " + level [ i ] ) ; } public static void main ( String args [ ] ) { int V = 8 ; Vector < Vector < Integer > > graph = new Vector < Vector < Integer > > ( ) ; for ( int i = 0 ; i < V + 1 ; i ++ ) graph . add ( new Vector < Integer > ( ) ) ; graph . get ( 0 ) . add ( 1 ) ; graph . get ( 0 ) . add ( 2 ) ; graph . get ( 1 ) . add ( 3 ) ; graph . get ( 1 ) . add ( 4 ) ; graph . get ( 1 ) . add ( 5 ) ; graph . get ( 2 ) . add ( 5 ) ; graph . get ( 2 ) . add ( 6 ) ; graph . get ( 6 ) . add ( 7 ) ; printLevels ( graph , V , 0 ) ; } }
Construct binary palindrome by repeated appending and trimming | JAVA code to form binary palindrome ; function to apply DFS ; set the parent marked ; if the node has not been visited set it and its children marked ; link which digits must be equal ; connect the two indices ; set everything connected to first character as 1 ; Driver code
import java . util . * ; class GFG { static void dfs ( int parent , int ans [ ] , Vector < Integer > connectchars [ ] ) { ans [ parent ] = 1 ; for ( int i = 0 ; i < connectchars [ parent ] . size ( ) ; i ++ ) { if ( ans [ connectchars [ parent ] . get ( i ) ] != 1 ) dfs ( connectchars [ parent ] . get ( i ) , ans , connectchars ) ; } } static void printBinaryPalindrome ( int n , int k ) { int [ ] arr = new int [ n ] ; int [ ] ans = new int [ n ] ; Vector < Integer > [ ] connectchars = new Vector [ k ] ; for ( int i = 0 ; i < k ; i ++ ) connectchars [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = i % k ; for ( int i = 0 ; i < n / 2 ; i ++ ) { connectchars [ arr [ i ] ] . add ( arr [ n - i - 1 ] ) ; connectchars [ arr [ n - i - 1 ] ] . add ( arr [ i ] ) ; } dfs ( 0 , ans , connectchars ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( ans [ arr [ i ] ] ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 4 ; printBinaryPalindrome ( n , k ) ; } }
Find n | Java program to find n - th node of Postorder Traversal of Binary Tree ; A binary tree node structure ; function to find the N - th node in the postorder traversal of a given binary tree ; left recursion ; right recursion ; prints the n - th node of preorder traversal ; driver code ; prints n - th node found
public class NthNodePostOrder { static int flag = 0 ; class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; } } ; public static void NthPostordernode ( Node root , int N ) { if ( root == null ) return ; if ( flag <= N ) { NthPostordernode ( root . left , N ) ; NthPostordernode ( root . right , N ) ; flag ++ ; if ( flag == N ) System . out . print ( root . data ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 25 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . left = new Node ( 18 ) ; root . left . right = new Node ( 22 ) ; root . right . left = new Node ( 24 ) ; root . right . right = new Node ( 32 ) ; int N = 6 ; NthPostordernode ( root , N ) ; } }
DFS for a n | JAVA Code For DFS for a n - ary tree ( acyclic graph ) represented as adjacency list ; DFS on tree ; Printing traversed node ; Traversing adjacent edges ; Not traversing the parent node ; Driver program to test above function ; Number of nodes ; Adjacency list ; Designing the tree ; Function call
import java . util . * ; class GFG { public static void dfs ( LinkedList < Integer > list [ ] , int node , int arrival ) { System . out . println ( node ) ; for ( int i = 0 ; i < list [ node ] . size ( ) ; i ++ ) { if ( list [ node ] . get ( i ) != arrival ) dfs ( list , list [ node ] . get ( i ) , node ) ; } } public static void main ( String [ ] args ) { int nodes = 5 ; LinkedList < Integer > list [ ] = new LinkedList [ nodes + 1 ] ; for ( int i = 0 ; i < list . length ; i ++ ) { list [ i ] = new LinkedList < Integer > ( ) ; } list [ 1 ] . add ( 2 ) ; list [ 2 ] . add ( 1 ) ; list [ 1 ] . add ( 3 ) ; list [ 3 ] . add ( 1 ) ; list [ 2 ] . add ( 4 ) ; list [ 4 ] . add ( 2 ) ; list [ 3 ] . add ( 5 ) ; list [ 5 ] . add ( 3 ) ; dfs ( list , 1 , 0 ) ; } }
Maximum number of edges to be added to a tree so that it stays a Bipartite graph | Java code to print maximum edges such that Tree remains a Bipartite graph ; To store counts of nodes with two colors ; Increment count of nodes with current color ; Traversing adjacent nodes ; Not recurring for the parent node ; Finds maximum number of edges that can be added without violating Bipartite property . ; Do a DFS to count number of nodes of each color ; Driver code
import java . util . * ; class GFG { static long [ ] count_color = new long [ 2 ] ; static void dfs ( Vector < Integer > adj [ ] , int node , int parent , boolean color ) { count_color [ color == false ? 0 : 1 ] ++ ; for ( int i = 0 ; i < adj [ node ] . size ( ) ; i ++ ) { if ( adj [ node ] . get ( i ) != parent ) dfs ( adj , adj [ node ] . get ( i ) , node , ! color ) ; } } static int findMaxEdges ( Vector < Integer > adj [ ] , int n ) { dfs ( adj , 1 , 0 , false ) ; return ( int ) ( count_color [ 0 ] * count_color [ 1 ] - ( n - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 5 ; Vector < Integer > [ ] adj = new Vector [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; adj [ 1 ] . add ( 2 ) ; adj [ 1 ] . add ( 3 ) ; adj [ 2 ] . add ( 4 ) ; adj [ 3 ] . add ( 5 ) ; System . out . println ( findMaxEdges ( adj , n ) ) ; } }
Min steps to empty an Array by removing a pair each time with sum at most K | Java program implementation of the above approach ; Function to count minimum steps ; Function to sort the array ; Run while loop ; Condition to check whether sum exceed the target or not ; Increment the step by 1 ; Return minimum steps ; Driver code ; Given target value ; Print the minimum flip
import java . util . * ; import java . io . * ; class GFG { static int countMinSteps ( int arr [ ] , int target , int n ) { Arrays . sort ( arr ) ; int minimumSteps = 0 ; int i = 0 ; int j = n - 1 ; while ( i <= j ) { if ( arr [ i ] + arr [ j ] <= target ) { i += 1 ; j -= 1 ; } else { j -= 1 ; } minimumSteps += 1 ; } return minimumSteps ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 2 , 9 , 6 , 5 , 8 , 10 } ; int target = 11 ; int size = arr . length ; System . out . print ( countMinSteps ( arr , target , size ) ) ; } }
Sort the strings based on the numbers of matchsticks required to represent them | Java implementation to sort the strings with the number of sticks required to represent them ; Stick [ ] stores the count of sticks required to represent the alphabets ; Number [ ] stores the count of sticks required to represent the numerals ; Utility Pair implementation ; Function that return the count of sticks required to represent the given string str ; Loop to iterate over every character of the string ; Add the count of sticks required to represent the current character ; Function to sort the array according to the number of sticks required to represent it ; ArrayList to store the number of sticks required with respective strings ; Inserting number of sticks with respective strings ; Sort the list , ; Print the sorted vector ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int sticks [ ] = { 6 , 7 , 4 , 6 , 5 , 4 , 6 , 5 , 2 , 4 , 4 , 3 , 6 , 6 , 6 , 5 , 7 , 6 , 5 , 3 , 5 , 4 , 6 , 4 , 3 , 4 } ; static int number [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static class Pair implements Comparable < Pair > { int num_sticks ; String str ; Pair ( int n , String s ) { num_sticks = n ; str = s ; } public int compareTo ( Pair p ) { return this . num_sticks - p . num_sticks ; } } static int countSticks ( String str ) { int cnt = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch >= ' A ' && ch <= ' Z ' ) { cnt += sticks [ ch - ' A ' ] ; } else { cnt += number [ ch - '0' ] ; } } return cnt ; } static void sortArr ( String arr [ ] , int n ) { ArrayList < Pair > list = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { list . add ( new Pair ( countSticks ( arr [ i ] ) , arr [ i ] ) ) ; } Collections . sort ( list ) ; for ( int i = 0 ; i < list . size ( ) ; i ++ ) System . out . print ( list . get ( i ) . str + " ▁ " ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " GEEKS " , " FOR " , " GEEKSFORGEEKS " } ; int n = arr . length ; sortArr ( arr , n ) ; } }