"Recursive selection sort for singly linked list | Swapping node links | Java implementation of recursive selection sort for singly linked list | Swapping node links ; A Linked list node ; function to swap nodes ' currX ' and ' currY ' in a linked list without swapping data ; make ' currY ' as new head ; adjust links ; Swap next pointers ; function to sort the linked list using recursive selection sort technique ; if there is only a single node ; ' min ' - pointer to store the node having minimum data value ; ' beforeMin ' - pointer to store node previous to ' min ' node ; traverse the list till the last node ; if true , then update ' min ' and ' beforeMin ' ; if ' min ' and ' head ' are not same , swap the head node with the ' min ' node ; recursively sort the remaining list ; function to sort the given linked list ; if list is empty ; sort the list using recursive selection sort technique ; function to insert a node at the beginning of the linked list ; allocate node ; put in the data ; link the old list to the new node ; move the head to point to the new node ; function to print the linked list ; Driver code ; create linked list 10.12 .8 .4 .6 ; sort the linked list"
How can the above be solved in Java?

class GFG { static class Node { int data ; Node next ; } ; static Node swapNodes ( Node head_ref , Node currX , Node currY , Node prevY ) { head_ref = currY ; prevY . next = currX ; Node temp = currY . next ; currY . next = currX . next ; currX . next = temp ; return head_ref ; } static Node recurSelectionSort ( Node head ) { if ( head . next == null ) return head ; Node min = head ; Node beforeMin = null ; Node ptr ; for ( ptr = head ; ptr . next != null ; ptr = ptr . next ) { if ( ptr . next . data < min . data ) { min = ptr . next ; beforeMin = ptr ; } } if ( min != head ) head = swapNodes ( head , head , min , beforeMin ) ; head . next = recurSelectionSort ( head . next ) ; return head ; } static Node sort ( Node head_ref ) { if ( ( head_ref ) == null ) return null ; head_ref = recurSelectionSort ( head_ref ) ; return head_ref ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 6 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 12 ) ; head = push ( head , 10 ) ; System . out . println ( " Linked ▁ list ▁ before ▁ sorting : " ) ; printList ( head ) ; head = sort ( head ) ; System . out . print ( " Linked list after sorting : "); printList ( head ) ; } }


"Recursive solution to count substrings with same first and last characters | Java program to count substrings with same first and last characters ; Function to count substrings with same first and last characters ; base cases ; Driver code"
How can the above be solved in Java?

class GFG { static int countSubstrs ( String str , int i , int j , int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; int res = countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ; if ( str . charAt ( i ) == str . charAt ( j ) ) res ++ ; return res ; } public static void main ( String [ ] args ) { String str = " abcab " ; int n = str . length ( ) ; System . out . print ( countSubstrs ( str , 0 , n - 1 , n ) ) ; } }


"Recursive sum of digit in n ^ x , where n and x are very large | Java Code for Sum of digit of n ^ x where n and x are very large ; function to get sum of digits of a number ; function to return sum ; Find sum of digits in n ; Find remainder of exponent ; Driver program to test above function"
How can the above be solved in Java?

import java . util . * ; class GFG { static long digSum ( long n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } static long PowDigSum ( long n , long x ) { long sum = digSum ( n ) ; long rem = x % 6 ; if ( ( sum == 3 sum == 6 ) && x > 1 ) return 9 ; else if ( x == 1 ) return sum ; else if ( x == 0 ) return 1 ; else if ( rem == 0 ) return digSum ( ( long ) Math . pow ( sum , 6 ) ) ; else return digSum ( ( long ) Math . pow ( sum , rem ) ) ; } public static void main ( String [ ] args ) { int n = 33333 ; int x = 332654 ; System . out . println ( PowDigSum ( n , x ) ) ; } }


"Recursive sum of digits of a number formed by repeated appends | Java program to find Sum of digits of a number formed by repeating a number X number of times until sum become single digit . ; return single digit sum of a number . ; Returns recursive sum of digits of a number formed by repeating a number X number of times until sum become single digit . ; Driver program"
How can the above be solved in Java?

class GFG { static int digSum ( int n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } static int repeatedNumberSum ( int n , int x ) { int sum = x * digSum ( n ) ; return digSum ( sum ) ; } public static void main ( String [ ] args ) { int n = 24 , x = 3 ; System . out . println ( repeatedNumberSum ( n , x ) ) ; } }


"Recursive sum of digits of a number is prime or not | java code to check if recursive sum of digits is prime or not . ; Function for recursive digit sum ; function to check if prime or not the single digit ; calls function which returns sum till single digit ; checking prime ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int recDigSum ( int n ) { if ( n == 0 ) return 0 ; else { if ( n % 9 == 0 ) return 9 ; else return n % 9 ; } } static void check ( int n ) { n = recDigSum ( n ) ; if ( n == 2 n == 3 n == 5 n == 7 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int n = 5602 ; check ( n ) ; } }


"Recursively break a number in 3 parts to get maximum sum | A simple recursive JAVA program to find maximum sum by recursively breaking a number in 3 parts . ; Function to find the maximum sum ; base conditions ; Fill in bottom - up manner using recursive formula . ; Driver program to test the above function"
How can the above be solved in Java?

import java . io . * ; class GFG { final int MAX = 1000000 ; static int breakSum ( int n ) { int dp [ ] = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = Math . max ( dp [ i / 2 ] + dp [ i / 3 ] + dp [ i / 4 ] , i ) ; return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 24 ; System . out . println ( breakSum ( n ) ) ; } }


"Recursively break a number in 3 parts to get maximum sum | A simple recursive JAVA program to find maximum sum by recursively breaking a number in 3 parts . ; Function to find the maximum sum ; base conditions ; recursively break the number and return what maximum you can get ; Driver program to test the above function"
How can the above be solved in Java?

import java . io . * ; class GFG { static int breakSum ( int n ) { if ( n == 0 n == 1 ) return n ; return Math . max ( ( breakSum ( n / 2 ) + breakSum ( n / 3 ) + breakSum ( n / 4 ) ) , n ) ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( breakSum ( n ) ) ; } }


"Recursively print all sentences that can be formed from list of word lists | Java program to print all possible sentences from a list of word list ; A recursive function to print all possible sentences that can be formed from a list of word list ; Add current word to output array ; If this is last word of current output sentence , then print the output sentence ; Recur for next row ; A wrapper over printUtil ( ) ; Create an array to store sentence ; Consider all words for first row as starting points and print all sentences ; Driver program to test above functions"
How can the above be solved in Java?

class GFG { static final int R = 3 ; static final int C = 3 ; static void printUtil ( String [ ] [ ] arr , int m , int n , String [ ] output ) { output [ m ] = arr [ m ] [ n ] ; if ( m == R - 1 ) { for ( int i = 0 ; i < R ; i ++ ) System . out . print ( output [ i ] + " ▁ " ) ; System . out . println ( ) ; return ; } for ( int i = 0 ; i < C ; i ++ ) if ( arr [ m + 1 ] [ i ] != " " && m < C ) printUtil ( arr , m + 1 , i , output ) ; } static void print ( String [ ] [ ] arr ) { String [ ] output = new String [ R ] ; for ( int i = 0 ; i < C ; i ++ ) if ( arr [ 0 ] [ i ] != " " ) printUtil ( arr , 0 , i , output ) ; } public static void main ( String [ ] args ) { String [ ] [ ] arr = { { " you " , " we " , " " } , { " have " , " are " , " " } , { " sleep " , " eat " , " drink " } } ; print ( arr ) ; } }


"Recursively remove all adjacent duplicates | Java Program for above approach ; Recursively removes adjacent duplicates from str and returns new string . las_removed is a pointer to last_removed character ; If length of string is 1 or 0 ; Recursively remove all the adjacent characters formed by removing the adjacent characters ; Have to check whether this is the repeated character that matches the last char of the parent String ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { private static String removeDuplicates ( String s , char ch ) { if ( s == null || s . length ( ) <= 1 ) { return s ; } int i = 0 ; while ( i < s . length ( ) ) { if ( i + 1 < s . length ( ) && s . charAt ( i ) == s . charAt ( i + 1 ) ) { int j = i ; while ( j + 1 < s . length ( ) && s . charAt ( j ) == s . charAt ( j + 1 ) ) { j ++ ; } char lastChar = i > 0 ? s . charAt ( i - 1 ) : ch ; String remStr = removeDuplicates ( s . substring ( j + 1 , s . length ( ) ) , lastChar ) ; s = s . substring ( 0 , i ) ; int k = s . length ( ) , l = 0 ; while ( remStr . length ( ) > 0 && s . length ( ) > 0 && remStr . charAt ( 0 ) == s . charAt ( s . length ( ) - 1 ) ) { while ( remStr . length ( ) > 0 && remStr . charAt ( 0 ) != ch && remStr . charAt ( 0 ) == s . charAt ( s . length ( ) - 1 ) ) { remStr = remStr . substring ( 1 , remStr . length ( ) ) ; } s = s . substring ( 0 , s . length ( ) - 1 ) ; } s = s + remStr ; i = j ; } else { i ++ ; } } return s ; } public static void main ( String [ ] args ) { String str1 = " mississipie " ; System . out . println ( removeDuplicates ( str1 , ' ▁ ' ) ) ; String str2 = " ocvvcolop " ; System . out . println ( removeDuplicates ( str2 , ' ▁ ' ) ) ; } }


"Reduce Hamming distance by swapping two characters | Java code to decrease hamming distance using swap . ; Function to return the swapped indexes to get minimum hamming distance . ; Find the initial hamming distance ; Case - I : To decrease distance by two ; ASCII values of present character . ; If two same letters appear in different positions print their indexes ; Store the index of letters which is in wrong position ; Case : II ; If misplaced letter is found , print its original index and its new index ; Store the index of letters in wrong position ; Case - III ; Driver code"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static final int MAX = 26 ; static void Swap ( String s , String t , int n ) { int dp [ ] [ ] = new int [ MAX ] [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int tot = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( s . charAt ( i ) != t . charAt ( i ) ) tot ++ ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . charAt ( i ) - ' a ' ; int b = t . charAt ( i ) - ' a ' ; if ( a == b ) continue ; if ( dp [ a ] [ b ] != - 1 ) { System . out . println ( i + 1 + " ▁ " + ( dp [ a ] [ b ] + 1 ) ) ; return ; } dp [ b ] [ a ] = i ; } int A [ ] = new int [ MAX ] , B [ ] = new int [ MAX ] ; Arrays . fill ( A , - 1 ) ; Arrays . fill ( B , - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . charAt ( i ) - ' a ' ; int b = t . charAt ( i ) - ' a ' ; if ( a == b ) continue ; if ( A [ b ] != - 1 ) { System . out . println ( i + 1 + " ▁ " + ( A [ b ] + 1 ) ) ; return ; } if ( B [ a ] != - 1 ) { System . out . println ( i + 1 + " ▁ " + ( B [ a ] + 1 ) ) ; return ; } A [ a ] = i ; B [ b ] = i ; } System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { String S = " permanent " ; String T = " pergament " ; int n = S . length ( ) ; if ( S == " " T == " " ) System . out . println ( " Required ▁ string ▁ is ▁ empty . " ) ; else Swap ( S , T , n ) ; } }


"Reduce N to 0 or less by given X and Y operations | Java program to implement the above approach ; Function to check if N can be reduced to <= 0 or not ; Update N to N / 2 + 10 at most X times ; Update N to N - 10 Y times ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean NegEqu ( int N , int X , int Y ) { while ( X > 0 && ( N > N / 2 + 10 ) ) { N = N / 2 + 10 ; X -= 1 ; } while ( Y > 0 ) { N = N - 10 ; Y -= 1 ; } if ( N <= 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int N = 100 ; int X = 3 ; int Y = 4 ; if ( NegEqu ( N , X , Y ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Reduce N to 1 with minimum number of given operations | Java implementation of the approach ; Function to return the minimum number of given operations required to reduce n to 1 ; To store the count of operations ; To store the digit ; If n is already then no operation is required ; Extract all the digits except the first digit ; Store the maximum of that digits ; for each digit ; First digit ; Add the value to count ; Driver code"
How can the above be solved in Java?

class GFG { static long minOperations ( long n ) { long count = 0 ; long d = 0 ; if ( n == 1 ) return 0 ; while ( n > 9 ) { d = Math . max ( n % 10 , d ) ; n /= 10 ; count += 10 ; } d = Math . max ( d , n - 1 ) ; count += Math . abs ( d ) ; return count - 1 ; } public static void main ( String [ ] args ) { long n = 240 ; System . out . println ( minOperations ( n ) ) ; } }


"Reduce a given Binary Array to a single element by removal of Triplets | Java program to implement the above approach ; Function to check if it is possible to reduce the array to a single element ; Stores frequency of 0 's ; Stores frequency of 1 's ; Condition for array to be reduced ; Otherwise ; Driver Code"
How can the above be solved in Java?

class GFG { static void solve ( int arr [ ] , int n ) { int countzeroes = 0 ; int countones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) countzeroes ++ ; else countones ++ ; } if ( Math . abs ( countzeroes - countones ) == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 0 , 1 , 1 , 1 } ; int n = arr . length ; solve ( arr , n ) ; } }


"Reduce a number N by at most D to maximize count of trailing nines | Java program for the above approach ; Function to find a number with maximum count of trailing nine ; Stores count of digits in n ; Stores power of 10 ; If last i digits greater than or equal to d ; Update res ; Update p10 ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { static void maxNumTrailNine ( int n , int d ) { int res = n ; int cntDigits = ( int ) Math . log10 ( n ) + 1 ; int p10 = 10 ; for ( int i = 1 ; i <= cntDigits ; i ++ ) { if ( n % p10 >= d ) { break ; } else { res = n - n % p10 - 1 ; } p10 = p10 * 10 ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int n = 1025 , d = 6 ; maxNumTrailNine ( n , d ) ; } }


"Reduce a number to 1 by performing given operations | Set 2 | Java implementation of the approach ; Function to return the number of set bits in n ; Function to return the minimum steps required to reach 1 ; If n is even then divide it by 2 ; If n is 3 or the number of set bits in ( n - 1 ) is less than the number of set bits in ( n + 1 ) ; Increment the number of steps ; Return the minimum number of steps ; Driver code"
How can the above be solved in Java?

class GFG { static int set_bits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n % 2 ; n /= 2 ; } return count ; } static int minSteps ( int n ) { int ans = 0 ; while ( n != 1 ) { if ( n % 2 == 0 ) n /= 2 ; else if ( n == 3 || set_bits ( n - 1 ) < set_bits ( n + 1 ) ) n -- ; else n ++ ; ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . print ( minSteps ( n ) ) ; } }


"Reduce a number to 1 by performing given operations | Set 3 | java program for the above approach ; Utility function to check if n is power of 2 ; Utility function to find highest power of 2 less than or equal to given number ; Recursive function to find steps needed to reduce a given integer to 1 ; Base Condition ; If the number is a power of 2 ; Else subtract the greatest power of 2 smaller than N from N ; Driver Code ; Input ; Function call"
How can the above be solved in Java?

class GFG { static int highestPowerof2 ( int n ) { int p = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; return ( int ) Math . pow ( 2 , p ) ; } static boolean isPowerOfTwo ( int n ) { if ( n == 0 ) return false ; return ( int ) ( Math . ceil ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ) == ( int ) ( Math . floor ( ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ) ) ; } static int reduceToOne ( int N ) { if ( N == 1 ) { return 0 ; } if ( isPowerOfTwo ( N ) == true ) { return 1 + reduceToOne ( N / 2 ) ; } else { return 1 + reduceToOne ( N - highestPowerof2 ( N ) ) ; } } public static void main ( String [ ] args ) { int N = 7 ; System . out . println ( reduceToOne ( N ) ) ; } }


"Reduce a string to a valid email address of minimum length by replacing specified substrings | Java program for the above approach ; Function to find the minimum length by replacing at with @ and dot with ' . ' such that the string is valid email ; Stores string by replacing at with @ and dot with ' . ' # such that the string is valid email ; append first character ; Stores index ; Check if at ( @ ) already included or not ; Iterate over characters of the string ; at can be replaced at most once ; Update ans ; Update i ; Update notAt ; If current substring found dot ; Update ans ; Update i ; Update ans ; Update i ; Driver code ; To display the result"
How can the above be solved in Java?

class GFG { static String minEmail ( String email ) { String ans = new String ( " " ) ; int len = email . length ( ) ; ans += email . charAt ( 0 ) ; int i = 1 ; boolean notAt = true ; while ( i < len ) { if ( i < len - 3 && notAt && email . charAt ( i ) == ' a ' && email . charAt ( i + 1 ) == ' t ' ) { ans += ' @ ' ; i += 1 ; notAt = false ; } else if ( i < len - 4 && email . charAt ( i ) == ' d ' && email . charAt ( i + 1 ) == ' o ' && email . charAt ( i + 2 ) == ' t ' ) { ans += ' . ' ; i += 2 ; } else { ans += email . charAt ( i ) ; } i += 1 ; } return ans ; } public static void main ( String [ ] args ) { String email = new String ( " geeksforgeeksatgmaildotcom " ) ; System . out . println ( minEmail ( email ) ) ; } }


"Reduce all array elements to zero by performing given operations thrice | Java program for the above approach ; Function to reduce all array elements to zero ; If size of array is 1 ; First operation ; 2 nd Operation ; 3 rd Operation ; Otherwise ; 1 st Operation ; 2 nd Operation ; 3 rd Operation ; Driver code ; Input ; Function call to make all array elements equal to 0"
How can the above be solved in Java?

import java . util . * ; class GFG { static void ConvertArray ( int arr [ ] , int N ) { if ( N == 1 ) { System . out . println ( " Operation ▁ 1 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; System . out . println ( " Added ▁ elements : ▁ " + - 1 * arr [ 0 ] ) ; System . out . println ( ) ; System . out . println ( " Operation ▁ 2 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; System . out . println ( " Added ▁ elements : ▁ " + 1 * arr [ 0 ] ) ; System . out . println ( ) ; System . out . println ( " Operation ▁ 3 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; System . out . println ( " Added ▁ elements : ▁ " + - 1 * arr [ 0 ] ) ; } else { System . out . println ( " Operation ▁ 1 ▁ : ▁ " + 1 + " ▁ " + N ) ; System . out . print ( " Added ▁ elements : ▁ " ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( - 1 * arr [ i ] * N + " ▁ " ) ; } System . out . println ( ) ; System . out . println ( ) ; System . out . println ( " Operation ▁ 2 ▁ : ▁ " + 1 + " ▁ " + ( N - 1 ) ) ; System . out . print ( " Added ▁ elements : ▁ " ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { System . out . print ( arr [ i ] * ( N - 1 ) + " ▁ " ) ; } System . out . println ( ) ; System . out . println ( ) ; System . out . println ( " Operation ▁ 3 ▁ : ▁ " + N + " ▁ " + N ) ; System . out . print ( " Added ▁ elements : ▁ " ) ; System . out . println ( arr [ N - 1 ] * ( N - 1 ) ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; int N = arr . length ; ConvertArray ( arr , N ) ; } }


"Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K | Java program for the above approach ; Function to check if an array can be reduced to single element by removing maximum element among any chosen pairs ; Sort the array in descending order ; Traverse the array ; If the absolute difference of 2 consecutive array elements is greater than K ; If the array can be reduced to a single element ; Driven Code ; Function Call to check if an array can be reduced to a single element"
How can the above be solved in Java?

import java . util . * ; class GFG { static void canReduceArray ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int b [ ] = new int [ N ] ; int j = N ; for ( int i = 0 ; i < N ; i ++ ) { b [ j - 1 ] = arr [ i ] ; j = j - 1 ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] - arr [ i + 1 ] > K ) { System . out . print ( " No " ) ; return ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 3 } ; int N = arr . length ; int K = 1 ; canReduceArray ( arr , N , K ) ; } }


"Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum | Java program for the above approach ; Function to print the index from where the operation can be started ; Initialize B [ ] ; Initialize save ; Make B [ ] equals to arr [ ] ; Sort the array B [ ] ; Traverse from N - 1 to 1 ; If B [ i ] & B [ i - 1 ] are unequal ; If all elements are same ; If arr [ 1 ] is maximum element ; If arr [ N - 1 ] is maximum element ; Find the maximum element ; Driver Code ; Given array arr [ ] ; Length of array ; Function Call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static void printIndex ( int arr [ ] , int N ) { int [ ] B = new int [ N ] ; int save = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = arr [ i ] ; } Arrays . sort ( B ) ; for ( int i = N - 1 ; i >= 1 ; i -- ) { if ( B [ i ] != B [ i - 1 ] ) { save = B [ i ] ; break ; } } if ( save == - 1 ) { System . out . print ( - 1 + "
"); return ; } if ( save == arr [ 0 ] && save != arr [ 1 ] ) { System . out . print ( 1 ) ; } else if ( save == arr [ N - 1 ] && save != arr [ N - 2 ] ) { System . out . print ( N ) ; } for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( save == arr [ i ] && ( save != arr [ i - 1 ] save != arr [ i + 1 ] ) ) { System . out . print ( i + 1 ) ; break ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 4 , 4 , 5 } ; int N = arr . length ; printIndex ( arr , N ) ; } }


"Reduce array to longest sorted array possible by removing either half of given array in each operation | Java program to implement the above approach ; Function to check if the subarray arr [ i . . j ] is a sorted subarray or not ; Traverse all elements of the subarray arr [ i ... j ] ; If the previous element of the subarray exceeds current element ; Return 0 ; Return 1 ; Recursively partition the array into two equal halves ; If atmost one element is left in the subarray arr [ i . . j ] ; Checks if subarray arr [ i . . j ] is a sorted subarray or not ; If the subarray arr [ i ... j ] is a sorted subarray ; Stores middle element of the subarray arr [ i . . j ] ; Recursively partition the current subarray arr [ i . . j ] into equal halves ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int isSortedparitions ( int arr [ ] , int i , int j ) { for ( int k = i + 1 ; k <= j ; k ++ ) { if ( arr [ k ] < arr [ k - 1 ] ) { return 0 ; } } return 1 ; } static int partitionsArr ( int arr [ ] , int i , int j ) { if ( i >= j ) return 1 ; int flag = ( int ) isSortedparitions ( arr , i , j ) ; if ( flag != 0 ) { return ( j - i + 1 ) ; } int mid = ( i + j ) / 2 ; int X = partitionsArr ( arr , i , mid ) ; int Y = partitionsArr ( arr , mid + 1 , j ) ; return Math . max ( X , Y ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 12 , 1 , 2 , 13 , 14 , 3 , 4 } ; int N = arr . length ; System . out . print ( partitionsArr ( arr , 0 , N - 1 ) ) ; } }


"Reduce given array by replacing subarrays with values less than K with their sum | Java program for the above approach ; Function to replace all the subarray having values < K with their sum ; Stores the sum of subarray having elements less than K ; Stores the updated array ; Traverse the array ; Update the sum ; Otherwise , update the vector ; Print the result ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void updateArray ( int [ ] arr , int K ) { int sum = 0 ; ArrayList < Integer > res = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] < K ) { sum += arr [ i ] ; } else { if ( sum != 0 ) { res . add ( sum ) ; } sum = 0 ; res . add ( arr [ i ] ) ; } } if ( sum != 0 ) res . add ( sum ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 200 , 6 , 36 , 612 , 121 , 66 , 63 , 39 , 668 , 108 } ; int K = 100 ; updateArray ( arr , K ) ; } }


"Reduce sum of same | Java program for the above approach ; Function to check if elements of B [ ] can be rearranged such that A [ i ] + B [ i ] <= X ; Checks the given condition ; Sort A [ ] in ascending order ; Sort B [ ] in descending order ; Traverse the arrays A [ ] and B [ ] ; If A [ i ] + B [ i ] exceeds X ; Rearrangement not possible , set flag to false ; If flag is true ; Otherwise ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void rearrange ( int A [ ] , int B [ ] , int N , int X ) { boolean flag = true ; Arrays . sort ( A ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] + B [ N - 1 - i ] > X ) { flag = false ; break ; } } if ( flag == true ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 1 , 1 , 2 } ; int X = 4 ; int N = A . length ; rearrange ( A , B , N , X ) ; } }


"Reduce the array by deleting elements which are greater than all elements to its left | Java program for the above approach ; Function to implement merging of arr [ ] ; Function to delete all elements which satisfy the condition A [ i ] > A [ i - 1 ] ; Divide array into its subarray ; Getting back merged array with all its right element greater than left one . ; Driver Code ; Given array arr [ ]"
How can the above be solved in Java?

import java . util . ArrayList ; import java . util . Arrays ; class GFG { static ArrayList < Integer > merge ( ArrayList < Integer > x , ArrayList < Integer > y ) { for ( Integer i : y ) { if ( x . get ( x . size ( ) - 1 ) > i ) x . add ( i ) ; } return x ; } static ArrayList < Integer > mergeDel ( ArrayList < Integer > l ) { if ( l . size ( ) == 1 ) return l ; int m = l . size ( ) / 2 ; ArrayList < Integer > temp1 = new ArrayList < > ( l . subList ( 0 , m ) ) ; ArrayList < Integer > temp2 = new ArrayList < > ( l . subList ( m , l . size ( ) ) ) ; return merge ( mergeDel ( temp1 ) , mergeDel ( temp2 ) ) ; } public static void main ( String [ ] args ) { Integer [ ] ar = { 5 , 4 , 3 , 2 , 1 } ; ArrayList < Integer > arr = new ArrayList < > ( Arrays . asList ( ar ) ) ; ArrayList < Integer > ans = mergeDel ( arr ) ; System . out . print ( " [ ▁ " ) ; for ( Integer x : ans ) System . out . print ( x + " , ▁ " ) ; System . out . println ( " ] " ) ; } }


"Reduce the array such that each element appears at most 2 times | Java program to reduce the array such that each element appears at most 2 times ; Function to remove duplicates ; Initialise 2 nd pointer ; Iterate over the array ; Updating the 2 nd pointer ; Driver code ; Function call"
How can the above be solved in Java?

class GFG { static void removeDuplicates ( int arr [ ] , int n ) { int st = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 2 && arr [ i ] == arr [ i + 1 ] && arr [ i ] == arr [ i + 2 ] ) continue ; else { arr [ st ] = arr [ i ] ; st ++ ; } } System . out . print ( " { " ) ; for ( int i = 0 ; i < st ; i ++ ) { System . out . print ( arr [ i ] ) ; if ( i != st - 1 ) System . out . print ( " , ▁ " ) ; } System . out . print ( " } " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 4 , 5 } ; int n = arr . length ; removeDuplicates ( arr , n ) ; } }


"Reduce the fraction to its lowest form | Java program to reduce a fraction x / y to its lowest form ; Function to reduce a fraction to its lowest form ; Driver Code"
How can the above be solved in Java?

class GFG { static void reduceFraction ( int x , int y ) { int d ; d = __gcd ( x , y ) ; x = x / d ; y = y / d ; System . out . println ( " x ▁ = ▁ " + x + " , ▁ y ▁ = ▁ " + y ) ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int x = 16 ; int y = 10 ; reduceFraction ( x , y ) ; } }


"Reduce the given Array of [ 1 , N ] by rotating left or right based on given conditions | Java program for the above approach ; Function to find the last element left after performing N - 1 queries of type X ; Stores the next power of 2 ; Iterate until nextPower is at most N ; If X is equal to 1 ; Stores the power of 2 less than or equal to N ; Return the final value ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int rotate ( int arr [ ] , int N , int X ) { long nextPower = 1 ; while ( nextPower <= N ) nextPower *= 2 ; if ( X == 1 ) return ( int ) nextPower - N ; long prevPower = nextPower / 2 ; return 2 * ( N - ( int ) prevPower ) + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int X = 1 ; int N = arr . length ; System . out . println ( rotate ( arr , N , X ) ) ; } }


"Reduce the number to minimum multiple of 4 after removing the digits | Java implementation of the approach ; Function to return the minimum number that can be formed after removing the digits which is a multiple of 4 ; For every digit of the number ; Check if the current digit is divisible by 4 ; If any subsequence of two digits is divisible by 4 ; Driver code"
How can the above be solved in Java?

class GFG { static int TEN = 10 ; static int minNum ( char [ ] str , int len ) { int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '4' str [ i ] == '8' ) { res = Math . min ( res , str [ i ] - '0' ) ; } } for ( int i = 0 ; i < len - 1 ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { int num = ( str [ i ] - '0' ) * TEN + ( str [ j ] - '0' ) ; if ( num % 4 == 0 ) { res = Math . min ( res , num ) ; } } } return ( ( res == Integer . MAX_VALUE ) ? - 1 : res ) ; } public static void main ( String [ ] args ) { String str = "17" ; int len = str . length ( ) ; System . out . print ( minNum ( str . toCharArray ( ) , len ) ) ; } }


"Reduce the string by removing K consecutive identical characters | Java implementation of the approach ; Function to find the reduced string ; Base Case ; Creating a stack of type Pair ; Length of the string S ; iterate through the string ; if stack is empty then simply add the character with count 1 else check if character is same as top of stack ; if character at top of stack is same as current character increase the number of repetitions in the top of stack by 1 ; if character at top of stack is not same as current character push the character along with count 1 into the top of stack ; iterate through the stack Use string ( int , char ) in order to replicate the character multiple times and convert into string then add in front of output string ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { public static String reduced_String ( int k , String s ) { if ( k == 1 ) { String ans = " " ; return ans ; } Stack < Pair > st = new Stack < Pair > ( ) ; int l = s . length ( ) ; int ctr = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( st . size ( ) == 0 ) { st . push ( new Pair ( s . charAt ( i ) , 1 ) ) ; continue ; } if ( st . peek ( ) . c == s . charAt ( i ) ) { Pair p = st . peek ( ) ; st . pop ( ) ; p . ctr += 1 ; if ( p . ctr == k ) { continue ; } else { st . push ( p ) ; } } else { st . push ( new Pair ( s . charAt ( i ) , 1 ) ) ; } } String ans = " " ; while ( st . size ( ) > 0 ) { char c = st . peek ( ) . c ; int cnt = st . peek ( ) . ctr ; while ( cnt -- > 0 ) ans = c + ans ; st . pop ( ) ; } return ans ; } public static void main ( String [ ] args ) { int k = 2 ; String st = " geeksforgeeks " ; String ans = reduced_String ( k , st ) ; System . out . println ( ans ) ; } static class Pair { char c ; int ctr ; Pair ( char c , int ctr ) { this . c = c ; this . ctr = ctr ; } } }


"Reduce the string to minimum length with the given operation | Java implementation of the approach ; Function to return the minimum possible length str can be reduced to with the given operation ; Stack to store the characters of the given string ; For every character of the string ; If the stack is empty then push the current character in the stack ; Get the top character ; If the top element is not equal to the current element and it only differs in the case ; Pop the top element from stack ; Else push the current element ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int minLength ( String str , int len ) { Stack < Character > s = new Stack < Character > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( s . empty ( ) ) { s . push ( str . charAt ( i ) ) ; } else { char c = s . peek ( ) ; if ( c != str . charAt ( i ) && Character . toUpperCase ( c ) == Character . toUpperCase ( ( str . charAt ( i ) ) ) ) { s . pop ( ) ; } else { s . push ( str . charAt ( i ) ) ; } } } return s . size ( ) ; } public static void main ( String [ ] args ) { String str = " ASbBsd " ; int len = str . length ( ) ; System . out . println ( minLength ( str , len ) ) ; } }


"Refactorable number | Java program to check whether number is refactorable or not ; Function to count all divisors ; Initialize result ; If divisors are equal , count only one . ; Otherwise count both ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isRefactorableNumber ( int n ) { int divCount = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; ++ i ) { if ( n % i == 0 ) { if ( n / i == i ) ++ divCount ; else divCount += 2 ; } } return n % divCount == 0 ; } public static void main ( String [ ] args ) { int n = 8 ; if ( isRefactorableNumber ( n ) ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; n = 14 ; if ( isRefactorableNumber ( n ) ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }


"Reflection of a point at 180 degree rotation of another point | Java Program to find the 180 degree reflection of one point around another point . ; Driver code"
How can the above be solved in Java?

class GFG { static void findPoint ( int x1 , int y1 , int x2 , int y2 ) { System . out . println ( " ( " + ( int ) ( 2 * x2 - x1 ) + " , " + ( int ) ( 2 * y2 - y1 ) + " ▁ ) " ) ; } public static void main ( String args [ ] ) { int x1 = 0 , y1 = 0 , x2 = 1 , y2 = 1 ; findPoint ( x1 , y1 , x2 , y2 ) ; } }


"Regular polygon using only 1 s in a binary numbered circle | Java program to find whether a regular polygon is possible in circle with 1 s as vertices ; method returns true if polygon is possible with ' midpoints ' number of midpoints ; loop for getting first vertex of polygon ; loop over array values at ' midpoints ' distance ; and ( & ) all those values , if even one of them is 0 , val will be 0 ; if val is still 1 and ( N / midpoints ) or ( number of vertices ) are more than two ( for a polygon minimum ) print result and return true ; method prints sides in the polygon or print not possible in case of no possible polygon ; limit for iterating over divisors ; If i divides N then i and ( N / i ) will be divisors ; check polygon for both divisors ; Driver method"
How can the above be solved in Java?

class Test { static boolean checkPolygonWithMidpoints ( int arr [ ] , int N , int midpoints ) { for ( int j = 0 ; j < midpoints ; j ++ ) { int val = 1 ; for ( int k = j ; k < N ; k += midpoints ) { val &= arr [ k ] ; } if ( val != 0 && N / midpoints > 2 ) { System . out . println ( " Polygon ▁ possible ▁ with ▁ side ▁ length ▁ " + N / midpoints ) ; return true ; } } return false ; } static void isPolygonPossible ( int arr [ ] , int N ) { int limit = ( int ) Math . sqrt ( N ) ; for ( int i = 1 ; i <= limit ; i ++ ) { if ( N % i == 0 ) { if ( checkPolygonWithMidpoints ( arr , N , i ) || checkPolygonWithMidpoints ( arr , N , ( N / i ) ) ) return ; } } System . out . println ( " Not ▁ possible " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 } ; isPolygonPossible ( arr , arr . length ) ; } }


"Remainder with 7 for large numbers | Java program to find remainder of a large number when divided by 7. ; Function which return Remainder after dividingthe number by 7 ; This series is used to find remainder with 7 ; Index of next element in series ; Initialize result ; Traverse num from end ; Find current digit of nun ; Add next term to result ; Move to next term in series ; Make sure that result never goes beyond 7. ; Make sure that remainder is positive ; Driver code"
How can the above be solved in Java?

class GFG { static int remainderWith7 ( String num ) { int series [ ] = { 1 , 3 , 2 , - 1 , - 3 , - 2 } ; int series_index = 0 ; int result = 0 ; for ( int i = num . length ( ) - 1 ; i >= 0 ; i -- ) { int digit = num . charAt ( i ) - '0' ; result += digit * series [ series_index ] ; series_index = ( series_index + 1 ) % 6 ; result %= 7 ; } if ( result < 0 ) result = ( result + 7 ) % 7 ; return result ; } public static void main ( String [ ] args ) { String str = "12345" ; System . out . print ( " Remainder ▁ with ▁ 7 ▁ is ▁ " + remainderWith7 ( str ) ) ; } }


"Remaining array element after repeated removal of last element and subtraction of each element from next adjacent element | package whatever do not write package name here ; Function to find the last remaining array element after performing the given operations repeatedly ; Stores the resultant sum ; Traverse the array ; Increment sum by arr [ i ] * coefficient of i - th term in ( x - y ) ^ ( N - 1 ) ; Update multiplier ; Return the resultant sum ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { public static int lastElement ( int arr [ ] , int n ) { int sum = 0 ; int multiplier = n % 2 == 0 ? - 1 : 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] * multiplier ; multiplier = multiplier * ( n - 1 - i ) / ( i + 1 ) * ( - 1 ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 2 , 1 } ; int N = 4 ; System . out . println ( lastElement ( arr , N ) ) ; } }


"Remove " b " and " ac " from a given string | A Java program to remove " b " and ' ac ' from input string ; The main function that removes occurrences of " a " and " bc " in input string ; previous character ; current character ; check if current and next character forms ac ; if current character is b ; if current char is ' c ▁ & & ▁ last ▁ char ▁ in ▁ output ▁ ▁ ▁ is ▁ ' a ' so delete both  ; else copy curr char to output string ; Driver program to check above functions"
How can the above be solved in Java?

class GfG { static void stringFilter ( char str [ ] ) { int n = str . length ; int i = - 1 ; int j = 0 ; while ( j < n ) { if ( j < n - 1 && str [ j ] == ' a ' && str [ j + 1 ] == ' c ' ) j += 2 ; else if ( str [ j ] == ' b ' ) j ++ ; else if ( i >= 0 && str [ j ] == ' c ' && str [ i ] == ' a ' ) { i -- ; j ++ ; } else str [ ++ i ] = str [ j ++ ] ; } System . out . println ( new String ( str ) . substring ( 0 , i + 1 ) ) ; } public static void main ( String [ ] args ) { String str1 = " ad " ; stringFilter ( str1 . toCharArray ( ) ) ; String str2 = " acbac " ; stringFilter ( str2 . toCharArray ( ) ) ; String str3 = " aaac " ; stringFilter ( str3 . toCharArray ( ) ) ; String str4 = " react " ; stringFilter ( str4 . toCharArray ( ) ) ; String str5 = " aa " ; stringFilter ( str5 . toCharArray ( ) ) ; String str6 = " ababaac " ; stringFilter ( str6 . toCharArray ( ) ) ; } }


"Remove Invalid Parentheses | Java program to remove invalid parenthesis ; method checks if character is parenthesis ( openor closed ) ; method returns true if string contains valid parenthesis ; method to remove invalid parenthesis ; visit set to ignore already visited string ; queue to maintain BFS ; pushing given string as starting node into queue ; If answer is found , make level true so that valid string of only that level are processed . ; Removing parenthesis from str and pushing into queue , if not visited already ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isParenthesis ( char c ) { return ( ( c == ' ( ' ) || ( c == ' ) ' ) ) ; } static boolean isValidString ( String str ) { int cnt = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) cnt ++ ; else if ( str . charAt ( i ) == ' ) ' ) cnt -- ; if ( cnt < 0 ) return false ; } return ( cnt == 0 ) ; } static void removeInvalidParenthesis ( String str ) { if ( str . isEmpty ( ) ) return ; HashSet < String > visit = new HashSet < String > ( ) ; Queue < String > q = new LinkedList < > ( ) ; String temp ; boolean level = false ; q . add ( str ) ; visit . add ( str ) ; while ( ! q . isEmpty ( ) ) { str = q . peek ( ) ; q . remove ( ) ; if ( isValidString ( str ) ) { System . out . println ( str ) ; level = true ; } if ( level ) continue ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ! isParenthesis ( str . charAt ( i ) ) ) continue ; temp = str . substring ( 0 , i ) + str . substring ( i + 1 ) ; if ( ! visit . contains ( temp ) ) { q . add ( temp ) ; visit . add ( temp ) ; } } } } public static void main ( String [ ] args ) { String expression = " ( ) ( ) ) ( ) " ; removeInvalidParenthesis ( expression ) ; expression = " ( ) v ) " ; removeInvalidParenthesis ( expression ) ; } }


"Remove Nth node from end of the Linked List | Java program to delete nth node from last ; Structure of node ; Function to insert node in a linked list ; Function to remove nth node from last ; To store length of the linked list ; B > length , then we can 't remove node ; We need to remove head node ; Return head . next ; Remove len - B th node from starting ; This function prints contents of linked list starting from the given node ; Driver code"
How can the above be solved in Java?

 class GFG { static class Node { int data ; Node next ; } ; static Node create ( Node head , int x ) { Node temp , ptr = head ; temp = new Node ( ) ; temp . data = x ; temp . next = null ; if ( head == null ) head = temp ; else { while ( ptr . next != null ) { ptr = ptr . next ; } ptr . next = temp ; } return head ; } static Node removeNthFromEnd ( Node head , int B ) { int len = 0 ; Node tmp = head ; while ( tmp != null ) { len ++ ; tmp = tmp . next ; } if ( B > len ) { System . out . print ( " Length ▁ of ▁ the ▁ linked ▁ list ▁ is ▁ " + len ) ; System . out . print ( " ▁ we ▁ can ' t ▁ remove ▁ " + B + " th ▁ node ▁ from ▁ the " ) ; System . out . print ( " linked list
"); return head ; } else if ( B == len ) { return head . next ; } else { int diff = len - B ; Node prev = null ; Node curr = head ; for ( int i = 0 ; i < diff ; i ++ ) { prev = curr ; curr = curr . next ; } prev . next = curr . next ; return head ; } } static void display ( Node head ) { Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { Node head = null ; head = create ( head , 1 ) ; head = create ( head , 2 ) ; head = create ( head , 3 ) ; head = create ( head , 4 ) ; head = create ( head , 5 ) ; int n = 2 ; System . out . print ( "Linked list before modification:
"); display ( head ) ; head = removeNthFromEnd ( head , 2 ) ; System . out . print ( "Linked list after modification:
"); display ( head ) ; } }


"Remove Sub | Java program for the above approach ; Function to remove sub - directories from the given lists dir ; Store final result ; Sort the given directories ; Insert 1 st directory ; Iterating in directory ; Current directory ; Our previous valid directory ; Find length of previous directory ; If subdirectory is found ; Else store it in result valid directory ; Driver Code ; Given lists of directories dir [ ] ; Function Call"
How can the above be solved in Java?

 import java . io . * ; import java . util . * ; class GFG { static void eraseSubdirectory ( ArrayList < String > dir ) { ArrayList < String > res = new ArrayList < String > ( ) ; Collections . sort ( dir ) ; res . add ( dir . get ( 0 ) ) ; System . out . print ( " { " + dir . get ( 0 ) + " , ▁ " ) ; for ( int i = 1 ; i < dir . size ( ) ; i ++ ) { String curr = dir . get ( i ) ; String prev = res . get ( res . size ( ) - 1 ) ; int l = prev . length ( ) ; if ( curr . length ( ) > l && curr . charAt ( l ) == ' / ' && prev . equals ( curr . substring ( 0 , l ) ) ) continue ; res . add ( curr ) ; System . out . print ( curr + " , ▁ " ) ; } System . out . print ( "}
"); } public static void main ( String [ ] args ) { ArrayList < String > dir = new ArrayList < String > ( Arrays . asList ( " / a " , " / a / j " , " / c / d / e " , " / c / d " , " / b " ) ) ; eraseSubdirectory ( dir ) ; } }


"Remove a character from a string to make it a palindrome | Java program to check whether it is possible to make string palindrome by removing one character ; Utility method to check if substring from low to high is palindrome or not . ; This method returns - 1 if it is not possible to make string a palindrome . It returns - 2 if string is already a palindrome . Otherwise it returns index of character whose removal can make the whole string palindrome . ; Initialize low and right by both the ends of the string ; loop until low and high cross each other ; If both characters are equal then move both pointer towards end ; * If removing str [ low ] makes the * whole string palindrome . We basically * check if substring str [ low + 1. . high ] * is palindrome or not . ; * If removing str [ high ] makes the whole string * palindrome . We basically check if substring * str [ low + 1. . high ] is palindrome or not . ; We reach here when complete string will be palindrome if complete string is palindrome then return mid character ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isPalindrome ( String str , int low , int high ) { while ( low < high ) { if ( str . charAt ( low ) != str . charAt ( high ) ) return false ; low ++ ; high -- ; } return true ; } static int possiblePalinByRemovingOneChar ( String str ) { int low = 0 , high = str . length ( ) - 1 ; while ( low < high ) { if ( str . charAt ( low ) == str . charAt ( high ) ) { low ++ ; high -- ; } else { if ( isPalindrome ( str , low + 1 , high ) ) return low ; if ( isPalindrome ( str , low , high - 1 ) ) return high ; return - 1 ; } } return - 2 ; } public static void main ( String [ ] args ) { String str = " abecbea " ; int idx = possiblePalinByRemovingOneChar ( str ) ; if ( idx == - 1 ) System . out . println ( " Not ▁ Possible " ) ; else if ( idx == - 2 ) System . out . println ( " Possible ▁ without ▁ " + " removing ▁ any ▁ character " ) ; else System . out . println ( " Possible ▁ by ▁ removing " + " ▁ character ▁ at ▁ index ▁ " + idx ) ; } }


"Remove all 1 s from the adjacent left of 0 s in a Binary Array | Java implementation of the above approach ; Function to find the maximum number of 1 's before 0 ; Traverse the array ; If value is 1 ; If consecutive 1 followed by 0 , then update the maxCnt ; Print the maximum consecutive 1 's  followed by 0 ; Driver Code ; Function Call ; Function Call"
How can the above be solved in Java?

 class GFG { static void noOfMoves ( int arr [ ] , int n ) { int cnt = 0 ; int maxCnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { cnt ++ ; } else { if ( cnt != 0 ) { maxCnt = Math . max ( maxCnt , cnt ) ; cnt = 0 ; } } } System . out . print ( maxCnt + "
"); } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 } ; int N = arr . length ; noOfMoves ( arr , N ) ; int arr1 [ ] = { 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 } ; N = arr1 . length ; noOfMoves ( arr1 , N ) ; } }


"Remove all Fibonacci Nodes from a Circular Singly Linked List | Java program to delete all the Fibonacci nodes from the circular singly linked list ; Structure for a node ; Function to insert a node at the beginning of a Circular linked list ; Create a new node and make head as next of it . ; If linked list is not null then set the next of last node ; Find the node before head and update next of it . ; Point for the first node ; Delete the node from a Circular Linked list ; If node to be deleted is head node ; Traverse list till not found delete node ; Copy the address of the node ; Finally , free the memory occupied by del ; Function to find the maximum node of the circular linked list ; Pointer for traversing ; Initialize head to the current pointer ; Initialize min int value to max ; While the last node is not reached ; If current node data is greater for max then replace it ; Function to create hash table to check Fibonacci numbers ; Adding the first two elements to the hash ; Inserting the Fibonacci numbers into the hash ; Function to delete all the Fibonacci nodes from the singly circular linked list ; Find the largest node value in Circular Linked List ; Creating a hash containing all the Fibonacci numbers upto the maximum data value in the circular linked list ; Traverse the list till the end ; If the node ' s ▁ data ▁ is ▁ Fibonacci , ▁ ▁ delete ▁ node ▁ ' ptr ' ; Point to the next node ; Function to print nodes in a given Circular linked list ; Driver code ; Initialize lists as empty ; Created linked list will be 9.11 .34 .6 .13 .20"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; Node temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static void deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) head_ref = del . next ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; del = null ; return ; } static int largestElement ( Node head_ref ) { Node current ; current = head_ref ; int maxEle = Integer . MIN_VALUE ; do { if ( current . data > maxEle ) { maxEle = current . data ; } current = current . next ; } while ( current != head_ref ) ; return maxEle ; } static void createHash ( HashSet < Integer > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } static void deleteFibonacciNodes ( Node head ) { int maxEle = largestElement ( head ) ; HashSet < Integer > hash = new HashSet < Integer > ( ) ; createHash ( hash , maxEle ) ; Node ptr = head ; Node next ; do { if ( hash . contains ( ptr . data ) ) deleteNode ( head , ptr ) ; next = ptr . next ; ptr = next ; } while ( ptr != head ) ; } static void printList ( Node head ) { Node temp = head ; if ( head != null ) { do { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } while ( temp != head ) ; } } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 20 ) ; head = push ( head , 13 ) ; head = push ( head , 6 ) ; head = push ( head , 34 ) ; head = push ( head , 11 ) ; head = push ( head , 9 ) ; deleteFibonacciNodes ( head ) ; printList ( head ) ; } }


"Remove all duplicate adjacent characters from a string using Stack | Java program to implement the above approach ; Function to remove adjacent duplicate elements ; Store the String without duplicate elements ; Store the index of str ; Traverse the String str ; Checks if stack is empty or top of the stack is not equal to current character ; If top element of the stack is equal to the current character ; If stack is empty ; If stack is not Empty ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String ShortenString ( String str1 ) { Stack < Character > st = new Stack < Character > ( ) ; int i = 0 ; while ( i < str1 . length ( ) ) { if ( st . isEmpty ( ) || str1 . charAt ( i ) != st . peek ( ) ) { st . add ( str1 . charAt ( i ) ) ; i ++ ; } else { st . pop ( ) ; i ++ ; } } if ( st . isEmpty ( ) ) { return ( " Empty ▁ String " ) ; } else { String short_String = " " ; while ( ! st . isEmpty ( ) ) { short_String = st . peek ( ) + short_String ; st . pop ( ) ; } return ( short_String ) ; } } public static void main ( String [ ] args ) { String str1 = " azzxzy " ; System . out . print ( ShortenString ( str1 ) ) ; } }


"Remove all even parity nodes from a Doubly and Circular Singly Linked List | Java program to remove all the Even Parity Nodes from a circular singly linked list ; Structure for a node ; Function to insert a node at the beginning of a Circular linked list ; Create a new node and make head as next of it . ; If linked list is not null then set the next of last node ; Find the node before head and update next of it . ; Point for the first node ; Function to delete the node from a Circular Linked list ; If node to be deleted is head node ; Traverse list till not found delete node ; Copy the address of the node ; Finally , free the memory occupied by del ; Function that returns true if count of set bits in x is even ; Parity will store the count of set bits ; Function to delete all the Even Parity Nodes from the singly circular linked list ; Traverse the list till the end ; If the node ' s ▁ data ▁ has ▁ ▁ even ▁ parity , ▁ delete ▁ node ▁ ' ptr ' ; Point to the next node ; Function to print nodes in a given Circular linked list ; Driver code ; Initialize lists as empty ; Created linked list will be 11.9 .34 .6 .13 .21"
How can the above be solved in Java?

 class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; Node temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static void deleteNode ( Node head_ref , Node del ) { if ( head_ref == del ) head_ref = del . next ; Node temp = head_ref ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; System . gc ( ) ; return ; } static boolean isEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { if ( ( x & 1 ) != 0 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return true ; else return false ; } static void deleteEvenParityNodes ( Node head ) { if ( head == null ) return ; if ( head == head . next ) { if ( isEvenParity ( head . data ) ) head = null ; return ; } Node ptr = head ; Node next ; do { next = ptr . next ; if ( isEvenParity ( ptr . data ) ) deleteNode ( head , ptr ) ; ptr = next ; } while ( ptr != head ) ; if ( head == head . next ) { if ( isEvenParity ( head . data ) ) head = null ; return ; } } static void printList ( Node head ) { if ( head == null ) { System . out . print ( "Empty List
"); return ; } Node temp = head ; if ( head != null ) { do { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } while ( temp != head ) ; } } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 21 ) ; head = push ( head , 13 ) ; head = push ( head , 6 ) ; head = push ( head , 34 ) ; head = push ( head , 9 ) ; head = push ( head , 11 ) ; deleteEvenParityNodes ( head ) ; printList ( head ) ; } }


"Remove all leaf nodes from the binary search tree | Java program to delete leaf Node from binary search tree . ; Create a newNode in binary search tree . ; Insert a Node in binary search tree . ; Function for inorder traversal in a BST . ; Delete leaf nodes from binary search tree . ; Else recursively delete in left and right subtrees . ; Driver code"
How can the above be solved in Java?

class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( Node root , int data ) { if ( root == null ) return newNode ( data ) ; if ( data < root . data ) root . left = insert ( root . left , data ) ; else if ( data > root . data ) root . right = insert ( root . right , data ) ; return root ; } static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } } static Node leafDelete ( Node root ) { if ( root == null ) { return null ; } if ( root . left == null && root . right == null ) { return null ; } root . left = leafDelete ( root . left ) ; root . right = leafDelete ( root . right ) ; return root ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 20 ) ; insert ( root , 10 ) ; insert ( root , 5 ) ; insert ( root , 15 ) ; insert ( root , 30 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; System . out . println ( " Inorder ▁ before ▁ Deleting ▁ the ▁ leaf ▁ Node . ▁ " ) ; inorder ( root ) ; System . out . println ( ) ; leafDelete ( root ) ; System . out . println ( " INorder ▁ after ▁ Deleting ▁ the ▁ leaf ▁ Node . ▁ " ) ; inorder ( root ) ; } }


"Remove all nodes which don 't lie in any path with sum>= k | Class representing binary tree node ; for print traversal ; class to truncate binary tree recursive method to truncate binary tree ; base case ; recur for left and right subtree ; if node is a leaf node whose data is smaller than the sum we delete the leaf . An important thing to note is a non - leaf node can become leaf when its children are deleted . ; utility method to check if node is leaf ; Driver class to test above function"
How can the above be solved in Java?

class Node { int data ; Node left ; Node right ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } public void print ( Node root ) { if ( root == null ) return ; print ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; print ( root . right ) ; } } class BinaryTree { Node root ; public Node prune ( Node root , int sum ) { if ( root == null ) return null ; root . left = prune ( root . left , sum - root . data ) ; root . right = prune ( root . right , sum - root . data ) ; if ( isLeaf ( root ) ) { if ( sum > root . data ) root = null ; } return root ; } public boolean isLeaf ( Node root ) { if ( root == null ) return false ; if ( root . left == null && root . right == null ) return true ; return false ; } public class GFG { 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 ( 12 ) ; tree . root . right . right . left = new Node ( 10 ) ; tree . root . right . right . left . right = new Node ( 11 ) ; tree . root . left . left . right . left = new Node ( 13 ) ; tree . root . left . left . right . right = new Node ( 14 ) ; tree . root . left . left . right . right . left = new Node ( 15 ) ; System . out . println ( " Tree ▁ before ▁ truncation " ) ; tree . print ( tree . root ) ; tree . prune ( tree . root , 45 ) ; System . out . println ( " Tree after truncation "); tree . print ( tree . root ) ; } }


"Remove all nodes which don 't lie in any path with sum>= k | Java program to implement the above approach ; A utility function to get maximum of two integers ; A Binary Tree Node ; A utility function to create a new Binary Tree node with given data ; print the tree in LVR ( Inorder traversal ) way . ; Main function which truncates the binary tree . ; Base Case ; Initialize left and right sums as sum from root to this node ( including this node ) ; Recursively prune left and right subtrees ; Get the maximum of left and right sums ; If maximum is smaller than k , then this node must be deleted ; A wrapper over pruneUtil ( ) ; Driver Code ; k is 45"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int max ( int l , int r ) { return ( l > r ? l : r ) ; } static class Node { int data ; Node left , right ; } ; static class INT { int v ; INT ( int a ) { v = a ; } } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return node ; } static void print ( Node root ) { if ( root != null ) { print ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; print ( root . right ) ; } } static Node pruneUtil ( Node root , int k , INT sum ) { if ( root == null ) return null ; INT lsum = new INT ( sum . v + ( root . data ) ) ; INT rsum = new INT ( lsum . v ) ; root . left = pruneUtil ( root . left , k , lsum ) ; root . right = pruneUtil ( root . right , k , rsum ) ; sum . v = max ( lsum . v , rsum . v ) ; if ( sum . v < k ) { root = null ; } return root ; } static Node prune ( Node root , int k ) { INT sum = new INT ( 0 ) ; return pruneUtil ( root , k , sum ) ; } public static void main ( String args [ ] ) { int k = 45 ; 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 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 12 ) ; root . right . right . left = newNode ( 10 ) ; root . right . right . left . right = newNode ( 11 ) ; root . left . left . right . left = newNode ( 13 ) ; root . left . left . right . right = newNode ( 14 ) ; root . left . left . right . right . left = newNode ( 15 ) ; System . out . println ( "Tree before truncation
");  print ( root ) ; root = prune ( root , k ) ; System . out . println ( " Tree after truncation ");  print ( root ) ; } }


"Remove all occurrences of a word from a given string using Z | Java program for the above approach ; Function to fill the Z - array for str ; L Stores start index of window which matches with prefix of str ; R Stores end index of window which matches with prefix of str ; Iterate over the characters of str ; If i is greater thn R ; Update L and R ; If subString match with prefix ; Update R ; Update Z [ i ] ; Update R ; Update k ; if Z [ k ] is less than remaining interval ; Update Z [ i ] ; Start from R and check manually ; Update R ; Update Z [ i ] ; Update R ; Function to remove all the occurrences of word from str ; Create concatenated String " P $ T " ; Store Z array of concat ; Stores String , str by removing all the occurrences of word from str ; Stores length of word ; Traverse the array , Z [ ] ; if Z [ i + pSize + 1 ] equal to length of word ; Update i ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void getZarr ( String str , int Z [ ] ) { int n = str . length ( ) ; int k ; int L = 0 ; int R = 0 ; for ( int i = 1 ; i < n ; ++ i ) { if ( i > R ) { L = R = i ; while ( R < n && str . charAt ( R - L ) == str . charAt ( R ) ) { R ++ ; } Z [ i ] = R - L ; R -- ; } else { k = i - L ; if ( Z [ k ] < R - i + 1 ) { Z [ i ] = Z [ k ] ; } else { L = i ; while ( R < n && str . charAt ( R - L ) == str . charAt ( R ) ) { R ++ ; } Z [ i ] = R - L ; R -- ; } } } } static String goodStr ( String str , String word ) { String concat = word + " $ " + str ; int l = concat . length ( ) ; int [ ] Z = new int [ l ] ; getZarr ( concat , Z ) ; String res = " " ; int pSize = word . length ( ) ; for ( int i = 0 ; i < l ; ++ i ) { if ( i + pSize < l - 1 && Z [ i + pSize + 1 ] == pSize ) { i += pSize - 1 ; } else if ( i < str . length ( ) ) { res += str . charAt ( i ) ; } } return res ; } public static void main ( String [ ] args ) { String str = " Z - kmalgorithmkmiskmkmkmhelpfulkminkmsearching " ; String word = " km " ; System . out . print ( goodStr ( str , word ) ) ; } }


"Remove all occurrences of any element for maximum array sum | Java program to convert fractional decimal to binary number ; Find total sum and frequencies of elements ; Find minimum value to be subtracted . ; Find maximum sum after removal ; Drivers code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int maxSumArray ( int arr [ ] , int n ) { int sum = 0 ; Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } int minimum = Integer . MAX_VALUE ; for ( Map . Entry < Integer , Integer > x : m . entrySet ( ) ) minimum = Math . min ( minimum , x . getValue ( ) * x . getKey ( ) ) ; return ( sum - minimum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 3 , 3 , 2 , 2 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( maxSumArray ( arr , n ) ) ; } }


"Remove all occurrences of duplicates from a sorted Linked List | class to create Linked lIst ; Function to insert data nodes into the Linked List at the front ; Function to remove all occurrences of duplicate elements ; Create a dummy node that acts like a fake head of list pointing to the original head ; Dummy node points to the original head ; Until the current and previous values are same , keep updating current ; If current has unique value i . e current is not updated , Move the prev pointer to next node ; When current is updated to the last duplicate value of that segment , make prev the next of current ; Update original head to the next of dummy node ; Function to print the list elements ; Driver code"
How can the above be solved in Java?

class LinkedList { Node head = null ; class Node { int val ; Node next ; Node ( int v ) { val = v ; next = null ; } } public void insert ( int data ) { Node new_node = new Node ( data ) ; new_node . next = head ; head = new_node ; } public void removeAllDuplicates ( ) { Node dummy = new Node ( 0 ) ; dummy . next = head ; Node prev = dummy ; Node current = head ; while ( current != null ) { while ( current . next != null && prev . next . val == current . next . val ) current = current . next ; if ( prev . next == current ) prev = prev . next ; else prev . next = current . next ; current = current . next ; } head = dummy . next ; } public void printList ( ) { Node trav = head ; if ( head == null ) System . out . print ( " ▁ List ▁ is ▁ empty " ) ; while ( trav != null ) { System . out . print ( trav . val + " ▁ " ) ; trav = trav . next ; } } public static void main ( String [ ] args ) { LinkedList ll = new LinkedList ( ) ; ll . insert ( 53 ) ; ll . insert ( 53 ) ; ll . insert ( 49 ) ; ll . insert ( 49 ) ; ll . insert ( 35 ) ; ll . insert ( 28 ) ; ll . insert ( 28 ) ; ll . insert ( 23 ) ; System . out . println ( " Before ▁ removal ▁ of ▁ duplicates " ) ; ll . printList ( ) ; ll . removeAllDuplicates ( ) ; System . out . println ( " After removal of duplicates "); ll . printList ( ) ; } }


"Remove all subtrees consisting only of even valued nodes from a Binary Tree | Java program for the above approach ; Node of the tree ; Function to create a new node ; Function to print tree level wise ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root ; Print front of queue and remove it from queue ; If left child is present ; Otherwise ; If right child is present ; Otherwise ; Function to remove subtrees ; Base Case ; Search for required condition in left and right half ; If the node is even and leaf node ; Driver Code ; Function Call ; Print answer"
How can the above be solved in Java?

import java . util . * ; class GFG { static class node { int data ; node left , right ; } ; static node newNode ( int key ) { node temp = new node ( ) ; temp . data = key ; temp . left = temp . right = null ; return ( temp ) ; } static void printLevelOrder ( node root ) { if ( root == null ) return ; Queue < node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { node temp = q . peek ( ) ; System . out . print ( temp . data + " ▁ " ) ; q . remove ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; } else if ( temp . right != null ) { System . out . print ( " null ▁ " ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } else if ( temp . left != null ) { System . out . print ( " null ▁ " ) ; } } } static node pruneTree ( node root ) { if ( root == null ) { return null ; } root . left = pruneTree ( root . left ) ; root . right = pruneTree ( root . right ) ; if ( root . data % 2 == 0 && root . right == null && root . left == null ) return null ; return root ; } public static void main ( String [ ] args ) { node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 8 ) ; root . left . right = newNode ( 10 ) ; root . right = newNode ( 7 ) ; root . right . left = newNode ( 12 ) ; root . right . right = newNode ( 5 ) ; node newRoot = pruneTree ( root ) ; printLevelOrder ( newRoot ) ; } }


"Remove all the Even Digit Sum Nodes from a Circular Singly Linked List | Java program to remove all the Even Digit Sum Nodes from a circular singly linked list ; Structure for a node ; Function to insert a node at the beginning of a Circular linked list ; Create a new node and make head as next of it . ; If linked list is not null then set the next of last node ; Find the node before head and update next of it . ; Point for the first node ; Function to delete the node from a Circular Linked list ; If node to be deleted is head node ; Traverse list till not found delete node ; Copy the address of the node ; Finally , free the memory occupied by del ; Function to find the digit sum for a number ; Function to delete all the Even Digit Sum Nodes from the singly circular linked list ; Traverse the list till the end ; If the node ' s ▁ data ▁ is ▁ Fibonacci , ▁ ▁ delete ▁ node ▁ ' ptr ' ; Point to the next node ; Function to print nodes in a given Circular linked list ; Driver code ; Initialize lists as empty ; Created linked list will be 9.11 .34 .6 .13 .21"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; Node temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static void deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) head_ref = del . next ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; del = null ; return ; } static int digitSum ( int num ) { int sum = 0 ; while ( num > 0 ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } static void deleteEvenDigitSumNodes ( Node head ) { Node ptr = head ; Node next ; do { if ( ! ( digitSum ( ptr . data ) % 2 == 1 ) ) deleteNode ( head , ptr ) ; next = ptr . next ; ptr = next ; } while ( ptr != head ) ; } static void printList ( Node head ) { Node temp = head ; if ( head != null ) { do { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } while ( temp != head ) ; } } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 21 ) ; head = push ( head , 13 ) ; head = push ( head , 6 ) ; head = push ( head , 34 ) ; head = push ( head , 11 ) ; head = push ( head , 9 ) ; deleteEvenDigitSumNodes ( head ) ; printList ( head ) ; } }


"Remove all the prime numbers from the given array | Java implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print the elements of the array ; Function to remove all the prime numbers ; Generate primes ; Traverse the array ; If the current element is prime ; Shift all the elements on the right of it to the left ; Decrease the loop counter by 1 to check the shifted element ; Decrease the length ; Print the updated array ; Driver code"
How can the above be solved in Java?

class GFG { static int sz = ( int ) 1e5 ; static boolean [ ] isPrime = new boolean [ sz + 1 ] ; static void sieve ( ) { for ( int i = 0 ; i < sz + 1 ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static void printArray ( int arr [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void removePrimes ( int arr [ ] , int len ) { sieve ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( isPrime [ arr [ i ] ] ) { for ( int j = i ; j < len - 1 ; j ++ ) { arr [ j ] = arr [ j + 1 ] ; } i -- ; len -- ; } } printArray ( arr , len ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 5 , 3 , 8 , 7 , 10 , 11 , 14 , 15 } ; int len = arr . length ; removePrimes ( arr , len ) ; } }


"Remove all zero | Java program for the above approach ; Function to remove the rows or columns from the matrix which contains all 0 s elements ; Stores count of rows ; col [ i ] : Stores count of 0 s in current column ; row [ i ] : Stores count of 0 s in current row ; Traverse the matrix ; Stores count of 0 s in current row ; Update col [ j ] ; Update count ; Update row [ i ] ; Traverse the matrix ; If all elements of current row is 0 ; If all elements of current column is 0 ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { static void removeZeroRowCol ( int arr [ ] [ ] ) { int n = arr . length ; int col [ ] = new int [ n + 1 ] ; int row [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) { int count = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( arr [ i ] [ j ] == 1 ) col [ j ] += 1 ; else col [ j ] += 0 ; if ( arr [ i ] [ j ] == 1 ) count += 1 ; else count += 0 ; } row [ i ] = count ; } for ( int i = 0 ; i < n ; ++ i ) { if ( row [ i ] == 0 ) { continue ; } for ( int j = 0 ; j < n ; ++ j ) { if ( col [ j ] != 0 ) System . out . print ( arr [ i ] [ j ] ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 } } ; removeZeroRowCol ( arr ) ; } }


"Remove an occurrence of most frequent array element exactly K times | Java program to implement the above approach ; Function to print the most frequent array element exactly K times ; Stores frequency array element ; Count frequency of array element ; Maximum array element ; Traverse the Map ; Find the element with maximum frequency ; If the frequency is maximum , store that number in element ; Print element as it contains the element having highest frequency ; Decrease the frequency of the maximum array element ; Reduce the number of operations ; Driver code ; Given array ; Size of the array ; Given K"
How can the above be solved in Java?

import java . util . * ; class GFG { static void maxFreqElements ( int arr [ ] , int N , int K ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } while ( K > 0 ) { int max = 0 ; int element = 0 ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { if ( i . getValue ( ) > max ) { max = i . getValue ( ) ; element = i . getKey ( ) ; } } System . out . print ( element + " ▁ " ) ; if ( mp . containsKey ( element ) ) { mp . put ( element , mp . get ( element ) + 1 ) ; } else { mp . put ( element , 1 ) ; } K -- ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 1 , 4 , 1 } ; int N = arr . length ; int K = 2 ; maxFreqElements ( arr , N , K ) ; } }


"Remove array elements to reduce frequency of each array element to at most K | Java program for the above approach ; Function to remove array elements such that frequency of each distinct array element is at most K ; Base Case ; Stores index of array element by removing the array element such that the frequency of array elements is at most K ; Traverse the array ; If j < k or arr [ i ] > arr [ j - k ] ; Update arr [ j ] ; Remove array elements ; Function to print the array ; Traverse the array ; Utility Function to remove array elements such that frequency of each distinct array element is at most K ; Print updated array ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int [ ] RemoveElemArr ( int [ ] arr , int n , int k ) { if ( n == 0 n == 1 ) return arr ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( j < k arr [ i ] > arr [ j - k ] ) { arr [ j ++ ] = arr [ i ] ; } } while ( arr . length > j ) { arr = Arrays . copyOfRange ( arr , 0 , arr . length - 1 ) ; } return arr ; } static void printArray ( int [ ] arr ) { for ( int i = 0 ; i < arr . length ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void UtilRemov ( int [ ] arr , int n , int k ) { arr = RemoveElemArr ( arr , n , k ) ; printArray ( arr ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 } ; int k = 2 ; int n = arr . length ; UtilRemov ( arr , n , k ) ; } }


"Remove array end element to maximize the sum of product | Java program to find maximum score we can get by removing elements from either end . ; If only one element left . ; If already calculated , return the value . ; Computing Maximum value when element at index i and index j is to be choosed . ; Driven Program"
How can the above be solved in Java?

public class GFG { static final int MAX = 50 ; static int solve ( int dp [ ] [ ] , int a [ ] , int low , int high , int turn ) { if ( low == high ) { return a [ low ] * turn ; } if ( dp [ low ] [ high ] != 0 ) { return dp [ low ] [ high ] ; } dp [ low ] [ high ] = Math . max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a , low , high - 1 , turn + 1 ) ) ; return dp [ low ] [ high ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 1 , 5 , 2 } ; int n = arr . length ; int dp [ ] [ ] = new int [ MAX ] [ MAX ] ; System . out . println ( solve ( dp , arr , 0 , n - 1 , 1 ) ) ; } }


"Remove brackets from an algebraic string containing + and | Java program to simplify algebraic string ; Function to simplify the string ; resultant string of max length equal to length of input string ; create empty stack ; If top is 1 , flip the operator ; If top is 0 , append the same operator ; x is opposite to the top of stack ; push value equal to top of the stack ; If closing parentheses pop the stack once ; copy the character to the result ; Driver program"
How can the above be solved in Java?

import java . util . * ; class GfG { static String simplify ( String str ) { int len = str . length ( ) ; char res [ ] = new char [ len ] ; int index = 0 , i = 0 ; Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( 0 ) ; while ( i < len ) { if ( str . charAt ( i ) == ' + ' ) { if ( s . peek ( ) == 1 ) res [ index ++ ] = ' - ' ; if ( s . peek ( ) == 0 ) res [ index ++ ] = ' + ' ; } else if ( str . charAt ( i ) == ' - ' ) { if ( s . peek ( ) == 1 ) res [ index ++ ] = ' + ' ; else if ( s . peek ( ) == 0 ) res [ index ++ ] = ' - ' ; } else if ( str . charAt ( i ) == ' ( ' && i > 0 ) { if ( str . charAt ( i - 1 ) == ' - ' ) { int x = ( s . peek ( ) == 1 ) ? 0 : 1 ; s . push ( x ) ; } else if ( str . charAt ( i - 1 ) == ' + ' ) s . push ( s . peek ( ) ) ; } else if ( str . charAt ( i ) == ' ) ' ) s . pop ( ) ; else res [ index ++ ] = str . charAt ( i ) ; i ++ ; } return new String ( res ) ; } public static void main ( String [ ] args ) { String s1 = " a - ( b + c ) " ; String s2 = " a - ( b - c - ( d + e ) ) - f " ; System . out . println ( simplify ( s1 ) ) ; System . out . println ( simplify ( s2 ) ) ; } }


"Remove characters from a numeric string such that string becomes divisible by 8 | Java program to remove digits from a numeric string such that the number becomes divisible by 8 ; Function that return true if sub is a sub - sequence in s ; Function to return a multiple of 8 formed after removing 0 or more characters from the given string ; Iterate over all multiples of 8 ; If current multiple exists as a subsequence in the given string ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean checkSub ( String sub , String s ) { int j = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( sub . charAt ( j ) == s . charAt ( i ) ) j ++ ; return j == sub . length ( ) ; } static int getMultiple ( String s ) { for ( int i = 0 ; i < 1E3 ; i += 8 ) { if ( checkSub ( Integer . toString ( i ) , s ) ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { String s = "3454" ; System . out . println ( getMultiple ( s ) ) ; } }


"Remove characters from given string whose frequencies are a Prime Number | Java program for the above approach ; Function to perform the seive of eratosthenes algorithm ; Initialize all entries in prime [ ] as true ; Initialize 0 and 1 as non prime ; Traversing the prime array ; If i is prime ; All multiples of i must be marked false as they are non prime ; Function to remove characters which have prime frequency in the String ; Length of the String ; Create a boolean array prime ; Sieve of Eratosthenes ; Stores the frequency of character ; Storing the frequencies ; New String that will be formed ; Removing the characters which have prime frequencies ; If the character has prime frequency then skip ; Else concatenate the character to the new String ; Print the modified String ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void SieveOfEratosthenes ( boolean [ ] prime , int n ) { for ( int i = 0 ; i <= n ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = 2 ; i * j <= n ; j ++ ) { prime [ i * j ] = false ; } } } } static void removePrimeFrequencies ( char [ ] s ) { int n = s . length ; boolean [ ] prime = new boolean [ n + 1 ] ; SieveOfEratosthenes ( prime , n ) ; HashMap < Character , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( m . containsKey ( s [ i ] ) ) { m . put ( s [ i ] , m . get ( s [ i ] ) + 1 ) ; } else { m . put ( s [ i ] , 1 ) ; } } String new_String = " " ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( prime [ m . get ( s [ i ] ) ] ) continue ; new_String += s [ i ] ; } System . out . print ( new_String ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; removePrimeFrequencies ( str . toCharArray ( ) ) ; } }


"Remove characters from string that appears strictly less than K times | Java program to reduce the string by removing the characters which appears less than k times ; Function to reduce the string by removing the characters which appears less than k times ; Hash table initialised to 0 ; Increment the frequency of the character ; create a new empty string ; Append the characters which appears more than equal to k times ; Driver Code"
How can the above be solved in Java?

class GFG { final static int MAX_CHAR = 26 ; static String removeChars ( String str , int k ) { int hash [ ] = new int [ MAX_CHAR ] ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; ++ i ) { hash [ str . charAt ( i ) - ' a ' ] ++ ; } String res = " " ; for ( int i = 0 ; i < n ; ++ i ) { if ( hash [ str . charAt ( i ) - ' a ' ] >= k ) { res += str . charAt ( i ) ; } } return res ; } static public void main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 2 ; System . out . println ( removeChars ( str , k ) ) ; } }


"Remove consecutive alphabets which are in same case | Java program to remove the consecutive characters from a string that are in same case ; Function to return the modified string ; Traverse through the remaining characters in the string ; If the current and the previous characters are not in the same case then take the character ; Driver code"
How can the above be solved in Java?

class GFG { static String removeChars ( String s ) { String modifiedStr = " " ; modifiedStr += s . charAt ( 0 ) ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( Character . isUpperCase ( s . charAt ( i ) ) && Character . isLowerCase ( s . charAt ( i - 1 ) ) || Character . isLowerCase ( s . charAt ( i ) ) && Character . isUpperCase ( s . charAt ( i - 1 ) ) ) modifiedStr += s . charAt ( i ) ; } return modifiedStr ; } public static void main ( String [ ] args ) { String s = " GeeksForGeeks " ; System . out . println ( removeChars ( s ) ) ; } }


"Remove consecutive vowels from string | Java program for printing sentence without repetitive vowels ; function which returns True or False for occurrence of a vowel ; this compares vowel with character ' c ' ; function to print resultant string ; print 1 st character ; loop to check for each character ; comparison of consecutive characters ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static boolean is_vow ( char c ) { return ( c == ' a ' ) || ( c == ' e ' ) || ( c == ' i ' ) || ( c == ' o ' ) || ( c == ' u ' ) ; } static void removeVowels ( String str ) { System . out . print ( str . charAt ( 0 ) ) ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) if ( ( ! is_vow ( str . charAt ( i - 1 ) ) ) || ( ! is_vow ( str . charAt ( i ) ) ) ) System . out . print ( str . charAt ( i ) ) ; } public static void main ( String [ ] args ) { String str = " geeks ▁ for ▁ geeks " ; removeVowels ( str ) ; } }


"Remove duplicates from a given string | Java program to create a unique string ; Function to make the string unique ; loop to traverse the string and check for repeating chars using IndexOf ( ) method in Java ; character at i 'th index of s ; if c is present in str , it returns the index of c , else it returns - 1 ; adding c to str if - 1 is returned ; Driver code ; Input string with repeating chars"
How can the above be solved in Java?

import java . util . * ; class IndexOf { public static String unique ( String s ) { String str = new String ( ) ; int len = s . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = s . charAt ( i ) ; if ( str . indexOf ( c ) < 0 ) { str += c ; } } return str ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( unique ( s ) ) ; } }


"Remove duplicates from a given string | Java program to remove duplicate character from character array and print in sorted order ; Create a set using String characters excluding ' \0' ; HashSet doesn 't allow repetition of elements ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void removeDuplicate ( char str [ ] , int n ) { HashSet < Character > s = new LinkedHashSet < > ( n - 1 ) ; for ( char x : str ) s . add ( x ) ; for ( char x : s ) System . out . print ( x ) ; } public static void main ( String [ ] args ) { char str [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = str . length ; removeDuplicate ( str , n ) ; } }


"Remove duplicates from a given string | Java program to remove duplicate character from character array and print in sorted order ; Used as index in the modified string ; Traverse through all characters ; Check if str [ i ] is present before it ; If not present , then add it to result . ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String removeDuplicate ( char str [ ] , int n ) { int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) { if ( str [ i ] == str [ j ] ) { break ; } } if ( j == i ) { str [ index ++ ] = str [ i ] ; } } return String . valueOf ( Arrays . copyOf ( str , index ) ) ; } public static void main ( String [ ] args ) { char str [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = str . length ; System . out . println ( removeDuplicate ( str , n ) ) ; } }


"Remove duplicates from a given string | access time in unordered_map on is O ( 1 ) generally if no collisions occur and therefore it helps us check if an element exists in a String in O ( 1 ) time complexity with constant space . ; driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static char [ ] removeDuplicates ( char [ ] s , int n ) { Map < Character , Integer > exists = new HashMap < > ( ) ; String st = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! exists . containsKey ( s [ i ] ) ) { st += s [ i ] ; exists . put ( s [ i ] , 1 ) ; } } return st . toCharArray ( ) ; } public static void main ( String [ ] args ) { char s [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = s . length ; System . out . print ( removeDuplicates ( s , n ) ) ; } }


"Remove duplicates from a sorted linked list | Java program for the above approach ; Function to insert a node at the beginging of the linked * list ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Function to print nodes in a given linked list ; Function to remove duplicates ; Driver Code ; Created linked list will be 11 -> 11 -> 11 -> 13 -> 13 -> 20"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class Node { int data ; Node next ; Node ( ) { data = 0 ; next = null ; } } class GFG { static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; head_ref = new_node ; return head_ref ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } static void removeDuplicates ( Node head ) { HashMap < Integer , Boolean > track = new HashMap < > ( ) ; Node temp = head ; while ( temp != null ) { if ( ! track . containsKey ( temp . data ) ) { System . out . print ( temp . data + " ▁ " ) ; } track . put ( temp . data , true ) ; temp = temp . next ; } } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 20 ) ; head = push ( head , 13 ) ; head = push ( head , 13 ) ; head = push ( head , 11 ) ; head = push ( head , 11 ) ; head = push ( head , 11 ) ; System . out . print ( " Linked ▁ list ▁ before ▁ duplicate ▁ removal ▁ " ) ; printList ( head ) ; System . out . print ( " Linked list after duplicate removal "); removeDuplicates ( head ) ; } }


"Remove duplicates from a string in O ( 1 ) extra space | Java implementation of above approach ; Function to remove duplicates ; keeps track of visited characters ; gets character value ; keeps track of length of resultant String ; check if Xth bit of counter is unset ; mark current character as visited ; Driver code"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static char [ ] removeDuplicatesFromString ( String string ) { int counter = 0 ; char [ ] str = string . toCharArray ( ) ; int i = 0 ; int size = str . length ; int x ; int length = 0 ; while ( i < size ) { x = str [ i ] - 97 ; if ( ( counter & ( 1 << x ) ) == 0 ) { str [ length ] = ( char ) ( ' a ' + x ) ; counter = counter | ( 1 << x ) ; length ++ ; } i ++ ; } return Arrays . copyOfRange ( str , 0 , length ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( removeDuplicatesFromString ( str ) ) ; } }


"Remove duplicates from a string in O ( 1 ) extra space | Java implementation of above approach ; Method to remove duplicates ; table to keep track of visited characters ; to keep track of end index of resultant string ; Driver code"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static char [ ] removeDuplicatesFromString ( String string ) { int [ ] table = new int [ 256 ] ; char [ ] chars = string . toCharArray ( ) ; int endIndex = 0 ; for ( int i = 0 ; i < chars . length ; i ++ ) { if ( table [ chars [ i ] ] == 0 ) { table [ chars [ i ] ] = - 1 ; chars [ endIndex ++ ] = chars [ i ] ; } } return Arrays . copyOfRange ( chars , 0 , endIndex ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( removeDuplicatesFromString ( str ) ) ; } }


"Remove duplicates from an unsorted doubly linked list | Java implementation to remove duplicates from an unsorted doubly linked list ; a node of the doubly linked list ; Function to delete a node in a Doubly Linked List . head_ref - . pointer to head node pointer . del - . pointer to node to be deleted . ; base case ; If node to be deleted is head node ; Change next only if node to be deleted is NOT the last node ; Change prev only if node to be deleted is NOT the first node ; function to remove duplicates from an unsorted doubly linked list ; if DLL is empty or if it contains only a single node ; pick elements one by one ; Compare the picked element with the rest of the elements ; if duplicate , then delete it ; store pointer to the node next to ' ptr2' ; delete node pointed to by ' ptr2' ; update ' ptr2' ; else simply move to the next node ; Function to insert a node at the beginning of the Doubly Linked List ; allocate node ; put in the data ; since we are adding at the beginning , prev is always null ; link the old list off the new node ; change prev of head node to new node ; move the head to point to the new node ; Function to print nodes in a given doubly linked list ; if list is empty ; Driver Code ; Create the doubly linked list : 8 < .4 < .4 < .6 < .4 < .8 < .4 < .10 < .12 < .12 ; remove duplicate nodes"
How can the above be solved in Java?

 class GFG { static class Node { int data ; Node next ; Node prev ; } static Node deleteNode ( Node head_ref , Node del ) { if ( head_ref == null del == null ) return head_ref ; if ( head_ref == del ) head_ref = del . next ; if ( del . next != null ) del . next . prev = del . prev ; if ( del . prev != null ) del . prev . next = del . next ; return head_ref ; } static Node removeDuplicates ( Node head_ref ) { if ( ( head_ref ) == null || ( head_ref ) . next == null ) return head_ref ; ; Node ptr1 , ptr2 ; for ( ptr1 = head_ref ; ptr1 != null ; ptr1 = ptr1 . next ) { ptr2 = ptr1 . next ; while ( ptr2 != null ) { if ( ptr1 . data == ptr2 . data ) { Node next = ptr2 . next ; head_ref = deleteNode ( head_ref , ptr2 ) ; ptr2 = next ; } else ptr2 = ptr2 . next ; } } return head_ref ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = ( head_ref ) ; if ( ( head_ref ) != null ) ( head_ref ) . prev = new_node ; ( head_ref ) = new_node ; return head_ref ; } static void printList ( Node head ) { if ( head == null ) System . out . print ( " Doubly ▁ Linked ▁ list ▁ empty " ) ; while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 12 ) ; head = push ( head , 12 ) ; head = push ( head , 10 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 4 ) ; head = push ( head , 6 ) ; head = push ( head , 4 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; System . out . print ( "Original Doubly linked list:
"); printList ( head ) ; head = removeDuplicates ( head ) ; System . out . print ( " Doubly linked list after " ▁ + ▁ " removing duplicates : "); printList ( head ) ; } }