codeparrot/xlcost-text-to-code huggingface dataset:
Smallest index that splits an array into two subarrays with equal product | Java program for the above approach ; Function to find the smallest index that splits the array into two subarrays with equal product ; Stores the product of the array ; Traverse the given array ; Stores the product of left and the right subarrays ; Traverse the given array ; Update the products ; Check if product is equal ; Print resultant index ; If no partition exists , then print - 1. ; Driver Code
import java . util . * ; class GFG { static void prodEquilibrium ( int arr [ ] , int N ) { int product = 1 ; for ( int i = 0 ; i < N ; i ++ ) { product *= arr [ i ] ; } int left = 1 ; int right = product ; for ( int i = 0 ; i < N ; i ++ ) { left = left * arr [ i ] ; right = right / arr [ i ] ; if ( left == right ) { System . out . print ( i + 1 + "NEW_LINE"); return ; } } System . out . print ( - 1 + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 2 , 1 } ; int N = arr . length ; prodEquilibrium ( arr , N ) ; } }
Count triplets from a sorted array having difference between adjacent elements equal to D | Java program for the above approach ; Function to count the number of triplets having difference between adjacent elements equal to D ; Stores the frequency of array elements ; Stores the count of resultant triplets ; Traverse the array ; Check if arr [ i ] - D and arr [ i ] - 2 * D exists in the Hashmap or not ; Update the value of ans ; Increase the frequency of the current element ; Return the resultant count ; Driver Code
import java . util . * ; class GFG { static int countTriplets ( int D , int [ ] arr ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; int ans = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( freq . containsKey ( arr [ i ] - D ) && freq . containsKey ( arr [ i ] - 2 * D ) ) { ans += freq . get ( arr [ i ] - D ) * freq . get ( arr [ i ] - 2 * D ) ; } if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 5 , 7 , 8 , 10 } ; int D = 1 ; System . out . print ( countTriplets ( D , arr ) ) ; } }
Find the array element having equal sum of Prime Numbers on its left and right | Java program for the above approach ; Function to find an index in the array having sum of prime numbers to its left and right equal ; Stores the maximum value present in the array ; Stores all positive elements which are <= max_value ; If 1 is present ; Remove 1 ; Sieve of Eratosthenes to store all prime numbers which are <= max_value in the Map ; Erase non - prime numbers ; Stores the sum of prime numbers from left ; Stores the sum of prime numbers to the left of each index ; Stores the sum of prime numbers to the left of the current index ; Add current value to the prime sum if the current value is prime ; Stores the sum of prime numbers from right ; Stores the sum of prime numbers to the right of each index ; Stores the sum of prime numbers to the right of the current index ; Add current value to the prime sum if the current value is prime ; Traverse through the two arrays to find the index ; Compare the values present at the current index ; Return the index where both the values are same ; No index is found . ; Driver code ; Given array arr [ ] ; Size of Array ; Function Call
import java . lang . * ; import java . util . * ; class GFG { static int find_index ( int arr [ ] , int N ) { int max_value = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { max_value = Math . max ( max_value , arr [ i ] ) ; } Map < Integer , Integer > store = new HashMap < > ( ) ; for ( int i = 1 ; i <= max_value ; i ++ ) { store . put ( i , store . getOrDefault ( i , 0 ) + 1 ) ; } if ( store . containsKey ( 1 ) ) { store . remove ( 1 ) ; } for ( int i = 2 ; i <= Math . sqrt ( max_value ) ; i ++ ) { int multiple = 2 ; while ( ( i * multiple ) <= max_value ) { if ( store . containsKey ( i * multiple ) ) { store . remove ( i * multiple ) ; } multiple ++ ; } } int prime_sum_from_left = 0 ; int [ ] first_array = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { first_array [ i ] = prime_sum_from_left ; if ( store . containsKey ( arr [ i ] ) ) { prime_sum_from_left += arr [ i ] ; } } int prime_sum_from_right = 0 ; int [ ] second_array = new int [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { second_array [ i ] = prime_sum_from_right ; if ( store . containsKey ( arr [ i ] ) ) { prime_sum_from_right += arr [ i ] ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( first_array [ i ] == second_array [ i ] ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 4 , 7 , 6 , 13 , 1 , 5 } ; int N = arr . length ; System . out . println ( find_index ( arr , N ) ) ; } }
Convert an array to reduced form | Set 3 ( Binary Search ) | Java program for the above approach ; Function to find the reduced form of the given array arr [ ] ; Stores the sorted form of the the given array arr [ ] ; Sort the array brr [ ] ; Traverse the given array arr [ ] ; Perform the Binary Search ; Calculate the value of mid ; Print the current index and break ; Update the value of l ; Update the value of r ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void convert ( int arr [ ] , int n ) { int brr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; Arrays . sort ( brr ) ; for ( int i = 0 ; i < n ; i ++ ) { int l = 0 , r = n - 1 , mid ; while ( l <= r ) { mid = ( l + r ) / 2 ; if ( brr [ mid ] == arr [ i ] ) { System . out . print ( mid + " ▁ " ) ; break ; } else if ( brr [ mid ] < arr [ i ] ) { l = mid + 1 ; } else { r = mid - 1 ; } } } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 15 , 12 , 11 , 50 } ; int N = arr . length ; convert ( arr , N ) ; } }
Find the array element having equal count of Prime Numbers on its left and right | Java program for the above approach ; Function to find the index of the array such that the count of prime numbers to its either ends are same ; Store the maximum value in the array ; Traverse the array arr [ ] ; Stores all the numbers ; Iterate over the range [ 1 , Max ] ; Increment the value of st [ i ] ; Removes 1 from the map St ; Perform Sieve of Prime Numbers ; While i * j is less than the maxValue ; If i * j is in map St ; Erase the value ( i * j ) ; Increment the value of j ; Stores the count of prime from index 0 to i ; Stores the count of prime numbers ; Traverse the array arr [ ] ; If arr [ i ] is present in the map st ; Stores the count of prime from index i to N - 1 ; Stores the count of prime numbers ; Iterate over the range [ 0 , N - 1 ] in reverse order ; If arr [ i ] is in map st ; Iterate over the range [ 0 , N - 1 ] ; If prefix [ i ] is equal to the Suffix [ i ] ; Return - 1 if no such index is present ; Driver code
import java . util . HashMap ; public class GFG { static int findIndex ( int arr [ ] , int N ) { int maxValue = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { maxValue = Math . max ( maxValue , arr [ i ] ) ; } HashMap < Integer , Integer > St = new HashMap < > ( ) ; for ( int i = 1 ; i <= maxValue ; i ++ ) { St . put ( i , St . getOrDefault ( i , 0 ) + 1 ) ; } if ( St . containsKey ( 1 ) ) { St . remove ( 1 ) ; } for ( int i = 2 ; i <= Math . sqrt ( maxValue ) ; i ++ ) { int j = 2 ; while ( ( i * j ) <= maxValue ) { if ( St . containsKey ( i * j ) ) { St . remove ( i * j ) ; } j ++ ; } } int LeftCount = 0 ; int Prefix [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Prefix [ i ] = LeftCount ; if ( St . containsKey ( arr [ i ] ) ) { LeftCount ++ ; } } int RightCount = 0 ; int Suffix [ ] = new int [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { Suffix [ i ] = RightCount ; if ( St . containsKey ( arr [ i ] ) ) { RightCount ++ ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( Prefix [ i ] == Suffix [ i ] ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 7 , 5 , 10 , 1 , 8 } ; int N = arr . length ; System . out . println ( findIndex ( arr , N ) ) ; } }
Numbers of pairs from an array whose average is also present in the array | Java program for the above approach ; Function to count the number of pairs from the array having sum S ; Stores the total count of pairs whose sum is 2 * S ; Generate all possible pairs and check their sums ; If the sum is S , then increment the count ; Return the total count of pairs ; Function to count of pairs having whose average exists in the array ; Initialize the count ; Use set to remove duplicates ; Add elements in the set ; For every sum , count all possible pairs ; Return the total count ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static int getCountPairs ( int arr [ ] , int N , int S ) { int count = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { for ( int j = i + 1 ; j < arr . length ; j ++ ) { if ( ( arr [ i ] + arr [ j ] ) == S ) count ++ ; } } return count ; } public static int countPairs ( int arr [ ] , int N ) { int count = 0 ; HashSet < Integer > S = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) S . add ( arr [ i ] ) ; for ( int ele : S ) { int sum = 2 * ele ; count += getCountPairs ( arr , N , sum ) ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 5 , 1 , 3 , 5 } ; int N = arr . length ; System . out . print ( countPairs ( arr , N ) ) ; } }
Count numbers up to N having exactly 5 divisors | Java program for the above approach ; Function to calculate the value of ( x ^ y ) using binary exponentiation ; Stores the value of x ^ y ; Base Case ; If y is odd multiply x with result ; Otherwise , divide y by 2 ; Function to perform the Sieve Of Eratosthenes to find the prime number over the range [ 1 , 10 ^ 5 ] ; If prime [ p ] is not changed then it is a prime ; Set all the multiples of p to non - prime ; Iterate over the range [ 1 , MAX ] ; Store all the prime number ; Function to find the primes having only 5 divisors ; Base Case ; First value of the pair has the prime number and the second value has the count of primes till that prime numbers ; Precomputing all the primes ; Perform the Binary search ; Calculate the fourth power of the curr and prev ; Return value of mid ; Return value of mid - 1 ; Update the value of high ; Update the value of low ; Driver code
import java . util . Vector ; public class GFG { static int MAX = ( int ) 1e5 ; public static class pair { long first ; long second ; pair ( long first , long second ) { this . first = first ; this . second = second ; } } static long power ( long x , long y ) { long res = 1 ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) ; y = y >> 1 ; x = ( x * x ) ; } return res ; } static void SieveOfEratosthenes ( Vector < pair > v ) { boolean prime [ ] = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } int num = 1 ; for ( int i = 1 ; i <= MAX ; i ++ ) { if ( prime [ i ] ) { v . add ( new pair ( i , num ) ) ; num ++ ; } } } static long countIntegers ( long n ) { if ( n < 16 ) { return 0 ; } Vector < pair > v = new Vector < > ( ) ; SieveOfEratosthenes ( v ) ; int low = 0 ; int high = v . size ( ) - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; long curr = power ( v . get ( mid ) . first , 4 ) ; long prev = power ( v . get ( mid - 1 ) . first , 4 ) ; if ( curr == n ) { return v . get ( mid ) . second ; } else if ( curr > n && prev <= n ) { return v . get ( mid - 1 ) . second ; } else if ( curr > n ) { high = mid - 1 ; } else { low = mid + 1 ; } } return 0 ; } public static void main ( String [ ] args ) { long N = 100 ; System . out . println ( countIntegers ( N ) ) ; } }
Sort array of strings after sorting each string after removing characters whose frequencies are not a powers of 2 | Java program for the above approach ; Function to check if N is power of 2 or not ; Base Case ; Return true if N is power of 2 ; Function to print array of strings in ascending order ; Sort strings in ascending order ; Print the array ; Function to sort the strings after modifying each string according to the given conditions ; Store the frequency of each characters of the string ; Stores the required array of strings ; Traverse the array of strings ; Temporary string ; Stores frequency of each alphabet of the string ; Update frequency of S [ i ] [ j ] ; Traverse the map freq ; Check if the frequency of i . first is a power of 2 ; Update string st ; Clear the map ; Null string ; Sort the string in descending order ; Update res ; Print the array of strings ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean isPowerOfTwo ( int n ) { if ( n == 0 ) return false ; return ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) == Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) ) ; } static void printArray ( ArrayList < String > res ) { Collections . sort ( res ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) { System . out . print ( res . get ( i ) + " ▁ " ) ; } } static void sortedStrings ( String S [ ] , int N ) { HashMap < Character , Integer > freq = new HashMap < > ( ) ; ArrayList < String > res = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { String st = " " ; for ( int j = 0 ; j < S [ i ] . length ( ) ; j ++ ) { freq . put ( S [ i ] . charAt ( j ) , freq . getOrDefault ( S [ i ] . charAt ( j ) , 0 ) + 1 ) ; } for ( char ch : freq . keySet ( ) ) { if ( isPowerOfTwo ( freq . get ( ch ) ) ) { for ( int j = 0 ; j < freq . get ( ch ) ; j ++ ) { st += ch ; } } } freq . clear ( ) ; if ( st . length ( ) == 0 ) continue ; char myCharArr [ ] = st . toCharArray ( ) ; Arrays . sort ( myCharArr ) ; String ns = " " ; for ( int j = myCharArr . length - 1 ; j >= 0 ; -- j ) ns += myCharArr [ j ] ; res . add ( ns ) ; } printArray ( res ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " aaacbb " , " geeks " , " aaa " } ; int N = arr . length ; sortedStrings ( arr , N ) ; } }
Modify string by replacing all occurrences of given characters by specified replacing characters | Java program for the above approach ; Function to modify given string by replacing characters ; Store the size of string and the number of pairs ; Initialize 2 character arrays ; Traverse the string s Update arrays arr [ ] and brr [ ] ; Traverse the array of pairs p ; a -> Character to be replaced b -> Replacing character ; Iterate over the range [ 0 , 25 ] ; If it is equal to current character , then replace it in the array b ; Print the array brr [ ] ; Driver code
import java . util . * ; class GFG { static void replaceCharacters ( String s , char [ ] [ ] p ) { int n = s . length ( ) , k = p . length ; char [ ] arr = new char [ 26 ] ; char [ ] brr = new char [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ s . charAt ( i ) - ' a ' ] = s . charAt ( i ) ; brr [ s . charAt ( i ) - ' a ' ] = s . charAt ( i ) ; } for ( int j = 0 ; j < k ; j ++ ) { char a = p [ j ] [ 0 ] , b = p [ j ] [ 1 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] == a ) { brr [ i ] = b ; } } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( brr [ s . charAt ( i ) - ' a ' ] ) ; } } public static void main ( String [ ] args ) { String S = " aabbgg " ; char [ ] [ ] P = { { ' a ' , ' b ' } , { ' b ' , ' g ' } , { ' g ' , ' a ' } } ; replaceCharacters ( S , P ) ; } }
Smallest Semi | Java approach for the above approach ; Function to find all the prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to find the smallest semi - prime number having a difference between any of its two divisors at least N ; Stores the prime numbers ; Fill the prime array ; Initialize the first divisor ; Find the value of the first prime number ; Initialize the second divisor ; Find the second prime number ; Print the semi - prime number ; Driver Code
class GFG { static int MAX = 100001 ; static void SieveOfEratosthenes ( boolean prime [ ] ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < MAX ; i += p ) prime [ i ] = false ; } } } static void smallestSemiPrime ( int n ) { boolean [ ] prime = new boolean [ MAX ] ; for ( int i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } SieveOfEratosthenes ( prime ) ; int num1 = n + 1 ; while ( prime [ num1 ] != true ) { num1 ++ ; } int num2 = num1 + n ; while ( prime [ num2 ] != true ) { num2 ++ ; } System . out . print ( num1 * num2 ) ; } public static void main ( String [ ] args ) { int N = 2 ; smallestSemiPrime ( N ) ; } }
Check if a number can be represented as product of two positive perfect cubes | Java program for the above approach ; Function to check if N can be represented as the product of two perfect cubes or not ; Stores the perfect cubes ; Traverse the Map ; Stores the first number ; Stores the second number ; Search the pair for the first number to obtain product N from the Map ; If N cannot be represented as the product of the two positive perfect cubes ; Driver code
import java . lang . * ; import java . util . * ; class GFG { static void productOfTwoPerfectCubes ( int N ) { Map < Integer , Integer > cubes = new HashMap < > ( ) ; for ( int i = 1 ; i * i * i <= N ; i ++ ) cubes . put ( i * i * i , i ) ; for ( Map . Entry < Integer , Integer > itr : cubes . entrySet ( ) ) { int firstNumber = itr . getKey ( ) ; if ( N % itr . getKey ( ) == 0 ) { int secondNumber = N / itr . getKey ( ) ; if ( cubes . containsKey ( secondNumber ) ) { System . out . println ( " Yes " ) ; return ; } } } System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 216 ; productOfTwoPerfectCubes ( N ) ; } }
Check if a number can be represented as product of two positive perfect cubes | Java program for the above approach ; Function to check if the number N can be represented as the product of two perfect cubes or not ; If cube of cube_root is N ; Otherwise , print No ; Driver Code
import java . lang . * ; class GFG { public static void productOfTwoPerfectCubes ( double N ) { double cube_root ; cube_root = Math . round ( Math . cbrt ( N ) ) ; if ( cube_root * cube_root * cube_root == N ) { System . out . println ( " Yes " ) ; return ; } else { System . out . println ( " No " ) ; return ; } } public static void main ( String args [ ] ) { double N = 216 ; productOfTwoPerfectCubes ( N ) ; } }
Size of smallest square that contains N non | Java program for the above approach ; Function to check if side of square X can pack all the N rectangles or not ; Find the number of rectangle it can pack ; If val is atleast N , then return true ; Otherwise , return false ; Function to find the size of the smallest square that can contain N rectangles of dimensions W * H ; Stores the lower bound ; Stores the upper bound ; Iterate until i is less than j ; Calculate the mid value ; If the current size of square cam contain N rectangles ; Otherwise , update i ; Return the minimum size of the square required ; Driver code ; Function Call
class GFG { static boolean bound ( int w , int h , int N , int x ) { int val = ( x / w ) * ( x / h ) ; if ( val >= N ) return true ; else return false ; } static int FindSquare ( int N , int W , int H ) { int i = 1 ; int j = W * H * N ; while ( i < j ) { int mid = i + ( j - i ) / 2 ; if ( bound ( W , H , N , mid ) ) j = mid ; else i = mid + 1 ; } return j ; } public static void main ( String [ ] args ) { int W = 2 ; int H = 3 ; int N = 10 ; System . out . print ( FindSquare ( N , W , H ) ) ; } }
Count integers up to N that are equal to at least 2 nd power of any integer exceeding 1 | Java program for the above approach ; Function to count the integers up to N that can be represented as a ^ b , where a & b > 1 ; Initialize a HashSet ; Iterating over the range [ 2 , sqrt ( N ) ] ; Generate all possible power of x ; Multiply x by i ; If the generated number lies in the range [ 1 , N ] then insert it in HashSet ; Print the total count ; Driver Code
import java . util . HashSet ; public class GFG { static void printNumberOfPairs ( int N ) { HashSet < Integer > st = new HashSet < Integer > ( ) ; for ( int i = 2 ; i * i <= N ; i ++ ) { int x = i ; while ( x <= N ) { x *= i ; if ( x <= N ) { st . add ( x ) ; } } } System . out . println ( st . size ( ) ) ; } public static void main ( String args [ ] ) { int N = 10000 ; printNumberOfPairs ( N ) ; } }
Maximize product of lengths of strings having no common characters | Java program for the above approach ; Function to count the number of set bits in the integer n ; Stores the count of set bits in n ; Return the count ; Function to find the maximum product of pair of strings having no common characters ; Stores the integer equivalent of the strings ; Traverse the array of strings ; Traverse the current string ; Store the current bit position in bits [ i ] ; Store the required result ; Traverse the array , bits [ ] to get all unique pairs ( i , j ) ; Check whether the strings have no common characters ; Update the overall maximum product ; Print the maximum product ; Driver Code
import java . io . * ; class GFG { public static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static void maximumProduct ( String [ ] words ) { int [ ] bits = new int [ words . length ] ; for ( int i = 0 ; i < words . length ; i ++ ) { for ( int j = 0 ; j < words [ i ] . length ( ) ; j ++ ) { bits [ i ] = bits [ i ] | 1 << ( words [ i ] . charAt ( j ) - ' a ' ) ; } } int result = 0 ; for ( int i = 0 ; i < bits . length ; i ++ ) { for ( int j = i + 1 ; j < bits . length ; j ++ ) { if ( ( bits [ i ] & bits [ j ] ) == 0 ) { int L = countSetBits ( bits [ i ] ) ; int R = countSetBits ( bits [ j ] ) ; result = Math . max ( L * R , result ) ; } } } System . out . println ( result ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " abcw " , " baz " , " foo " , " bar " , " xtfn " , " abcdef " } ; maximumProduct ( arr ) ; } }
Modify a sentence by reversing order of occurrences of all Palindrome Words | Java implementation of the above approach ; Function to check if a string S is a palindrome ; Function to print the modified string after reversing teh order of occurrences of all palindromic words in the sentence ; Stores the palindromic words ; Stores the words in the list ; Traversing the list ; If current word is a palindrome ; Update newlist ; Reverse the newlist ; Traverse the list ; If current word is a palindrome ; Update lis [ i ] ; Increment j ; Print the updated sentence ; Driver code
import java . util . * ; class GFG { static boolean palindrome ( String str ) { int st = 0 ; int ed = str . length ( ) - 1 ; while ( st < ed ) { if ( str . charAt ( st ) == str . charAt ( ed ) ) { st ++ ; ed -- ; } else return false ; } return true ; } static void printReverse ( String sentence ) { ArrayList < String > newlist = new ArrayList < String > ( ) ; ArrayList < String > lis = new ArrayList < String > ( ) ; String temp = " " ; for ( char x : sentence . toCharArray ( ) ) { if ( x == ' ▁ ' ) { lis . add ( temp ) ; temp = " " ; } else temp += x ; } lis . add ( temp ) ; for ( String x : lis ) { if ( palindrome ( x ) ) newlist . add ( x ) ; } Collections . reverse ( newlist ) ; int j = 0 ; for ( int i = 0 ; i < lis . size ( ) ; i ++ ) { if ( palindrome ( lis . get ( i ) ) ) { lis . set ( i , newlist . get ( j ) ) ; j = j + 1 ; } } for ( String x : lis ) System . out . print ( x + " ▁ " ) ; } public static void main ( String [ ] args ) { String sentence = " mom ▁ and ▁ dad ▁ went ▁ to ▁ eye ▁ hospital " ; printReverse ( sentence ) ; } }