"Contiguous unique substrings with the given length L | Java implementation of above approach ; Function to print the unique sub - String of length n ; set to store the Strings ; if the size of the String is equal to 1 then insert ; inserting unique sub - String of length L ; Printing the set of Strings ; Driver Code ; Function calling"
How can the above be solved in Java?

import java . util . * ; class GFG { static void result ( String s , int n ) { HashSet < String > st = new HashSet < String > ( ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; i ++ ) { String ans = " " ; for ( int j = i ; j < ( int ) s . length ( ) ; j ++ ) { ans += s . charAt ( j ) ; if ( ans . length ( ) == n ) { st . add ( ans ) ; break ; } } } for ( String it : st ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { String s = " abca " ; int n = 3 ; result ( s , n ) ; } }


"Continuous Tree | JAVA Code to check if the tree is continuos or not ; Node structure ; Function to check if the tree is continuos or not ; If root is Null then tree isn 't Continuos ; BFS Traversal ; Move to left child ; if difference between parent and child is equal to 1 then do continue otherwise make flag = 0 and break ; Move to right child ; if difference between parent and child is equal to 1 then do continue otherwise make flag = 0 and break ; Driver Code ; Constructing the Tree ; Function Call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static class node { int val ; node left ; node right ; node ( ) { this . val = 0 ; this . left = null ; this . right = null ; } node ( int x ) { this . val = x ; this . left = null ; this . right = null ; } node ( int x , node left , node right ) { this . val = x ; this . left = left ; this . right = right ; } } ; static boolean continuous ( node root ) { if ( root == null ) return false ; int flag = 1 ; Queue < node > Q = new LinkedList < > ( ) ; Q . add ( root ) ; node temp ; while ( ! Q . isEmpty ( ) ) { temp = Q . peek ( ) ; Q . remove ( ) ; if ( temp . left != null ) { if ( Math . abs ( temp . left . val - temp . val ) == 1 ) Q . add ( temp . left ) ; else { flag = 0 ; break ; } } if ( temp . right != null ) { if ( Math . abs ( temp . right . val - temp . val ) == 1 ) Q . add ( temp . right ) ; else { flag = 0 ; break ; } } } if ( flag != 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { node root = new node ( 3 ) ; root . left = new node ( 2 ) ; root . right = new node ( 4 ) ; root . left . left = new node ( 1 ) ; root . left . right = new node ( 3 ) ; root . right . right = new node ( 5 ) ; if ( continuous ( root ) ) System . out . print ( "True
"); else System . out . print ( "False
"); } }


"Continuous Tree | Java program to check if a tree is continuous or not ; 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 . ; Function to check tree is continuous or not ; if next node is empty then return true ; if current node is leaf node then return true because it is end of root to leaf path ; If left subtree is empty , then only check right ; If right subtree is empty , then only check left ; If both left and right subtrees are not empty , check everything ; Driver program to test mirror ( )"
How can the above be solved in Java?

import java . util . * ; class solution { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static boolean treeContinuous ( Node ptr ) { if ( ptr == null ) return true ; if ( ptr . left == null && ptr . right == null ) return true ; if ( ptr . left == null ) return ( Math . abs ( ptr . data - ptr . right . data ) == 1 ) && treeContinuous ( ptr . right ) ; if ( ptr . right == null ) return ( Math . abs ( ptr . data - ptr . left . data ) == 1 ) && treeContinuous ( ptr . left ) ; return Math . abs ( ptr . data - ptr . left . data ) == 1 && Math . abs ( ptr . data - ptr . right . data ) == 1 && treeContinuous ( ptr . left ) && treeContinuous ( ptr . right ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 4 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 3 ) ; root . right . right = newNode ( 5 ) ; if ( treeContinuous ( root ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Convert 0 to N by adding 1 or multiplying by 2 in minimum steps | Java program for the above approach ; Function to count number of set bits in N ; Stores the count of set bits ; If N is odd , then it a set bit ; Return the result ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int minimumAdditionOperation ( int N ) { int count = 0 ; while ( N > 0 ) { if ( N % 2 == 1 ) { count ++ ; } N = N >> 1 ; } return count ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( minimumAdditionOperation ( N ) ) ; } }


"Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1 | Java program for the above approach ; Function to print the Minimum number of operations required to convert 1 into X by using three given operations ; Initialize a DP array to store min operations for sub - problems ; Base Condition : No operation required when N is 1 ; Multiply the number by 2 ; Multiply the number by 3 ; Add 1 to the number . ; Print the minimum operations ; Initialize a list to store the sequence ; Backtrack to find the sequence ; If add by 1 ; If multiply by 2 ; If multiply by 3 ; Print the sequence of operation ; Driver code ; Given number X ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void printMinOperations ( int N ) { int dp [ ] = new int [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = N ; dp [ 1 ] = 0 ; for ( int i = 2 ; i < N + 1 ; i ++ ) { if ( i % 2 == 0 && dp [ i ] > dp [ i / 2 ] + 1 ) dp [ i ] = dp [ i / 2 ] + 1 ; if ( i % 3 == 0 && dp [ i ] > dp [ i / 3 ] + 1 ) dp [ i ] = dp [ i / 3 ] + 1 ; if ( dp [ i ] > dp [ i - 1 ] + 1 ) dp [ i ] = dp [ i - 1 ] + 1 ; } System . out . println ( dp [ N ] ) ; Vector < Integer > seq = new Vector < Integer > ( ) ; while ( N > 1 ) { seq . add ( N ) ; if ( dp [ N - 1 ] == dp [ N ] - 1 ) N = N - 1 ; else if ( N % 2 == 0 && dp [ N / 2 ] == dp [ N ] - 1 ) N = N / 2 ; else N = N / 3 ; } seq . add ( 1 ) ; for ( int i = seq . size ( ) - 1 ; i >= 0 ; i -- ) System . out . print ( seq . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int X = 96234 ; printMinOperations ( X ) ; } }


"Convert A into B by incrementing or decrementing 1 , 2 , or 5 any number of times | Java program for the above approach ; Function to find minimum number of moves required to convert A into B ; Stores the minimum number of moves required ; Stores the absolute difference ; FInd the number of moves ; Return cnt ; Driver Code ; Input ; Function call"
How can the above be solved in Java?

import java . io . * ; class GFG { static int minimumSteps ( int a , int b ) { int cnt = 0 ; a = Math . abs ( a - b ) ; cnt = ( a / 5 ) + ( a % 5 ) / 2 + ( a % 5 ) % 2 ; return cnt ; } public static void main ( String [ ] args ) { int A = 3 , B = 9 ; System . out . println ( minimumSteps ( A , B ) ) ; } }


"Convert Decimal To Hexa | Java program to convert decimal to hexadecimal covering negative numbers ; Function to convert decimal no . to hexadecimal number ; map for decimal to hexa , 0 - 9 are straightforward , alphabets a - f used for 10 to 15. ; string to be returned ; check if num is 0 and directly return "0" ; if num > 0 , use normal technique as discussed in other post ; if num < 0 , we need to use the elaborated trick above , lets see this ; store num in a u_int , size of u_it is greater , it will be positive since msb is 0 ; use the same remainder technique . ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . util . HashMap ; import java . util . Map ; class GFG { static String Hex ( int num ) { HashMap < Integer , Character > m = new HashMap < Integer , Character > ( ) ; char digit = '0' ; char c = ' a ' ; for ( int i = 0 ; i <= 15 ; i ++ ) { if ( i < 10 ) { m . put ( i , digit ) ; digit ++ ; } else { m . put ( i , c ) ; c ++ ; } } String res = " " ; if ( num == 0 ) { return "0" ; } if ( num > 0 ) { while ( num != 0 ) { res = m . get ( num % 16 ) + res ; num /= 16 ; } } else { int n = num ; while ( n != 0 ) { res = m . get ( n % 16 ) + res ; n /= 16 ; } } return res ; } public static void main ( String [ ] args ) { int x = 134 , y = - 1 , z = - 234 ; System . out . println ( " Hexa ▁ representation ▁ for " ) ; System . out . println ( x + " ▁ is ▁ " + Hex ( x ) ) ; System . out . println ( y + " ▁ is ▁ " + Hex ( y ) ) ; System . out . println ( z + " ▁ is ▁ " + Hex ( z ) ) ; } }


"Convert String into Binary Sequence | Java program to convert string into binary string ; utility function ; convert each char to ASCII value ; Convert ASCII value to binary ; Utility reverse function ; Driver code"
How can the above be solved in Java?

import java . util . * ; class Node { static void strToBinary ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int val = Integer . valueOf ( s . charAt ( i ) ) ; String bin = " " ; while ( val > 0 ) { if ( val % 2 == 1 ) { bin += '1' ; } else bin += '0' ; val /= 2 ; } bin = reverse ( bin ) ; System . out . print ( bin + " ▁ " ) ; } } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = 0 ; r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String s = " geeks " ; strToBinary ( s ) ; } }


"Convert Ternary Expression to a Binary Tree | Java program to convert a ternary expreesion to a tree . ; Class to represent Tree node ; Class to convert a ternary expression to a Tree ; Function to convert Ternary Expression to a Binary Tree . It return the root of tree ; Base case ; store current character of expression_string [ ' a ' to ' z ' ] ; Move ahead in str ; if current character of ternary expression is ' ? ' then we add next character as a left child of current node ; else we have to add it as a right child of current node expression . at ( 0 ) == ' : ' ; function print tree ; Driver program to test above function"
How can the above be solved in Java?

import java . util . Queue ; import java . util . LinkedList ; class Node { char data ; Node left , right ; public Node ( char item ) { data = item ; left = null ; right = null ; } } class BinaryTree { Node convertExpression ( char [ ] expression , int i ) { if ( i >= expression . length ) return null ; Node root = new Node ( expression [ i ] ) ; ++ i ; if ( i < expression . length && expression [ i ] == ' ? ' ) root . left = convertExpression ( expression , i + 1 ) ; else if ( i < expression . length ) root . right = convertExpression ( expression , i + 1 ) ; return root ; } public void printTree ( Node root ) { if ( root == null ) return ; System . out . print ( root . data + " ▁ " ) ; printTree ( root . left ) ; printTree ( root . right ) ; } public static void main ( String args [ ] ) { String exp = " a ? b ? c : d : e " ; BinaryTree tree = new BinaryTree ( ) ; char [ ] expression = exp . toCharArray ( ) ; Node root = tree . convertExpression ( expression , 0 ) ; tree . printTree ( root ) ; } }


"Convert Unix timestamp to DD / MM / YYYY HH : MM : SS format | Unix time is in seconds and Humar Readable Format : DATE : MONTH : YEAR : HOUR : MINUTES : SECONDS , Start of unix time : 01 Jan 1970 , 00 : 00 : 00 ; Function to convert unix time to Human readable format ; Save the time in Human readable format ; Number of days in month in normal year ; Calculate total days unix time T ; Calculating current year ; Updating extradays because it will give days till previous day and we have include current day ; Calculating MONTH and DATE ; Current Month ; Calculating HH : MM : YYYY ; Return the time ; Driver Code ; Given unix time ; Function call to convert unix time to human read able ; Print time in format DD : MM : YYYY : HH : MM : SS"
How can the above be solved in Java?

 import java . util . * ; class GFG { static String unixTimeToHumanReadable ( int seconds ) { String ans = " " ; int daysOfMonth [ ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; int currYear , daysTillNow , extraTime , extraDays , index , date , month , hours , minutes , secondss , flag = 0 ; daysTillNow = seconds / ( 24 * 60 * 60 ) ; extraTime = seconds % ( 24 * 60 * 60 ) ; currYear = 1970 ; while ( daysTillNow >= 365 ) { if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) { daysTillNow -= 366 ; } else { daysTillNow -= 365 ; } currYear += 1 ; } extraDays = daysTillNow + 1 ; if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) flag = 1 ; month = 0 ; index = 0 ; if ( flag == 1 ) { while ( true ) { if ( index == 1 ) { if ( extraDays - 29 < 0 ) break ; month += 1 ; extraDays -= 29 ; } else { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; } index += 1 ; } } else { while ( true ) { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; index += 1 ; } } if ( extraDays > 0 ) { month += 1 ; date = extraDays ; } else { if ( month == 2 && flag == 1 ) date = 29 ; else { date = daysOfMonth [ month - 1 ] ; } } hours = extraTime / 3600 ; minutes = ( extraTime % 3600 ) / 60 ; secondss = ( extraTime % 3600 ) % 60 ; ans += String . valueOf ( date ) ; ans += " / " ; ans += String . valueOf ( month ) ; ans += " / " ; ans += String . valueOf ( currYear ) ; ans += " ▁ " ; ans += String . valueOf ( hours ) ; ans += " : " ; ans += String . valueOf ( minutes ) ; ans += " : " ; ans += String . valueOf ( secondss ) ; return ans ; } public static void main ( String [ ] args ) { int T = 1595497956 ; String ans = unixTimeToHumanReadable ( T ) ; System . out . print ( ans + "
"); } }


"Convert a Binary String to another by flipping prefixes minimum number of times | Java program for the above approach ; Function to count minimum number of operations required to convert string a to another string b ; Store the lengths of each prefixes selected ; Traverse the string ; If first character is same as b [ i ] ; Insert 1 to ops [ ] ; And , flip the bit ; Reverse the prefix string of length i + 1 ; Flip the characters in this prefix length ; Push ( i + 1 ) to array ops [ ] ; Print the number of operations ; Print the length of each prefixes stored ; Function to reverse a [ ] from start to end ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minOperations ( String s1 , String s2 , int n ) { char a [ ] = s1 . toCharArray ( ) ; char b [ ] = s2 . toCharArray ( ) ; ArrayList < Integer > ops = new ArrayList < > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] != b [ i ] ) { if ( a [ 0 ] == b [ i ] ) { ops . add ( 1 ) ; a [ 0 ] = ( a [ 0 ] == '0' ? '1' : '0' ) ; } reverse ( a , 0 , i ) ; for ( int j = 0 ; j <= i ; j ++ ) { a [ j ] = ( a [ j ] == '0' ? '1' : '0' ) ; } ops . add ( i + 1 ) ; } } System . out . println ( ops . size ( ) ) ; for ( int x : ops ) { System . out . print ( x + " ▁ " ) ; } } static void reverse ( char a [ ] , int start , int end ) { while ( start < end ) { char temp = a [ start ] ; a [ start ] = a [ end ] ; a [ end ] = temp ; start ++ ; end -- ; } } public static void main ( String [ ] args ) { String a = "10" , b = "01" ; int N = a . length ( ) ; minOperations ( a , b , N ) ; } }


"Convert a Binary Tree into Doubly Linked List in spiral fashion | Java program to convert Binary Tree into Doubly Linked List where the nodes are represented spirally ; A binary tree node ; Given a reference to a node , inserts the node on the front of the list . ; Make right of given node as head and left as NULL ; change left of head node to given node ; move the head to point to the given node ; Function to prints contents of DLL ; Function to print corner node at each level ; Base Case ; Create an empty deque for doing spiral level order traversal and enqueue root ; create a stack to store Binary Tree nodes to insert into DLL later ; nodeCount indicates number of Nodes at current level . ; Dequeue all Nodes of current level and Enqueue all Nodes of next level odd level ; dequeue node from front & push it to stack ; insert its left and right children in the back of the deque ; even level ; dequeue node from the back & push it to stack ; inserts its right and left children in the front of the deque ; pop all nodes from stack and push them in the beginning of the list ; Driver program to test above functions ; Let us create binary tree as shown in above diagram ; tree . root . right . left . left = new Node ( 12 ) ; ; tree . root . right . right . right = new Node ( 15 ) ;"
How can the above be solved in Java?

import java . util . * ; class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; Node head ; void push ( Node node ) { node . right = head ; node . left = null ; if ( head != null ) head . left = node ; head = node ; } void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . right ; } } void spiralLevelOrder ( Node root ) { if ( root == null ) return ; Deque < Node > q = new LinkedList < Node > ( ) ; q . addFirst ( root ) ; Stack < Node > stk = new Stack < Node > ( ) ; int level = 0 ; while ( ! q . isEmpty ( ) ) { int nodeCount = q . size ( ) ; if ( ( level & 1 ) % 2 != 0 ) { while ( nodeCount > 0 ) { Node node = q . peekFirst ( ) ; q . pollFirst ( ) ; stk . push ( node ) ; if ( node . left != null ) q . addLast ( node . left ) ; if ( node . right != null ) q . addLast ( node . right ) ; nodeCount -- ; } } else { while ( nodeCount > 0 ) { Node node = q . peekLast ( ) ; q . pollLast ( ) ; stk . push ( node ) ; if ( node . right != null ) q . addFirst ( node . right ) ; if ( node . left != null ) q . addFirst ( node . left ) ; nodeCount -- ; } } level ++ ; } while ( ! stk . empty ( ) ) { push ( stk . peek ( ) ) ; stk . pop ( ) ; } System . out . println ( " Created ▁ DLL ▁ is ▁ : ▁ " ) ; printList ( head ) ; } 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 . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . root . right . left . right = new Node ( 13 ) ; tree . root ....


"Convert a Binary Tree into its Mirror Tree | Class containing left and right child of current node and key value ; Change a tree so that the roles of the left and right pointers are swapped at every node . So the tree ... 4 / \ 2 5 / \ 1 3 is changed to ... 4 / \ 5 2 / \ 3 1 ; do the subtrees ; swap the left and right pointers ; Helper function to test mirror ( ) . Given a binary search tree , print out its data elements in increasing sorted order . ; testing for example nodes ; print inorder traversal of the input tree ; convert tree to its mirror ; print inorder traversal of the minor tree"
How can the above be solved in Java?

class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void mirror ( ) { root = mirror ( root ) ; } Node mirror ( Node node ) { if ( node == null ) return node ; Node left = mirror ( node . left ) ; Node right = mirror ( node . right ) ; node . left = right ; node . right = left ; return node ; } void inOrder ( ) { inOrder ( root ) ; } void inOrder ( Node node ) { if ( node == null ) return ; inOrder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; inOrder ( node . 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 ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ input ▁ tree ▁ is ▁ : " ) ; tree . inOrder ( ) ; System . out . println ( " " ) ; tree . mirror ( ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ : ▁ " ) ; tree . inOrder ( ) ; } }


"Convert a Binary Tree into its Mirror Tree | Iterative Java program to convert a Binary Tree to its mirror ; 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 . ; Change a tree so that the roles of the left and right pointers are swapped at every node . So the tree ... 4 / \ 2 5 / \ 1 3 is changed to ... 4 / \ 5 2 / \ 3 1 ; Do BFS . While doing BFS , keep swapping left and right children ; pop top node from queue ; swap left child with right child ; push left and right children ; Helper function to print Inorder traversal . ; Driver code ; Print inorder traversal of the input tree ; Convert tree to its mirror ; Print inorder traversal of the mirror tree"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void mirror ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { Node curr = q . peek ( ) ; q . remove ( ) ; Node temp = curr . left ; curr . left = curr . right ; curr . right = temp ; ; if ( curr . left != null ) q . add ( curr . left ) ; if ( curr . right != null ) q . add ( curr . right ) ; } } static void inOrder ( Node node ) { if ( node == null ) return ; inOrder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; inOrder ( node . 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 ) ; System . out . print ( " Inorder traversal of the " ▁ + " coned tree is "); inOrder ( root ) ; mirror ( root ) ; System . out . print ( " Inorder traversal of the " + ▁ " mirror tree is "); inOrder ( root ) ; } }


"Convert a Binary Tree to Threaded binary tree | Set 2 ( Efficient ) | Java program to convert a Binary Tree to Threaded Tree ; structure of a node in threaded binary tree ; Used to indicate whether the right pointer is a normal right pointer or a pointer to inorder successor . ; Converts tree with given root to threaded binary tree . This function returns rightmost child of root . ; Base cases : Tree is empty or has single node ; Find predecessor if it exists ; Find predecessor of root ( Rightmost child in left subtree ) ; Link a thread from predecessor to root . ; If current node is rightmost child ; Recur for right subtree . ; A utility function to find leftmost node in a binary tree rooted with ' root ' . This function is used in inOrder ( ) ; Function to do inorder traversal of a threadded binary tree ; Find the leftmost node in Binary Tree ; If this Node is a thread Node , then go to inorder successor ; Else go to the leftmost child in right subtree ; A utility function to create a new node ; Driver program to test above functions ; 1 / \ 2 3 / \ / \ 4 5 6 7"
How can the above be solved in Java?

 import java . util . * ; class solution { static class Node { int key ; Node left , right ; boolean isThreaded ; } ; static Node createThreaded ( Node root ) { if ( root == null ) return null ; if ( root . left == null && root . right == null ) return root ; if ( root . left != null ) { Node l = createThreaded ( root . left ) ; l . right = root ; l . isThreaded = true ; } if ( root . right == null ) return root ; return createThreaded ( root . right ) ; } static Node leftMost ( Node root ) { while ( root != null && root . left != null ) root = root . left ; return root ; } static void inOrder ( Node root ) { if ( root == null ) return ; Node cur = leftMost ( root ) ; while ( cur != null ) { System . out . print ( cur . key + " ▁ " ) ; if ( cur . isThreaded ) cur = cur . right ; else cur = leftMost ( cur . right ) ; } } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . left = temp . right = null ; temp . key = key ; return temp ; } 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 ( 6 ) ; root . right . right = newNode ( 7 ) ; createThreaded ( root ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ created ▁ " + "threaded tree is
"); inOrder ( root ) ; } }


"Convert a Matrix into another Matrix of given dimensions | Java program to implement the above approach ; Function to cona matrix of size A * B from the given matrix elements ; Initialize a new matrix ; Traverse the matrix , mat [ ] [ ] ; Update idx ; Print the resultant matrix ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static void ConstMatrix ( int [ ] [ ] mat , int N , int M , int A , int B ) { if ( N * M != A * B ) return ; int idx = 0 ; int [ ] [ ] res = new int [ A ] [ B ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { res [ idx / B ] [ idx % B ] = mat [ i ] [ j ] ; idx ++ ; } } for ( int i = 0 ; i < A ; i ++ ) { for ( int j = 0 ; j < B ; j ++ ) System . out . print ( res [ i ] [ j ] + " ▁ " ) ; System . out . print ( "
"); } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 , 6 } } ; int A = 2 ; int B = 3 ; int N = mat . length ; int M = mat [ 0 ] . length ; ConstMatrix ( mat , N , M , A , B ) ; } }


"Convert a Mobile Numeric Keypad sequence to equivalent sentence | java program for the above approach ; Function to convert mobile numeric keypad sequence into its equivalent string ; Store the mobile keypad mappings ; Traverse the string str ; If the current character is ' . ' , then continue to the next iteration ; Stores the number of continuous clicks ; Iterate a loop to find the count of same characters ; 2 , 3 , 4 , 5 , 6 and 8 keys will have maximum of 3 letters ; 7 and 9 keys will have maximum of 4 keys ; Handle the end condition ; Check if the current pressed key is 7 or 9 ; Else , the key pressed is either 2 , 3 , 4 , 5 , 6 or 8 ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void printSentence ( String S ) { String nums [ ] = { " " , " " , " ABC " , " DEF " , " GHI " , " JKL " , " MNO " , " PQRS " , " TUV " , " WXYZ " } ; char str [ ] = S . toCharArray ( ) ; int i = 0 ; while ( i < str . length ) { if ( str [ i ] == ' . ' ) { i ++ ; continue ; } int count = 0 ; while ( i + 1 < str . length && str [ i ] == str [ i + 1 ] ) { if ( count == 2 && ( ( str [ i ] >= '2' && str [ i ] <= '6' ) || ( str [ i ] == '8' ) ) ) break ; else if ( count == 3 && ( str [ i ] == '7' str [ i ] == '9' ) ) break ; count ++ ; i ++ ; if ( i == str . length ) break ; } if ( str [ i ] == '7' str [ i ] == '9' ) { System . out . print ( nums [ str [ i ] - 48 ] . charAt ( count % 4 ) ) ; } else { System . out . print ( nums [ str [ i ] - 48 ] . charAt ( count % 3 ) ) ; } i ++ ; } } public static void main ( String [ ] args ) { String str = "234" ; printSentence ( str ) ; } }


"Convert a String to a Singly Linked List | Java program to Convert a String to a Singly Linked List ; Structure for a Singly Linked List ; Function to add a new node to the Linked List ; Function to convert the string to Linked List . ; curr pointer points to the current node where the insertion should take place ; Function to print the data present in all the nodes ; Driver code"
How can the above be solved in Java?

class GFG { static class node { char data ; node next ; } ; static node add ( char data ) { node newnode = new node ( ) ; newnode . data = data ; newnode . next = null ; return newnode ; } static node string_to_SLL ( String text , node head ) { head = add ( text . charAt ( 0 ) ) ; node curr = head ; for ( int i = 1 ; i < text . length ( ) ; i ++ ) { curr . next = add ( text . charAt ( i ) ) ; curr = curr . next ; } return head ; } static void print ( node head ) { node curr = head ; while ( curr != null ) { System . out . print ( curr . data + " ▁ - > ▁ " ) ; curr = curr . next ; } } public static void main ( String [ ] args ) { String text = " GEEKS " ; node head = null ; head = string_to_SLL ( text , head ) ; print ( head ) ; } }


"Convert a binary number to hexadecimal number | Java implementation to convert a binary number to hexadecimal number ; Function to create map between binary number and its equivalent hexadecimal ; Function to find hexadecimal equivalent of binary ; Length of string before ' . ' ; Add min 0 's in the beginning to make  left substring length divisible by 4 ; If decimal point exists ; Length of string after ' . ' ; Add min 0 's in the end to make right  substring length divisible by 4 ; Create map between binary and its equivalent hex code ; One by one extract from left , substring of size 4 and add its hex code ; If ' . ' is encountered add it to result ; Required hexadecimal number ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void createMap ( Map < String , Character > um ) { um . put ( "0000" , '0' ) ; um . put ( "0001" , '1' ) ; um . put ( "0010" , '2' ) ; um . put ( "0011" , '3' ) ; um . put ( "0100" , '4' ) ; um . put ( "0101" , '5' ) ; um . put ( "0110" , '6' ) ; um . put ( "0111" , '7' ) ; um . put ( "1000" , '8' ) ; um . put ( "1001" , '9' ) ; um . put ( "1010" , ' A ' ) ; um . put ( "1011" , ' B ' ) ; um . put ( "1100" , ' C ' ) ; um . put ( "1101" , ' D ' ) ; um . put ( "1110" , ' E ' ) ; um . put ( "1111" , ' F ' ) ; } static String convertBinToHex ( String bin ) { int l = bin . length ( ) ; int t = bin . indexOf ( ' . ' ) ; int len_left = t != - 1 ? t : l ; for ( int i = 1 ; i <= ( 4 - len_left % 4 ) % 4 ; i ++ ) bin = '0' + bin ; if ( t != - 1 ) { int len_right = l - len_left - 1 ; for ( int i = 1 ; i <= ( 4 - len_right % 4 ) % 4 ; i ++ ) bin = bin + '0' ; } Map < String , Character > bin_hex_map = new HashMap < String , Character > ( ) ; createMap ( bin_hex_map ) ; int i = 0 ; String hex = " " ; while ( true ) { hex += bin_hex_map . get ( bin . substring ( i , i + 4 ) ) ; i += 4 ; if ( i == bin . length ( ) ) break ; if ( bin . charAt ( i ) == ' . ' ) { hex += ' . ' ; i ++ ; } } return hex ; } public static void main ( String [ ] args ) { String bin = "1111001010010100001.010110110011011" ; System . out . print ( " Hexadecimal ▁ number ▁ = ▁ " + convertBinToHex ( bin ) ) ; } }


"Convert a binary number to octal | Java implementation to convert a binary number to octal number ; Function to create map between binary number and its equivalent hexadecimal ; Function to find octal equivalent of binary ; Length of string before ' . ' ; Add min 0 's in the beginning to make  left substring length divisible by 3 ; If decimal point exists ; Length of string after ' . ' ; add min 0 's in the end to make right  substring length divisible by 3 ; Create map between binary and its equivalent octal code ; One by one extract from left , substring of size 3 and add its octal code ; If ' . ' is encountered add it to result ; Required octal number ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void createMap ( Map < String , Character > um ) { um . put ( "000" , '0' ) ; um . put ( "001" , '1' ) ; um . put ( "010" , '2' ) ; um . put ( "011" , '3' ) ; um . put ( "100" , '4' ) ; um . put ( "101" , '5' ) ; um . put ( "110" , '6' ) ; um . put ( "111" , '7' ) ; } static String convertBinToOct ( String bin ) { int l = bin . length ( ) ; int t = bin . indexOf ( ' . ' ) ; int len_left = t != - 1 ? t : l ; for ( int i = 1 ; i <= ( 3 - len_left % 3 ) % 3 ; i ++ ) bin = '0' + bin ; if ( t != - 1 ) { int len_right = l - len_left - 1 ; for ( int i = 1 ; i <= ( 3 - len_right % 3 ) % 3 ; i ++ ) bin = bin + '0' ; } Map < String , Character > bin_oct_map = new HashMap < String , Character > ( ) ; createMap ( bin_oct_map ) ; int i = 0 ; String octal = " " ; while ( true ) { octal += bin_oct_map . get ( bin . substring ( i , i + 3 ) ) ; i += 3 ; if ( i == bin . length ( ) ) break ; if ( bin . charAt ( i ) == ' . ' ) { octal += ' . ' ; i ++ ; } } return octal ; } public static void main ( String [ ] args ) { String bin = "1111001010010100001.010110110011011" ; System . out . println ( " Octal ▁ number ▁ = ▁ " + convertBinToOct ( bin ) ) ; } }


"Convert a given Binary Tree to Circular Doubly Linked List | Set 2 | Java program for conversion of Binary Tree to CDLL ; A binary tree node has data , and left and right pointers ; Function to perform In - Order traversal of the tree and store the nodes in a vector ; first recur on left child ; append the data of node in vector ; now recur on right child ; Function to convert Binary Tree to Circular Doubly Linked list using the vector which stores In - Order traversal of the Binary Tree ; Base cases ; Vector to be used for storing the nodes of tree in In - order form ; Calling the In - Order traversal function ; Create the head of the linked list pointing to the root of the tree ; Create a current pointer to be used in traversal ; Traversing the nodes of the tree starting from the second elements ; Create a temporary pointer pointing to current ; Current 's right points to the current  node in traversal ; Current points to its right ; Current 's left points to temp ; Current 's right points to head of the list ; Head 's left points to current ; Return head of the list ; Display Circular Link List ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; Node ( int x ) { data = x ; left = right = null ; } } ; static void inorder ( Node root , Vector < Integer > v ) { if ( root == null ) return ; inorder ( root . left , v ) ; v . add ( root . data ) ; inorder ( root . right , v ) ; } static Node bTreeToCList ( Node root ) { if ( root == null ) return null ; Vector < Integer > v = new Vector < > ( ) ; inorder ( root , v ) ; Node head_ref = new Node ( v . get ( 0 ) ) ; Node curr = head_ref ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { Node temp = curr ; curr . right = new Node ( v . get ( i ) ) ; curr = curr . right ; curr . left = temp ; } curr . right = head_ref ; head_ref . left = curr ; return head_ref ; } static void displayCList ( Node head ) { System . out . println ( " Circular ▁ Doubly ▁ Linked ▁ List ▁ is ▁ : " ) ; Node itr = head ; do { System . out . print ( itr . data + " ▁ " ) ; itr = itr . right ; } while ( head != itr ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 10 ) ; root . left = new Node ( 12 ) ; root . right = new Node ( 15 ) ; root . left . left = new Node ( 25 ) ; root . left . right = new Node ( 30 ) ; root . right . left = new Node ( 36 ) ; Node head = bTreeToCList ( root ) ; displayCList ( head ) ; } }


"Convert a given Binary Tree to Doubly Linked List | Set 1 | A binary tree node has data , and left and right pointers ; This is the core function to convert Tree to list . This function follows steps 1 and 2 of the above algorithm ; Base case ; Convert the left subtree and link to root ; Convert the left subtree ; Find inorder predecessor . After this loop , left will point to the inorder predecessor ; Make root as next of the predecessor ; Make predecssor as previous of root ; Convert the right subtree and link to root ; Convert the right subtree ; Find inorder successor . After this loop , right will point to the inorder successor ; Make root as previous of successor ; Make successor as next of root ; The main function that first calls bintree2listUtil ( ) , then follows step 3 of the above algorithm ; Base case ; Convert to DLL using bintree2listUtil ( ) ; bintree2listUtil ( ) returns root node of the converted DLL . We need pointer to the leftmost node which is head of the constructed DLL , so move to the leftmost node ; Function to print nodes in a given doubly linked list ; Driver program to test above functions ; Let us create the tree shown in above diagram ; Convert to DLL ; Print the converted list"
How can the above be solved in Java?

class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; Node bintree2listUtil ( Node node ) { if ( node == null ) return node ; if ( node . left != null ) { Node left = bintree2listUtil ( node . left ) ; for ( ; left . right != null ; left = left . right ) ; left . right = node ; node . left = left ; } if ( node . right != null ) { Node right = bintree2listUtil ( node . right ) ; for ( ; right . left != null ; right = right . left ) ; right . left = node ; node . right = right ; } return node ; } Node bintree2list ( Node node ) { if ( node == null ) return node ; node = bintree2listUtil ( node ) ; while ( node . left != null ) node = node . left ; return node ; } void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . right ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 12 ) ; tree . root . right = new Node ( 15 ) ; tree . root . left . left = new Node ( 25 ) ; tree . root . left . right = new Node ( 30 ) ; tree . root . right . left = new Node ( 36 ) ; Node head = tree . bintree2list ( tree . root ) ; tree . printList ( head ) ; } }


"Convert a given Binary Tree to Doubly Linked List | Set 2 | Java program to convert BTT to DLL using simple inorder traversal ; A tree node ; Standard Inorder traversal of tree ; Changes left pointers to work as previous pointers in converted DLL The function simply does inorder traversal of Binary Tree and updates left pointer using previously visited node ; Changes right pointers to work as next pointers in converted DLL ; Find the right most node in BT or last node in DLL ; Start from the rightmost node , traverse back using left pointers . While traversing , change right pointer of nodes ; The leftmost node is head of linked list , return it ; The main function that converts BST to DLL and returns head of DLL ; Set the previous pointer ; Set the next pointer and return head of DLL ; Traverses the DLL from left tor right ; Driver code ; Let us create the tree shown in above diagram"
How can the above be solved in Java?

public class BinaryTreeToDLL { static class node { int data ; node left , right ; public node ( int data ) { this . data = data ; } } static node prev ; static void inorder ( node root ) { if ( root == null ) return ; inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } static void fixPrevptr ( node root ) { if ( root == null ) return ; fixPrevptr ( root . left ) ; root . left = prev ; prev = root ; fixPrevptr ( root . right ) ; } static node fixNextptr ( node root ) { while ( root . right != null ) root = root . right ; while ( root != null && root . left != null ) { node left = root . left ; left . right = root ; root = root . left ; } return root ; } static node BTTtoDLL ( node root ) { prev = null ; fixPrevptr ( root ) ; return fixNextptr ( root ) ; } static void printlist ( node root ) { while ( root != null ) { System . out . print ( root . data + " ▁ " ) ; root = root . right ; } } public static void main ( String [ ] args ) { node root = new node ( 10 ) ; root . left = new node ( 12 ) ; root . right = new node ( 15 ) ; root . left . left = new node ( 25 ) ; root . left . right = new node ( 30 ) ; root . right . left = new node ( 36 ) ; System . out . println ( " Inorder ▁ Tree ▁ Traversal " ) ; inorder ( root ) ; node head = BTTtoDLL ( root ) ; System . out . println ( " DLL Traversal "); printlist ( head ) ; } }


"Convert a given Binary Tree to Doubly Linked List | Set 3 | A binary tree node has data , left pointers and right pointers ; head -- > Pointer to head node of created doubly linked list ; A simple recursive function to convert a given Binary tree to Doubly Linked List root -- > Root of Binary Tree ; Base case ; Initialize previously visited node as NULL . This is static so that the same value is accessible in all recursive calls ; Recursively convert left subtree ; Now convert this node ; Finally convert right subtree ; Function to print nodes in a given doubly linked list ; Driver program to test above functions ; Let us create the tree as shown in above diagram ; convert to DLL ; Print the converted List"
How can the above be solved in Java?

class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; Node head ; void BinaryTree2DoubleLinkedList ( Node root ) { if ( root == null ) return ; static Node prev = null ; BinaryTree2DoubleLinkedList ( root . left ) ; if ( prev == null ) head = root ; else { root . left = prev ; prev . right = root ; } prev = root ; BinaryTree2DoubleLinkedList ( root . right ) ; } void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . right ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 12 ) ; tree . root . right = new Node ( 15 ) ; tree . root . left . left = new Node ( 25 ) ; tree . root . left . right = new Node ( 30 ) ; tree . root . right . left = new Node ( 36 ) ; tree . BinaryTree2DoubleLinkedList ( tree . root ) ; tree . printList ( tree . head ) ; } }


"Convert a given Binary tree to a tree that holds Logical AND property | Java code to convert a given binary tree to a tree that holds logical AND property . ; Structure of binary tree ; function to create a new node ; Convert the given tree to a tree where each node is logical AND of its children The main idea is to do Postorder traversal ; first recur on left child ; then recur on right child ; first recur on left child ; then print the data of node ; now recur on right child ; main function ; Create following Binary Tree 1 / \ 1 0 / \ / \ 0 1 1 1"
How can the above be solved in Java?

class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int key ) { Node node = new Node ( ) ; node . data = key ; node . left = null ; node . right = null ; return node ; } static void convertTree ( Node root ) { if ( root == null ) return ; convertTree ( root . left ) ; convertTree ( root . right ) ; if ( root . left != null && root . right != null ) root . data = ( root . left . data ) & ( root . right . data ) ; } static void printInorder ( Node root ) { if ( root == null ) return ; printInorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; printInorder ( root . right ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 0 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 0 ) ; root . left . left = newNode ( 0 ) ; root . left . right = newNode ( 1 ) ; root . right . left = newNode ( 1 ) ; root . right . right = newNode ( 1 ) ; System . out . print ( " Inorder ▁ traversal ▁ before ▁ conversion ▁ " ) ; printInorder ( root ) ; convertTree ( root ) ; System . out . println ( ) ; System . out . print ( " Inorder ▁ traversal ▁ after ▁ conversion ▁ " ) ; printInorder ( root ) ; } }


"Convert a given temperature to another system based on given boiling and freezing points | Java program for above approach ; Function to return temperature in the second thermometer ; Calculate the temperature ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static double temp_convert ( int F1 , int B1 , int F2 , int B2 , int T ) { float t2 ; t2 = F2 + ( float ) ( B2 - F2 ) / ( B1 - F1 ) * ( T - F1 ) ; return t2 ; } public static void main ( String [ ] args ) { int F1 = 0 , B1 = 100 ; int F2 = 32 , B2 = 212 ; int T = 37 ; float t2 ; System . out . printf ( " % .2f " , temp_convert ( F1 , B1 , F2 , B2 , T ) ) ; } }


"Convert a given tree to its Sum Tree | A binary tree node ; Convert a given tree to a tree where every node contains sum of values of nodes in left and right subtrees in the original tree ; Base case ; Store the old value ; Recursively call for left and right subtrees and store the sum as new value of this node ; Return the sum of values of nodes in left and right subtrees and old_value of this node ; A utility function to print inorder traversal of a Binary Tree ; Driver function to test above functions ; Constructing tree given in the above figure ; Print inorder traversal of the converted tree to test result of toSumTree ( )"
How can the above be solved in Java?

class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int toSumTree ( Node node ) { if ( node == null ) return 0 ; int old_val = node . data ; node . data = toSumTree ( node . left ) + toSumTree ( node . right ) ; return node . data + old_val ; } 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 [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( - 2 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( - 4 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 5 ) ; tree . toSumTree ( tree . root ) ; System . out . println ( " Inorder ▁ Traversal ▁ of ▁ the ▁ resultant ▁ tree ▁ is : " ) ; tree . printInorder ( tree . root ) ; } }


"Convert a normal BST to Balanced BST | Java program to convert a left unbalanced BST to a balanced BST ; A binary tree node has data , pointer to left child and a pointer to right child ; This function traverse the skewed binary tree and stores its nodes pointers in vector nodes [ ] ; Base case ; Store nodes in Inorder ( which is sorted order for BST ) ; Recursive function to construct binary tree ; base case ; Get the middle element and make it root ; Using index in Inorder traversal , construct left and right subtress ; This functions converts an unbalanced BST to a balanced BST ; Store nodes of given BST in sorted order ; Constucts BST from nodes [ ] ; Function to do preorder traversal of tree ; Driver program to test the above functions ; Constructed skewed binary tree is 10 / 8 / 7 / 6 / 5"
How can the above be solved in Java?

import java . util . * ; class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; void storeBSTNodes ( Node root , Vector < Node > nodes ) { if ( root == null ) return ; storeBSTNodes ( root . left , nodes ) ; nodes . add ( root ) ; storeBSTNodes ( root . right , nodes ) ; } Node buildTreeUtil ( Vector < Node > nodes , int start , int end ) { if ( start > end ) return null ; int mid = ( start + end ) / 2 ; Node node = nodes . get ( mid ) ; node . left = buildTreeUtil ( nodes , start , mid - 1 ) ; node . right = buildTreeUtil ( nodes , mid + 1 , end ) ; return node ; } Node buildTree ( Node root ) { Vector < Node > nodes = new Vector < Node > ( ) ; storeBSTNodes ( root , nodes ) ; int n = nodes . size ( ) ; return buildTreeUtil ( nodes , 0 , n - 1 ) ; } void preOrder ( Node node ) { if ( node == null ) return ; System . out . print ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . left . left = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 6 ) ; tree . root . left . left . left . left = new Node ( 5 ) ; tree . root = tree . buildTree ( tree . root ) ; System . out . println ( " Preorder ▁ traversal ▁ of ▁ balanced ▁ BST ▁ is ▁ : " ) ; tree . preOrder ( tree . root ) ; } }


"Convert a number from base A to base B | Java program for the above approach ; Function to return ASCII value of a character ; Function to convert a number from given base to decimal number ; Stores the length of the String ; Initialize power of base ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Update num ; Update power ; Function to return equivalent character of a given value ; Function to convert a given decimal number to a given base ; Store the result ; Repeatedly divide inputNum by base and take remainder ; Update res ; Update inputNum ; Reverse the result ; Function to convert a given number from a base to another base ; Convert the number from base A to decimal ; Convert the number from decimal to base B ; Print the result ; Driver Code ; Given input ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( String str , int base ) { int len = str . length ( ) ; int power = 1 ; int num = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str . charAt ( i ) ) >= base ) { System . out . printf ( " Invalid ▁ Number " ) ; return - 1 ; } num += val ( str . charAt ( i ) ) * power ; power = power * base ; } return num ; } static char reVal ( int num ) { if ( num >= 0 && num <= 9 ) return ( char ) ( num + '0' ) ; else return ( char ) ( num - 10 + ' A ' ) ; } static String fromDeci ( int base , int inputNum ) { String res = " " ; while ( inputNum > 0 ) { res += reVal ( inputNum % base ) ; inputNum /= base ; } res = reverse ( res ) ; return res ; } static void convertBase ( String s , int a , int b ) { int num = toDeci ( s , a ) ; String ans = fromDeci ( b , num ) ; System . out . print ( ans ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String s = "10B " ; int a = 16 , b = 10 ; convertBase ( s , a , b ) ; } }


"Convert a number to another by dividing by its factor or removing first occurrence of a digit from an array | Java program for the above approach ; Function to check if a digit x is present in the number N or not ; Convert N to string ; Traverse the string num ; Return first occurrence of the digit x ; Function to remove the character at a given index from the number ; Convert N to string ; Store the resultant string ; Traverse the string num ; If the number becomes empty after deletion , then return - 1 ; Return the number ; Function to check if A can be reduced to B by performing the operations any number of times ; Create a queue ; Push A into the queue ; Hashmap to check if the element is present in the Queue or not ; Set A as visited ; Iterate while the queue is not empty ; Store the front value of the queue and pop it from it ; If top is equal to B , then return true ; Traverse the array , D [ ] ; Divide top by D [ i ] if it is possible and push the result in q ; If D [ i ] is present at the top ; Remove the first occurrence of D [ i ] from the top and store the new number ; Push newElement into the queue q ; Return false if A can not be reduced to B ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int isPresent ( int n , int x ) { String num = String . valueOf ( n ) ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { if ( ( num . charAt ( i ) - '0' ) == x ) return i ; } return - 1 ; } static int removeDigit ( int n , int index ) { String num = String . valueOf ( n ) ; String ans = " " ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { if ( i != index ) ans += num . charAt ( i ) ; } if ( ans == " " || ( ans . length ( ) == 1 && ans . charAt ( 0 ) == '0' ) ) return - 1 ; int x = Integer . valueOf ( ans ) ; return x ; } static boolean reduceNtoX ( int a , int b , int d [ ] , int n ) { Queue < Integer > q = new LinkedList < > ( ) ; q . add ( a ) ; Map < Integer , Boolean > visited = new HashMap < > ( ) ; visited . put ( a , true ) ; while ( ! q . isEmpty ( ) ) { int top = q . peek ( ) ; q . poll ( ) ; if ( top < 0 ) continue ; if ( top == b ) return true ; for ( int i = 0 ; i < n ; i ++ ) { if ( d [ i ] != 0 && top % d [ i ] == 0 && ! visited . getOrDefault ( top / d [ i ] , false ) ) { q . add ( top / d [ i ] ) ; visited . put ( top / d [ i ] , true ) ; } int index = isPresent ( top , d [ i ] ) ; if ( index != - 1 ) { int newElement = removeDigit ( top , index ) ; if ( newElement != - 1 && ( ! visited . getOrDefault ( newElement , false ) ) ) { q . add ( newElement ) ; visited . put ( newElement , true ) ; } } } } return false ; } public static void main ( String [ ] args ) { int A = 5643 , B = 81 ; int D [ ] = { 3 , 8 , 1 } ; int N = D . length ; if ( reduceNtoX ( A , B , D , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Convert a sentence into its equivalent mobile numeric keypad sequence | Java implementation to convert a sentence into its equivalent mobile numeric keypad sequence ; Function which computes the sequence ; length of input string ; Checking for space ; Calculating index for each character ; Output sequence ; Driver Function ; storing the sequence in array"
How can the above be solved in Java?

import java . util . * ; class GFG { static String printSequence ( String arr [ ] , String input ) { String output = " " ; int n = input . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( input . charAt ( i ) == ' ▁ ' ) output = output + "0" ; else { int position = input . charAt ( i ) - ' A ' ; output = output + arr [ position ] ; } } return output ; } public static void main ( String [ ] args ) { String str [ ] = { "2" , "22" , "222" , "3" , "33" , "333" , "4" , "44" , "444" , "5" , "55" , "555" , "6" , "66" , "666" , "7" , "77" , "777" , "7777" , "8" , "88" , "888" , "9" , "99" , "999" , "9999" } ; String input = " GEEKSFORGEEKS " ; System . out . println ( printSequence ( str , input ) ) ; } }


"Convert a tree to forest of even nodes | Java program to find maximum number to be removed to convert a tree into forest containing trees of even number of nodes ; Return the number of nodes of subtree having node as a root . ; Mark node as visited . ; Traverse the adjacency list to find non - visited node . ; Finding number of nodes of the subtree of a subtree . ; If nodes are even , increment number of edges to removed . Else leave the node as child of subtree . ; Return the maximum number of edge to remove to make forest . ; Driven Program"
How can the above be solved in Java?

import java . util . * ; class GFG { static int N = 12 , ans ; static Vector < Vector < Integer > > tree = new Vector < Vector < Integer > > ( ) ; static int dfs ( int visit [ ] , int node ) { int num = 0 , temp = 0 ; visit [ node ] = 1 ; for ( int i = 0 ; i < tree . get ( node ) . size ( ) ; i ++ ) { if ( visit [ tree . get ( node ) . get ( i ) ] == 0 ) { temp = dfs ( visit , tree . get ( node ) . get ( i ) ) ; if ( temp % 2 != 0 ) num += temp ; else ans ++ ; } } return num + 1 ; } static int minEdge ( int n ) { int visit [ ] = new int [ n + 2 ] ; ans = 0 ; dfs ( visit , 1 ) ; return ans ; } public static void main ( String args [ ] ) { int n = 10 ; for ( int i = 0 ; i < n + 2 ; i ++ ) tree . add ( new Vector < Integer > ( ) ) ; tree . get ( 1 ) . add ( 3 ) ; tree . get ( 3 ) . add ( 1 ) ; tree . get ( 1 ) . add ( 6 ) ; tree . get ( 6 ) . add ( 1 ) ; tree . get ( 1 ) . add ( 2 ) ; tree . get ( 2 ) . add ( 1 ) ; tree . get ( 3 ) . add ( 4 ) ; tree . get ( 4 ) . add ( 3 ) ; tree . get ( 6 ) . add ( 8 ) ; tree . get ( 8 ) . add ( 6 ) ; tree . get ( 2 ) . add ( 7 ) ; tree . get ( 7 ) . add ( 2 ) ; tree . get ( 2 ) . add ( 5 ) ; tree . get ( 5 ) . add ( 2 ) ; tree . get ( 4 ) . add ( 9 ) ; tree . get ( 9 ) . add ( 4 ) ; tree . get ( 4 ) . add ( 10 ) ; tree . get ( 10 ) . add ( 4 ) ; System . out . println ( minEdge ( n ) ) ; } }


"Convert all substrings of length ' k ' from base ' b ' to decimal | Simple Java program to convert all substrings from decimal to given base . ; Saving substring in sub ; Evaluating decimal for current substring and printing it . ; Driver code"
How can the above be solved in Java?

class GFG { static void substringConversions ( String str , int k , int b ) { for ( int i = 0 ; i + k <= str . length ( ) ; i ++ ) { String sub = str . substring ( i , i + k ) ; int sum = 0 , counter = 0 ; for ( int j = sub . length ( ) - 1 ; j >= 0 ; j -- ) { sum = ( int ) ( sum + ( ( sub . charAt ( j ) - '0' ) * Math . pow ( b , counter ) ) ) ; counter ++ ; } System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { String str = "12212" ; int b = 3 , k = 3 ; substringConversions ( str , b , k ) ; } }


"Convert an Array to a Circular Doubly Linked List | Java program to convert array to circular doubly linked list ; Doubly linked list node ; Utility function to create a node in memory ; Function to display the list ; Function to convert array into list ; Declare newNode and temporary pointer ; Iterate the loop until array length ; Create new node ; Assign the array data ; If it is first element Put that node prev and next as start as it is circular ; Find the last node ; Add the last node to make them in circular fashion ; Driver Code ; Array to be converted ; Start Pointer ; Create the List ; Display the list"
How can the above be solved in Java?

class GFG { static class node { int data ; node next ; node prev ; } ; static node getNode ( ) { return new node ( ) ; } static int displayList ( node temp ) { node t = temp ; if ( temp == null ) return 0 ; else { System . out . print ( " The ▁ list ▁ is : ▁ " ) ; while ( temp . next != t ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } System . out . print ( temp . data ) ; return 1 ; } } static node createList ( int arr [ ] , int n , node start ) { node newNode , temp ; int i ; for ( i = 0 ; i < n ; i ++ ) { newNode = getNode ( ) ; newNode . data = arr [ i ] ; if ( i == 0 ) { start = newNode ; newNode . prev = start ; newNode . next = start ; } else { temp = ( start ) . prev ; temp . next = newNode ; newNode . next = start ; newNode . prev = temp ; temp = start ; temp . prev = newNode ; } } return start ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; node start = null ; start = createList ( arr , n , start ) ; displayList ( start ) ; } }


"Convert an arbitrary Binary Tree to a tree that holds Children Sum Property | A binary tree node ; This function changes a tree to hold children sum property ; If tree is empty or it 's a leaf node then  return true  ; convert left and right subtrees ; If left child is not present then 0 is used as data of left child ; If right child is not present then 0 is used as data of right child ; get the diff of node 's data and children sum  ; If node ' s ▁ children ▁ sum ▁ is ▁ greater ▁ than ▁ the ▁ node ' s data ; THIS IS TRICKY -- > If node 's data is greater than children  sum, then increment subtree by diff  ; - diff is used to make diff positive ; This function is used to increment subtree by diff ; IF left child is not NULL then increment it ; Recursively call to fix the descendants of node -> left ; Else increment right child ; Recursively call to fix the descendants of node -> right ; Given a binary tree , printInorder ( ) prints out its inorder traversal ; first recur on left child ; then print the data of node ; now recur on right child ; Driver program to test above functions"
How can the above be solved in Java?

class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void convertTree ( Node node ) { int left_data = 0 , right_data = 0 , diff ; if ( node == null || ( node . left == null && node . right == null ) ) return ; else { convertTree ( node . left ) ; convertTree ( node . right ) ; if ( node . left != null ) left_data = node . left . data ; if ( node . right != null ) right_data = node . right . data ; diff = left_data + right_data - node . data ; if ( diff > 0 ) node . data = node . data + diff ; if ( diff < 0 ) increment ( node , - diff ) ; } } void increment ( Node node , int diff ) { if ( node . left != null ) { node . left . data = node . left . data + diff ; increment ( node . left , diff ) ; } else if ( node . right != null ) { node . right . data = node . right . data + diff ; increment ( node . right , diff ) ; } } 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 [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 50 ) ; tree . root . left = new Node ( 7 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 1 ) ; tree . root . right . right = new Node ( 30 ) ; System . out . println ( " Inorder ▁ traversal ▁ before ▁ conversion ▁ is ▁ : " ) ; tree . printInorder ( tree . root ) ; tree . convertTree ( tree . root ) ; System . out . println ( " " ) ; System . out . println ( " Inorder ▁ traversal ▁ after ▁ conversion ▁ is ▁ : " ) ; tree . printInorder ( tree . root ) ; } }


"Convert an array into another by repeatedly removing the last element and placing it at any arbitrary index | Java program for the above approach ; Function to count the minimum number of operations required to convert the array A [ ] into array B [ ] ; Stores the index in the first permutation A [ ] which is same as the subsequence in B [ ] ; Find the first i elements in A [ ] which is a subsequence in B [ ] ; If element A [ i ] is same as B [ j ] ; Return the count of operations required ; Driver Code"
How can the above be solved in Java?

class GFG { static int minCount ( int A [ ] , int B [ ] , int N ) { int i = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] == B [ j ] ) { i ++ ; } } return N - i ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 } ; int B [ ] = { 1 , 5 , 2 , 3 , 4 } ; int N = A . length ; System . out . println ( minCount ( A , B , N ) ) ; } }


"Convert an array to reduced form | Set 1 ( Simple and Hashing ) | Java Program to convert an Array to reduced form ; Create a temp array and copy contents of arr [ ] to temp ; Sort temp array ; Create a hash table . ; One by one insert elements of sorted temp [ ] and assign them values from 0 to n - 1 ; Convert array by taking positions from umap ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { public static void convert ( int arr [ ] , int n ) { int temp [ ] = arr . clone ( ) ; Arrays . sort ( temp ) ; HashMap < Integer , Integer > umap = new HashMap < > ( ) ; int val = 0 ; for ( int i = 0 ; i < n ; i ++ ) umap . put ( temp [ i ] , val ++ ) ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = umap . get ( arr [ i ] ) ; } public static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 15 , 12 , 11 , 50 } ; int n = arr . length ; System . out . println ( " Given ▁ Array ▁ is ▁ " ) ; printArr ( arr , n ) ; convert ( arr , n ) ; System . out . println ( " Converted Array is "); printArr ( arr , n ) ; } }


"Convert an array to reduced form | Set 3 ( Binary Search ) | Java program for the above approach ; Function to find the reduced form of the given array arr [ ] ; Stores the sorted form of the the given array arr [ ] ; Sort the array brr [ ] ; Traverse the given array arr [ ] ; Perform the Binary Search ; Calculate the value of mid ; Print the current index and break ; Update the value of l ; Update the value of r ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void convert ( int arr [ ] , int n ) { int brr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; Arrays . sort ( brr ) ; for ( int i = 0 ; i < n ; i ++ ) { int l = 0 , r = n - 1 , mid ; while ( l <= r ) { mid = ( l + r ) / 2 ; if ( brr [ mid ] == arr [ i ] ) { System . out . print ( mid + " ▁ " ) ; break ; } else if ( brr [ mid ] < arr [ i ] ) { l = mid + 1 ; } else { r = mid - 1 ; } } } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 15 , 12 , 11 , 50 } ; int N = arr . length ; convert ( arr , N ) ; } }


"Convert an unbalanced bracket sequence to a balanced sequence | Java implementation of the approach ; Function to return balancedBrackets string ; Initializing dep to 0 ; Stores maximum negative depth ; if dep is less than minDep ; if minDep is less than 0 then there is need to add ' ( ' at the front ; Reinitializing to check the updated string ; if dep is not 0 then there is need to add ' ) ' at the back ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String balancedBrackets ( String str ) { int dep = 0 ; int minDep = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) dep ++ ; else dep -- ; if ( minDep > dep ) minDep = dep ; } if ( minDep < 0 ) { for ( int i = 0 ; i < Math . abs ( minDep ) ; i ++ ) str = ' ( ' + str ; } dep = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) dep ++ ; else dep -- ; } if ( dep != 0 ) { for ( int i = 0 ; i < dep ; i ++ ) str = str + ' ) ' ; } return str ; } public static void main ( String [ ] args ) { String str = " ) ) ) ( ) " ; System . out . println ( balancedBrackets ( str ) ) ; } }


"Convert characters of a string to opposite case | Java program to toggle all characters ; Function to toggle characters ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static char [ ] S = " GeKf @ rGeek $ " . toCharArray ( ) ; static void toggleChars ( ) { for ( int i = 0 ; i < S . length ; i ++ ) { if ( Character . isAlphabetic ( S [ i ] ) ) { S [ i ] ^= ( 1 << 5 ) ; } } } public static void main ( String [ ] args ) { toggleChars ( ) ; System . out . print ( " String ▁ after ▁ toggle ▁ " + "
"); System . out . print ( String . valueOf ( S ) ) ; } }


"Convert decimal fraction to binary number | Java program to convert fractional decimal to binary number ; Function to convert decimal to binary upto k - precision after decimal point ; Fetch the integral part of decimal number ; Fetch the fractional part decimal number ; Conversion of integral part to binary equivalent ; Append 0 in binary ; Reverse string to get original binary equivalent ; Append point before conversion of fractional part ; Conversion of fractional part to binary equivalent ; Find next bit in fraction ; Swap values of left and right ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String decimalToBinary ( double num , int k_prec ) { String binary = " " ; int Integral = ( int ) num ; double fractional = num - Integral ; while ( Integral > 0 ) { int rem = Integral % 2 ; binary += ( ( char ) ( rem + '0' ) ) ; Integral /= 2 ; } binary = reverse ( binary ) ; binary += ( ' . ' ) ; while ( k_prec -- > 0 ) { fractional *= 2 ; int fract_bit = ( int ) fractional ; if ( fract_bit == 1 ) { fractional -= fract_bit ; binary += ( char ) ( 1 + '0' ) ; } else { binary += ( char ) ( 0 + '0' ) ; } } return binary ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . valueOf ( temparray ) ; } public static void main ( String [ ] args ) { double n = 4.47 ; int k = 3 ; System . out . println ( decimalToBinary ( n , k ) ) ; n = 6.986 ; k = 5 ; System . out . println ( decimalToBinary ( n , k ) ) ; } }


"Convert from any base to decimal and vice versa | Java program to convert a number from any base to decimal ; To return value of a char . For example , 2 is returned for '2' . 10 is returned for ' A ' , 11 for ' B ' ; Function to convert a number from given base ' b ' to decimal ; Initialize ; power of base Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( String str , int base ) { int len = str . length ( ) ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str . charAt ( i ) ) >= base ) { System . out . println ( " Invalid ▁ Number " ) ; return - 1 ; } num += val ( str . charAt ( i ) ) * power ; power = power * base ; } return num ; } public static void main ( String [ ] args ) { String str = "11A " ; int base = 16 ; System . out . println ( " Decimal ▁ equivalent ▁ of ▁ " + str + " ▁ in ▁ base ▁ " + base + " ▁ is ▁ " + " ▁ " + toDeci ( str , base ) ) ; } }


"Convert given Binary string S to all 1 s by changing all 0 s to 1 s in range [ i + 1 , i + K ] if S [ i ] is 1 | Java program for the above approach ; Function to check whether all 0 s in the string can be changed into 1 s ; Store the count of 0 s converted for the last occurrence of 1 ; Declere a stack ; Traverse the string , S ; If stack is empty ; There is no 1 that can change this 0 to 1 ; Push 1 into the stack ; The last 1 has reached its limit ; New 1 has been found which can now change at most K 0 s ; If flag is 1 , print " YES " else print " NO " ; Driver code ; Given Input ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void changeCharacters ( String S , int N , int K ) { int flag = 1 ; int count = 0 ; Stack < Character > st = new Stack < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( st . empty ( ) ) { if ( S . charAt ( i ) == '0' ) { flag = 0 ; break ; } count = 0 ; st . push ( S . charAt ( i ) ) ; } else { if ( S . charAt ( i ) == '0' ) { count ++ ; if ( count == K ) { st . pop ( ) ; count = 0 ; } } else { count = 0 ; } } } if ( flag == 1 ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } public static void main ( String args [ ] ) { String S = "100100" ; int N = S . length ( ) ; int K = 2 ; changeCharacters ( S , N , K ) ; } }


"Convert given Float value to equivalent Fraction | Java program for the above approach ; Recursive function to return gcd of a and b ; Function to convert the floating values into fraction ; Initialize variables ; Traverse the floating String ; Check if decimal part exist ; Check if recurrence sequence exist ; Retrieve decimal part and recurrence resquence ; Traverse the String ; Convert String to integer ; If no recurrence sequence exist ; Initialize numerator & denominator ; No reccuring term ; Print the result ; If reccuring term exist ; Convert reccuring term to integer ; reccu . size ( ) is num of digit in reccur term ; eq 2 - eq 1 ; Print the result ; Driver Code ; Given String str ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static void findFraction ( String s ) { String be_deci = " " , af_deci = " " , reccu = " " ; boolean x = true , y = false , z = false ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) == ' . ' ) { x = false ; y = true ; continue ; } if ( s . charAt ( i ) == ' ( ' ) { z = true ; y = false ; continue ; } if ( x ) be_deci += s . charAt ( i ) ; if ( y ) af_deci += s . charAt ( i ) ; if ( z ) { for ( ; i < s . length ( ) && s . charAt ( i ) != ' ) ' ; ++ i ) reccu += s . charAt ( i ) ; break ; } } int num_be_deci = Integer . valueOf ( be_deci ) ; int num_af_deci = 0 ; if ( af_deci . length ( ) != 0 ) num_af_deci = Integer . valueOf ( af_deci ) ; int numr = num_be_deci * ( int ) Math . pow ( 10 , af_deci . length ( ) ) + num_af_deci ; int deno = ( int ) Math . pow ( 10 , af_deci . length ( ) ) ; if ( reccu . length ( ) == 0 ) { int gd = __gcd ( numr , deno ) ; System . out . print ( numr / gd + " ▁ / ▁ " + deno / gd ) ; } else { int reccu_num = Integer . valueOf ( reccu ) ; int numr1 = numr * ( int ) Math . pow ( 10 , reccu . length ( ) ) + reccu_num ; int deno1 = deno * ( int ) Math . pow ( 10 , reccu . length ( ) ) ; int res_numr = numr1 - numr , res_deno = deno1 - deno ; int gd = __gcd ( res_numr , res_deno ) ; System . out . print ( res_numr / gd + " ▁ / ▁ " + res_deno / gd ) ; } } public static void main ( String [ ] args ) { String str = "23.98(231 ) " ; findFraction ( str ) ; } }


"Convert given Matrix into sorted Spiral Matrix | Java program to Convert given Matrix into sorted Spiral Matrix ; Function to convert the array to Spiral ; For Array pointer ; k - starting row index m - ending row index l - starting column index n - ending column index ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Function to convert 2D array to 1D array ; Store value 2D Matrix To 1D array ; Function to print the Matrix ; Print Spiral Matrix ; Function to sort the array ; Sort array Using InBuilt Function ; Function to Convert given Matrix into sorted Spiral Matrix ; Driver code"
How can the above be solved in Java?

import java . util . * ; public class TwistedMatrix { static int MAX = 1000 ; static void ToSpiral ( int m , int n , int Sorted [ ] , int a [ ] [ ] ) { int index = 0 ; int k = 0 , l = 0 ; while ( k < m && l < n ) { for ( int i = l ; i < n ; ++ i ) { a [ k ] [ i ] = Sorted [ index ] ; index ++ ; } k ++ ; for ( int i = k ; i < m ; ++ i ) { a [ i ] [ n - 1 ] = Sorted [ index ] ; index ++ ; } n -- ; if ( k < m ) { for ( int i = n - 1 ; i >= l ; -- i ) { a [ m - 1 ] [ i ] = Sorted [ index ] ; index ++ ; } m -- ; } if ( l < n ) { for ( int i = m - 1 ; i >= k ; -- i ) { a [ i ] [ l ] = Sorted [ index ] ; index ++ ; } l ++ ; } } } public static int [ ] convert2Dto1D ( int y [ ] [ ] , int m , int n ) { int index = 0 ; int x [ ] = new int [ m * n ] ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { x [ index ] = y [ i ] [ j ] ; index ++ ; } } return x ; } public static void PrintMatrix ( int a [ ] [ ] , int m , int n ) { for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( a [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static int [ ] SortArray ( int x [ ] ) { Arrays . sort ( x ) ; return x ; } public static void convertMatrixToSortedSpiral ( int y [ ] [ ] , int m , int n ) { int a [ ] [ ] = new int [ MAX ] [ MAX ] ; int x [ ] = new int [ m * n ] ; x = convert2Dto1D ( y , m , n ) ; x = SortArray ( x ) ; ToSpiral ( m , n , x , a ) ; PrintMatrix ( a , m , n ) ; } public static void main ( String [ ] args ) { int m = 4 , n = 3 ; int y [ ] [ ] = { { 2 , 5 , 12 } , { 22 , 45 , 55 } , { 1 , 6 , 8 } , { 13 , 56 , 10 } } ; convertMatrixToSortedSpiral ( y , m , n ) ; } }


"Convert given array to Arithmetic Progression by adding an element | Java implementation of the approach ; Function to return the number to be added ; If difference of the current consecutive elements is different from the common difference ; If number has already been chosen then it 's not possible  to add another number ; If the current different is twice the common difference then a number can be added midway from current and previous element ; Number has been chosen ; It 's not possible to maintain  the common difference ; Return last element + common difference if no element is chosen and the array is already in AP ; Else return the chosen number ; Driver code"
How can the above be solved in Java?

import java . util . * ; public class GFG { static int getNumToAdd ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int d = arr [ 1 ] - arr [ 0 ] ; int numToAdd = - 1 ; boolean numAdded = false ; for ( int i = 2 ; i < n ; i ++ ) { int diff = arr [ i ] - arr [ i - 1 ] ; if ( diff != d ) { if ( numAdded ) return - 1 ; if ( diff == 2 * d ) { numToAdd = arr [ i ] - d ; numAdded = true ; } else return - 1 ; } } if ( numToAdd == - 1 ) return ( arr [ n - 1 ] + d ) ; return numToAdd ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 7 , 11 , 13 , 15 } ; int n = arr . length ; System . out . println ( getNumToAdd ( arr , n ) ) ; } }


"Convert given string so that it holds only distinct characters | Java implementation of the approach ; Function to return the index of the character that has 0 occurrence starting from index i ; If current character has 0 occurrence ; If no character has 0 occurrence ; Function to return the modified string which consists of distinct characters ; String cannot consist of all distinct characters ; Count the occurrences for each of the character ; Index for the first character that hasn 't appeared in the string ; If current character appeared more than once then it has to be replaced with some character that hasn 't occurred yet ; Decrement current character 's occurrence by 1 ; Replace the character ; Update the new character ' s ▁ occurrence ▁ ▁ This ▁ step ▁ can ▁ also ▁ be ▁ skipped ▁ as ▁ we ' ll never encounter this character in the string because it has been added just now ; Find the next character that hasn 't occurred yet ; Return the modified string ; Driver code"
How can the above be solved in Java?

class GFG { static int nextZero ( int i , int occurrences [ ] ) { while ( i < occurrences . length ) { if ( occurrences [ i ] == 0 ) return i ; i ++ ; } return - 1 ; } static String getModifiedString ( String str ) { int n = str . length ( ) ; if ( n > 26 ) return " - 1" ; char ch [ ] = str . toCharArray ( ) ; int i , occurrences [ ] = new int [ 26 ] ; for ( i = 0 ; i < n ; i ++ ) occurrences [ ch [ i ] - ' a ' ] ++ ; int index = nextZero ( 0 , occurrences ) ; for ( i = 0 ; i < n ; i ++ ) { if ( occurrences [ ch [ i ] - ' a ' ] > 1 ) { occurrences [ ch [ i ] - ' a ' ] -- ; ch [ i ] = ( char ) ( ' a ' + index ) ; occurrences [ index ] = 1 ; index = nextZero ( index + 1 , occurrences ) ; } } return String . valueOf ( ch ) ; } public static void main ( String arr [ ] ) { String str = " geeksforgeeks " ; System . out . print ( getModifiedString ( str ) ) ; } }


"Convert given string to a valid mobile number | Java program for the above approach ; Function to print the valid and formatted phone number ; Length of given String ; Store digits in temp ; Iterate given M ; If any digit , append it to temp ; Find new length of String ; If length is not equal to 10 ; Store final result ; Make groups of 3 digits and enclose them within ( ) and separate them with " - " 0 to 2 index 1 st group ; 3 to 5 index 2 nd group ; 6 to 8 index 3 rd group ; 9 to 9 index last group ; Print final result ; Driver Code ; Given String ; Function Call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static void Validate ( String M ) { int len = M . length ( ) ; String temp = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( Character . isDigit ( M . charAt ( i ) ) ) temp += M . charAt ( i ) ; } int nwlen = temp . length ( ) ; if ( nwlen != 10 ) { System . out . print ( "Invalid
"); return ; } String res = " " ; String x = temp . substring ( 0 , 3 ) ; res += " ( " + x + " ) - " ; x = temp . substring ( 3 , 6 ) ; res += " ( " + x + " ) - " ; x = temp . substring ( 6 , 9 ) ; res += " ( " + x + " ) - " ; x = temp . substring ( 9 , 10 ) ; res += " ( " + x + " ) " ; System . out . print ( res + "
"); } public static void main ( String [ ] args ) { String M = "91 ▁ 234rt5%34*0 ▁ 3" ; Validate ( M ) ; } }


"Convert given string to another by minimum replacements of subsequences by its smallest character | Java program for the above approach ; Function to return the minimum number of operation ; Storing data ; Initialize both arrays ; Stores the index of character ; Filling str1array , convChar and hashmap convertMap . ; Not possible to convert ; Calculate result Initializing return values ; Iterating the character from the end ; Increment the number of operations ; Not possible to convert ; To check whether the final element has been added in set S or not . ; Check if v1 [ j ] is present in hashmap or not ; Already converted then then continue ; Not possible to convert ; Print the result ; Driver Code ; Given strings ; Function call"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static void transformString ( String str1 , String str2 ) { int N = str1 . length ( ) ; ArrayList < ArrayList < Integer > > convChar = new ArrayList < > ( ) ; ArrayList < ArrayList < Integer > > str1array = new ArrayList < > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { convChar . add ( new ArrayList < > ( ) ) ; str1array . add ( new ArrayList < > ( ) ) ; } Map < Integer , Character > convertMap = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { str1array . get ( str1 . charAt ( i ) - ' a ' ) . add ( i ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str1 . charAt ( i ) < str2 . charAt ( i ) ) { System . out . println ( - 1 ) ; return ; } else if ( str1 . charAt ( i ) == str2 . charAt ( i ) ) continue ; else { convChar . get ( str2 . charAt ( i ) - ' a ' ) . add ( i ) ; convertMap . put ( i , str2 . charAt ( i ) ) ; } } int ret = 0 ; ArrayList < ArrayList < Integer > > retv = new ArrayList < > ( ) ; for ( int i = 25 ; i >= 0 ; i -- ) { ArrayList < Integer > v = convChar . get ( i ) ; if ( v . size ( ) == 0 ) continue ; ret ++ ; ArrayList < Integer > v1 = str1array . get ( i ) ; if ( v1 . size ( ) == 0 ) { System . out . println ( - 1 ) ; return ; } boolean isScompleted = false ; for ( int j = 0 ; j < v1 . size ( ) ; j ++ ) { if ( convertMap . containsKey ( v1 . get ( j ) ) ) { char a = convertMap . get ( v1 . get ( j ) ) ; if ( a > i + ' a ' ) continue ; else { v . add ( v1 . get ( j ) ) ; isScompleted = true ; retv . add ( v ) ; break ; } } else { v . add ( v1 . get ( j ) ) ; isScompleted = true ; retv . add ( v ) ; break ; } } if ( ! isScompleted ) { System . out . println ( - 1 ) ; return ; } } System . out . println ( ret ) ; } public static void main ( String [ ] args ) { String A = " abcab " ; String B = " aabab " ; transformString ( A , B ) ; } }


"Convert left | Java program to convert left - right to down - right representation of binary tree ; A Binary Tree Node ; An Iterative level order traversal based function to convert left - right to down - right representation . ; Base Case ; Recursively convert left an right subtrees ; If left child is NULL , make right child as left as it is the first child . ; If left child is NOT NULL , then make right child as right of left child ; Set root 's right as NULL  ; A utility function to traverse a tree stored in down - right form . ; Utility function to create a new tree node ; Driver Code ; 1 / \ 2 3 / \ 4 5 / / \ 6 7 8"
How can the above be solved in Java?

class GFG { static class node { int key ; node left , right ; node ( int key ) { this . key = key ; this . left = null ; this . right = null ; } } static void convert ( node root ) { if ( root == null ) return ; convert ( root . left ) ; convert ( root . right ) ; if ( root . left == null ) root . left = root . right ; else root . left . right = root . right ; root . right = null ; } static void downRightTraversal ( node root ) { if ( root != null ) { System . out . print ( root . key + " ▁ " ) ; downRightTraversal ( root . right ) ; downRightTraversal ( root . left ) ; } } static node newNode ( int key ) { node temp = new node ( 0 ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } public static void main ( String [ ] args ) { node root = new node ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; root . right . left . left = newNode ( 6 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; convert ( root ) ; System . out . println ( " Traversal ▁ of ▁ the ▁ tree ▁ " + " converted ▁ to ▁ down - right ▁ form " ) ; downRightTraversal ( root ) ; } }


"Convert numbers into binary representation and add them without carry | Java program to add two binary number without carry ; Function returns sum of both the binary number without carry ; XOR of N and M ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int NoCarrySum ( int N , int M ) { return N ^ M ; } public static void main ( String [ ] args ) { int N = 37 ; int M = 12 ; System . out . print ( NoCarrySum ( N , M ) ) ; } }


"Convert string X to an anagram of string Y with minimum replacements | Java program to convert string X to string Y which minimum number of changes . ; Function that converts string X into lexicographically smallest anagram of string Y with minimal changes ; Counting frequency of characters in each string . ; We maintain two more counter arrays ctrx [ ] and ctry [ ] Ctrx [ ] maintains the count of extra elements present in string X than string Y Ctry [ ] maintains the count of characters missing from string X which should be present in string Y . ; This means that we cannot edit the current character as it 's frequency  in string X is equal to or less  than the frequency in string Y.  Thus, we go to the next position ; Here , we try to find that character , which has more frequency in string Y and less in string X . We try to find this character in lexicographical order so that we get lexicographically smaller string ; This portion deals with the lexicographical property . Now , we put a character in string X when either this character has smaller value than the character present there right now or if this is the last position for it to exchange , else we fix the character already present here in this position . ; Driver code"
How can the above be solved in Java?

class GFG { static final int MAX = 26 ; static void printAnagramAndChanges ( char [ ] X , char [ ] Y ) { int countx [ ] = new int [ MAX ] , county [ ] = new int [ MAX ] , ctrx [ ] = new int [ MAX ] , ctry [ ] = new int [ MAX ] ; int change = 0 ; int l = X . length ; for ( int i = 0 ; i < l ; i ++ ) { countx [ X [ i ] - ' A ' ] ++ ; county [ Y [ i ] - ' A ' ] ++ ; } for ( int i = 0 ; i < MAX ; i ++ ) { if ( countx [ i ] > county [ i ] ) { ctrx [ i ] += ( countx [ i ] - county [ i ] ) ; } else if ( countx [ i ] < county [ i ] ) { ctry [ i ] += ( county [ i ] - countx [ i ] ) ; } change += Math . abs ( county [ i ] - countx [ i ] ) ; } for ( int i = 0 ; i < l ; i ++ ) { if ( ctrx [ X [ i ] - ' A ' ] == 0 ) { continue ; } int j ; for ( j = 0 ; j < MAX ; j ++ ) { if ( ( ctry [ j ] ) > 0 ) { break ; } } if ( countx [ X [ i ] - ' A ' ] == ctrx [ X [ i ] - ' A ' ] X [ i ] - ' A ' > j ) { countx [ X [ i ] - ' A ' ] -- ; ctrx [ X [ i ] - ' A ' ] -- ; ctry [ j ] -- ; X [ i ] = ( char ) ( ' A ' + j ) ; } else { countx [ X [ i ] - ' A ' ] -- ; } } System . out . println ( " Anagram ▁ : ▁ " + String . valueOf ( X ) ) ; System . out . println ( " Number ▁ of ▁ changes ▁ made ▁ : ▁ " + change / 2 ) ; } public static void main ( String [ ] args ) { String x = " CDBABC " , y = " ADCABD " ; printAnagramAndChanges ( x . toCharArray ( ) , y . toCharArray ( ) ) ; } }


"Convert string to integer without using any in | Java program for the above approach ; Function to convert string to integer without using functions ; Initialize a variable ; Iterate till length of the string ; Subtract 48 from the current digit ; Print the answer ; Driver code ; Given string of number ; Function Call"
How can the above be solved in Java?

class GFG { public static void convert ( String s ) { int num = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) num = num * 10 + ( ( int ) s . charAt ( i ) - 48 ) ; System . out . print ( num ) ; } public static void main ( String [ ] args ) { String s = "123" ; convert ( s ) ; } }


"Convert the ASCII value sentence to its equivalent string | Java implementation of the approach ; Function to print the character sequence for the given ASCII sentence ; Append the current digit ; If num is within the required range ; Convert num to char ; Reset num to 0 ; Driver code"
How can the above be solved in Java?

class GFG { static void asciiToSentence ( String str , int len ) { int num = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = num * 10 + ( str . charAt ( i ) - '0' ) ; if ( num >= 32 && num <= 122 ) { char ch = ( char ) num ; System . out . print ( ch ) ; num = 0 ; } } } public static void main ( String args [ ] ) { String str = "7110110110711510211111471101101107115" ; int len = str . length ( ) ; asciiToSentence ( str , len ) ; } }


"Convert the given RGB color code to Hex color code | Java code to convert the given RGB color code to Hex color code ; function to convert decimal to hexadecimal ; char array to store hexadecimal number ; counter for hexadecimal number array ; temporary variable to store remainder ; storing remainder in temp variable . ; check if temp < 10 ; Return the equivalent hexadecimal color code ; Function to convert the RGB code to Hex color code ; The hex color code doesn 't exist ; Driver program to test above function"
How can the above be solved in Java?

 import java . util . * ; class GFG { static String decToHexa ( int n ) { char [ ] hexaDeciNum = new char [ 2 ] ; int i = 0 ; while ( n != 0 ) { int temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = ( char ) ( temp + 48 ) ; i ++ ; } else { hexaDeciNum [ i ] = ( char ) ( temp + 55 ) ; i ++ ; } n = n / 16 ; } String hexCode = " " ; if ( i == 2 ) { hexCode += hexaDeciNum [ 0 ] ; hexCode += hexaDeciNum [ 1 ] ; } else if ( i == 1 ) { hexCode = "0" ; hexCode += hexaDeciNum [ 0 ] ; } else if ( i == 0 ) hexCode = "00" ; return hexCode ; } static String convertRGBtoHex ( int R , int G , int B ) { if ( ( R >= 0 && R <= 255 ) && ( G >= 0 && G <= 255 ) && ( B >= 0 && B <= 255 ) ) { String hexCode = " # " ; hexCode += decToHexa ( R ) ; hexCode += decToHexa ( G ) ; hexCode += decToHexa ( B ) ; return hexCode ; } else return " - 1" ; } public static void main ( String [ ] args ) { int R = 0 , G = 0 , B = 0 ; System . out . print ( convertRGBtoHex ( R , G , B ) + "
"); R = 255 ; G = 255 ; B = 255 ; System . out . print ( convertRGBtoHex ( R , G , B ) + "
"); R = 25 ; G = 56 ; B = 123 ; System . out . print ( convertRGBtoHex ( R , G , B ) + "
"); R = 2 ; G = 3 ; B = 4 ; System . out . print ( convertRGBtoHex ( R , G , B ) + "
"); R = 255 ; G = 255 ; B = 256 ; System . out . print ( convertRGBtoHex ( R , G , B ) + "
"); } }


"Convert the number from International system to Indian system | Java program to convert the number from International system to Indian system ; Function to convert a number represented in International numeric system to Indian numeric system . ; Find the length of the sbInput ; Removing all the separators ( , ) from the sbInput ; Reverse the sbInput ; Declaring the output ; Process the sbInput ; Add a separator ( , ) after the third number ; Then add a separator ( , ) after every second number ; Reverse the output ; Return the output string back to the main function ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String convert ( String input ) { StringBuilder sbInput = new StringBuilder ( input ) ; int len = sbInput . length ( ) ; for ( int i = 0 ; i < len ; ) { if ( sbInput . charAt ( i ) == ' , ' ) { sbInput . deleteCharAt ( i ) ; len -- ; i -- ; } else if ( sbInput . charAt ( i ) == ' ▁ ' ) { sbInput . deleteCharAt ( i ) ; len -- ; i -- ; } else { i ++ ; } } StringBuilder sbInputReverse = sbInput . reverse ( ) ; StringBuilder output = new StringBuilder ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( i == 2 ) { output . append ( sbInputReverse . charAt ( i ) ) ; output . append ( " ▁ , " ) ; } else if ( i > 2 && i % 2 == 0 && i + 1 < len ) { output . append ( sbInputReverse . charAt ( i ) ) ; output . append ( " ▁ , " ) ; } else { output . append ( sbInputReverse . charAt ( i ) ) ; } } StringBuilder reverseOutput = output . reverse ( ) ; return reverseOutput . toString ( ) ; } public static void main ( String [ ] args ) { String input1 = "123 , ▁ 456 , ▁ 789" ; String input2 = "90 , ▁ 050 , ▁ 000 , ▁ 000" ; System . out . println ( convert ( input1 ) ) ; System . out . println ( convert ( input2 ) ) ; } }


"Convert the string into palindrome string by changing only one character | Java program to Check if it is possible to convert the string into palindrome string by changing only one character . ; Function to check if it is possible to convert the string into palindrome ; Counting number of characters that should be changed . ; If count of changes is less than or equal to 1 ; Driver Function"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean checkPalindrome ( String str ) { int n = str . length ( ) ; int count = 0 ; for ( int i = 0 ; i < n / 2 ; ++ i ) if ( str . charAt ( i ) != str . charAt ( n - i - 1 ) ) ++ count ; return ( count <= 1 ) ; } public static void main ( String [ ] args ) { String str = " abccaa " ; if ( checkPalindrome ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Convert to Strictly increasing integer array with minimum changes | Java program to find min elements to change so array is strictly increasing ; To find min elements to remove from array to make it strictly increasing ; Mark all elements of LIS as 1 ; Find LIS of array ; Return min changes for array to strictly increasing ; Driver program to test minRemove ( )"
How can the above be solved in Java?

public class Main { static int minRemove ( int arr [ ] , int n ) { int LIS [ ] = new int [ n ] ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) LIS [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) LIS [ i ] = Math . max ( LIS [ i ] , LIS [ j ] + 1 ) ; } len = Math . max ( len , LIS [ i ] ) ; } return n - len ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 6 , 5 , 4 } ; int n = arr . length ; System . out . println ( minRemove ( arr , n ) ) ; } }


"Convert to a string that is repetition of a substring of k length | Java program to check if a string can be converted to a string that has repeated substrings of length k . ; Returns true if str can be converted to a string with k repeated substrings after replacing k characters . ; Length of string must be a multiple of k ; Map to store strings of length k and their counts ; If string is already a repetition of k substrings , return true . ; If number of distinct substrings is not 2 , then not possible to replace a string . ; One of the two distinct must appear exactly once . Either the first entry appears once , or it appears n / k - 1 times to make other substring appear once . ; Driver code"
How can the above be solved in Java?

import java . util . HashMap ; import java . util . Iterator ; class GFG { static boolean checkString ( String str , int k ) { int n = str . length ( ) ; if ( n % k != 0 ) return false ; HashMap < String , Integer > mp = new HashMap < > ( ) ; try { for ( int i = 0 ; i < n ; i += k ) mp . put ( str . substring ( i , k ) , mp . get ( str . substring ( i , k ) ) == null ? 1 : mp . get ( str . substring ( i , k ) ) + 1 ) ; } catch ( Exception e ) { } if ( mp . size ( ) == 1 ) return true ; if ( mp . size ( ) != 2 ) return false ; HashMap . Entry < String , Integer > entry = mp . entrySet ( ) . iterator ( ) . next ( ) ; if ( entry . getValue ( ) == ( n / k - 1 ) || entry . getValue ( ) == 1 ) return true ; return false ; } public static void main ( String [ ] args ) { if ( checkString ( " abababcd " , 2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }