codeparrot/xlcost-text-to-code huggingface dataset:
Minimum LCM of all subarrays of length at least 2 | Java program for the above approach ; Function to find LCM pf two numbers ; Initialise lcm value ; Check for divisibility of a and b by the lcm ; Function to find the Minimum LCM of all subarrays of length greater than 1 ; Store the minimum LCM ; Traverse the array ; Find LCM of consecutive element ; Check if the calculated LCM is less than the minLCM then update it ; Print the minimum LCM ; Driver Code ; Given array arr [ ] ; Size of the array ; Function call
import java . util . * ; class GFG { static int LCM ( int a , int b ) { int lcm = a > b ? a : b ; while ( true ) { if ( lcm % a == 0 && lcm % b == 0 ) break ; else lcm ++ ; } return lcm ; } static void findMinLCM ( int arr [ ] , int n ) { int minLCM = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int val = LCM ( arr [ i ] , arr [ i + 1 ] ) ; if ( val < minLCM ) { minLCM = val ; } } System . out . print ( minLCM + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 12 , 16 , 20 , 24 } ; int n = arr . length ; findMinLCM ( arr , n ) ; } }
Minimum integer with at most K bits set such that their bitwise AND with N is maximum | Java program for the above approach ; Function to find the integer with maximum bitwise with N ; Store answer in the bitset Initialized with 0 ; To maintain the count of set bits that should exceed k ; Start traversing from the Most significant if that bit is set in n then we will set in our answer i . e in X ; Checking if the ith bit is set in n or not ; Converting into integer ; Driver function ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static int find_max ( int n , int k ) { int [ ] X = new int [ 32 ] ; int cnt = 0 ; for ( int i = 31 ; i >= 0 && cnt != k ; i -- ) { if ( ( n & ( 1 << i ) ) != 0 ) { X [ i ] = 1 ; cnt ++ ; } } String s = " " ; for ( int i = 31 ; i >= 0 ; i -- ) s += X [ i ] == 0 ? '0' : '1' ; return Integer . parseInt ( s , 2 ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 2 ; System . out . println ( find_max ( n , k ) ) ; } }
Convert string to integer without using any in | Java program for the above approach ; Function to convert string to integer without using functions ; Initialize a variable ; Iterate till length of the string ; Subtract 48 from the current digit ; Print the answer ; Driver code ; Given string of number ; Function Call
class GFG { public static void convert ( String s ) { int num = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) num = num * 10 + ( ( int ) s . charAt ( i ) - 48 ) ; System . out . print ( num ) ; } public static void main ( String [ ] args ) { String s = "123" ; convert ( s ) ; } }
Permutation of Array such that sum of adjacent elements are not divisible by 3 | Java implementation to find the permutation of the array such that sum of adjacent elements is not divisible by 3 ; Function to segregate numbers based on their remainder when divided by three ; Loop to iterate over the elements of the given array ; Condition to check the remainder of the number ; Function to find the permutation of the array such that sum of adjacent elements is not divisible by 3 ; Condition to check when it 's impossible to arrange ; Condition to check when there are no zeros , and only ones or only twos ; Array to store the permutation ; Place the ones on alternate places in the answer array , leaving spaces for zeros remainder elements in the array ; Adding a zero to connect it with a two ; Place the twos on alternate places in the answer array , leaving spaces for zeros ; Fill the zeros finally , between the ones and the twos ; Print the arrangement of the array ; Function to solve the problem ; As there can be only 3 remainders ; Function Call ; Driver Code
import java . util . * ; class GFG { static final int hell = 1000000007 ; static final int N = 100005 ; static int c_0 , c_1 , c_2 ; static void count_k ( int [ ] arr , Stack < Integer > ones , Stack < Integer > twos , Stack < Integer > zeros ) { for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] % 3 == 0 ) { c_0 ++ ; zeros . add ( arr [ i ] ) ; } else if ( arr [ i ] % 3 == 1 ) { c_1 ++ ; ones . add ( arr [ i ] ) ; } else { c_2 ++ ; twos . add ( arr [ i ] ) ; } } return ; } static void printArrangement ( int [ ] arr , Stack < Integer > ones , Stack < Integer > twos , Stack < Integer > zeros ) { if ( ( c_0 == 0 && c_1 != 0 && c_2 != 0 ) c_0 > c_1 + c_2 + 1 ) { System . out . print ( " - 1" ) ; return ; } if ( c_0 == 0 ) { for ( int i = 0 ; i < arr . length ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } return ; } int i , j ; int [ ] ans = new int [ N ] ; Arrays . fill ( ans , - 1 ) ; for ( i = 1 , j = 0 ; j < c_1 ; i += 2 , j ++ ) { ans [ i ] = ones . peek ( ) ; ones . pop ( ) ; } ans [ i - 1 ] = zeros . peek ( ) ; zeros . pop ( ) ; c_0 -- ; for ( j = 0 ; j < c_2 ; j ++ , i += 2 ) { ans [ i ] = twos . peek ( ) ; twos . pop ( ) ; } for ( int k = 0 ; c_0 > 0 ; k += 2 ) { if ( ans [ k ] == - 1 ) { ans [ k ] = zeros . peek ( ) ; c_0 -- ; } } for ( int i1 = 0 ; i1 < N ; i1 ++ ) { if ( ans [ i1 ] != - 1 ) System . out . print ( ans [ i1 ] + " ▁ " ) ; } return ; } static void solve ( int n , int [ ] arr ) { Stack < Integer > ones = new Stack < Integer > ( ) ; Stack < Integer > zeros = new Stack < Integer > ( ) ; Stack < Integer > twos = new Stack < Integer > ( ) ; c_0 = 0 ; c_1 = 0 ; c_2 = 0 ; count_k ( arr , ones , twos , zeros ) ; printArrangement ( arr , ones , twos , zeros ) ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; solve ( n , arr ) ; } }
Count of unique digits in a given number N | Java program for the above approach ; Function that returns the count of unique digits of number N ; Initialize a variable to store count of unique digits ; Initialize cnt array to store digit count ; Iterate through digits of N ; Retrieve the last digit of N ; Increase the count of the last digit ; Remove the last digit of N ; Iterate through the cnt array ; If frequency of digit is 1 ; Increment the count of unique digits ; Return the count of unique digit ; Driver Code ; Given Number N ; Function Call
import java . util . * ; class GFG { public static void countUniqueDigits ( int N ) { int res = 0 ; int cnt [ ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ; while ( N > 0 ) { int rem = N % 10 ; cnt [ rem ] ++ ; N = N / 10 ; } for ( int i = 0 ; i < cnt . length ; i ++ ) { if ( cnt [ i ] == 1 ) { res ++ ; } } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int N = 2234262 ; countUniqueDigits ( N ) ; } }
Check if frequency of each element in given array is unique or not | Java code for the above approach ; Function to check whether the frequency of elements in array is unique or not . ; Freq map will store the frequency of each element of the array ; Store the frequency of each element from the array ; Check whether frequency of any two or more elements are same or not . If yes , return false ; Return true if each frequency is unique ; Driver Code ; Given array arr [ ] ; Function call ; Print the result
import java . util . * ; class GFG { static boolean checkUniqueFrequency ( int arr [ ] , int n ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } } HashSet < Integer > uniqueFreq = new HashSet < Integer > ( ) ; for ( Map . Entry < Integer , Integer > i : freq . entrySet ( ) ) { if ( uniqueFreq . contains ( i . getValue ( ) ) ) return false ; else uniqueFreq . add ( i . getValue ( ) ) ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 5 , 5 } ; int n = arr . length ; boolean res = checkUniqueFrequency ( arr , n ) ; if ( res ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
Sum of first N natural numbers with all powers of 2 added twice | Java program to implement the above approach ; Function to raise N to the power P and return the value ; Function to calculate the log base 2 of an integer ; Calculate log2 ( N ) indirectly using log ( ) method ; Function to calculate and return the required sum ; Sum of first N natural numbers ; Sum of all powers of 2 up to N ; Driver Code
import java . util . * ; import java . lang . Math ; class GFG { static double power ( int N , int P ) { return Math . pow ( N , P ) ; } public static int log2 ( int N ) { int result = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) ; return result ; } static double specialSum ( int n ) { double sum = n * ( n + 1 ) / 2 ; int a = log2 ( n ) ; sum = sum + power ( 2 , a + 1 ) - 1 ; return sum ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( specialSum ( n ) ) ; } }
Rearrange an array such that product of every two consecutive elements is a multiple of 4 | Java program to rearray array elements such that the product of every two consecutive elements is a multiple of ; Function to rearrange array elements such that the every two consecutive elements is a multiple of 4 ; If element is odd ; Odd ; If element is divisible by 4 ; Divisible by 4 ; If element is not divisible by 4 ; Even but not divisible by 4 ; Condition for rearrangement to be possible ; Print ODD . get ( i ) and FOUR . get ( i ) consecutively ; Print the remaining FOUR . get ( i ) , if any ; Condition for rearrangement to be possible ; Print ODD . get ( i ) and FOUR . get ( i ) consecutively ; Print the remaining FOUR . get ( i ) , if any ; Print the NON_FOUR . get ( i ) elements at the end ; No possible configuration ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static void Permute ( Vector < Integer > arr , int n ) { int odd = 0 , four = 0 ; int non_four = 0 ; Vector < Integer > ODD = new Vector < Integer > ( ) ; Vector < Integer > FOUR = new Vector < Integer > ( n ) ; Vector < Integer > NON_FOUR = new Vector < Integer > ( n ) ; for ( int x : arr ) { if ( x % 2 != 0 ) { odd ++ ; ODD . add ( x ) ; } else if ( x % 4 == 0 ) { four ++ ; FOUR . add ( x ) ; } else { non_four ++ ; NON_FOUR . add ( x ) ; } } if ( non_four == 0 && four >= odd - 1 ) { int x = ODD . size ( ) ; int y = FOUR . size ( ) ; int i ; for ( i = 0 ; i < x ; i ++ ) { System . out . print ( ODD . get ( i ) + " ▁ " ) ; if ( i < y ) System . out . print ( FOUR . get ( i ) + " ▁ " ) ; } while ( i < y ) System . out . print ( FOUR . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } else if ( non_four > 0 && four >= odd ) { int x = ODD . size ( ) ; int y = FOUR . size ( ) ; int i ; for ( i = 0 ; i < x ; i ++ ) { System . out . print ( ODD . get ( i ) + " ▁ " ) ; if ( i < y ) System . out . print ( FOUR . get ( i ) + " ▁ " ) ; } while ( i < y ) System . out . print ( FOUR . get ( i ) + " ▁ " ) ; for ( int j = 0 ; j < ( int ) NON_FOUR . size ( ) ; j ++ ) System . out . print ( NON_FOUR . get ( j ) + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " Not ▁ Possible " ) ; } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < Integer > ( ) ; arr . add ( 2 ) ; arr . add ( 7 ) ; arr . add ( 1 ) ; arr . add ( 8 ) ; arr . add ( 2 ) ; arr . add ( 8 ) ; Permute ( arr , arr . size ( ) ) ; } }
Construct the smallest possible Array with given Sum and XOR | Java program implementation of the approach ; Function to find array ; Array not possible ; Array possible with exactly 1 or no element ; Checking array with two elements possible or not . ; Driver code ; Given sum and value of Bitwise XOR ; Function call
import java . util . * ; import java . io . * ; class GFG { static void findArray ( int sum , int xorr ) { if ( xorr > sum sum % 2 != xorr % 2 ) { System . out . println ( " No ▁ Array ▁ Possible " ) ; return ; } if ( sum == xorr ) { if ( sum == 0 ) System . out . println ( " Array ▁ is ▁ empty ▁ " + " with ▁ size ▁ 0" ) ; else System . out . println ( " Array ▁ size ▁ is ▁ " + 1 ) ; System . out . println ( " Array ▁ is ▁ " + sum ) ; return ; } int mid = ( sum - xorr ) / 2 ; if ( xorr == 1 && mid == 1 ) { System . out . println ( " Array ▁ size ▁ is ▁ " + 3 ) ; System . out . println ( " Array ▁ is ▁ " + xorr + " ▁ " + mid + " ▁ " + mid ) ; } else { System . out . println ( " Array ▁ size ▁ is ▁ " + 2 ) ; System . out . println ( " Array ▁ is ▁ " + ( xorr + mid ) + " ▁ " + mid ) ; } } public static void main ( String [ ] args ) { int sum = 4 , xorr = 2 ; findArray ( sum , xorr ) ; } }
Smallest number greater than X which is K | Java program to find the smallest K periodic integer greater than X ; Function to find the smallest K periodic integer greater than X ; Stores the number in a temporary String ; Set X [ i ] = X [ i - k ] for i > k ; Start from the current index ; Loop upto N change X [ j ] to X [ i ] ; Return X if current Value is greater than original value ; Find the first digit not equal to 9 ; Increment X [ i ] ; Set POS to current index ; Change X [ i ] to 0 for all indices from POS + 1 to K ; Set X [ i ] = X [ i - k ] for i > k ; Loop upto N change X [ j ] to X [ i ] ; Return the final String ; Driver Code
import java . util . * ; class GFG { static String Kperiodicinteger ( char [ ] X , int N , int K ) { String temp = String . valueOf ( X ) ; for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } if ( String . valueOf ( X ) . compareTo ( temp ) >= 0 ) { return String . valueOf ( X ) ; } int POS = 0 ; for ( int i = K - 1 ; i >= 0 ; i -- ) { if ( X [ i ] != '9' ) { X [ i ] ++ ; POS = i ; break ; } } for ( int i = POS + 1 ; i < K ; i ++ ) { X [ i ] = '0' ; } for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } return String . valueOf ( X ) ; } public static void main ( String [ ] args ) { int N = 4 , K = 2 ; String X = "1215" ; System . out . print ( Kperiodicinteger ( X . toCharArray ( ) , N , K ) ) ; } }
Maximum cost path in an Undirected Graph such that no edge is visited twice in a row | Java program for the above approach ; To store the resulting sum of the cost ; To store largest cost leaf vertex ; DFS Traversal to find the update the maximum cost of from any node to leaf ; Mark vertex as visited ; Store vertex initial cost ; Initially assuming edge not to be traversed ; Back edge found so , edge can be part of traversal ; New vertex is found ; Bitwise AND the current check with the returned check by the previous DFS Call ; Adds parent and its children cost ; Updates total cost of parent including child nodes ; Edge is part of the cycle ; Add cost of vertex to the answer ; Updates the largest cost leaf vertex ; Function to find the maximum cost from source vertex such that no two edges is traversed twice ; DFS call ; Print the maximum cost ; Driver Code ; Cost Array ; Given Graph ; Given Source Node ; Function call
import java . util . * ; class GFG { static int N = 100000 ; static int canTake ; static int best ; static int [ ] dp = new int [ N ] ; static boolean [ ] vis = new boolean [ N ] ; static boolean dfs ( Vector < Integer > [ ] g , int [ ] cost , int u , int pre ) { vis [ u ] = true ; dp [ u ] = cost [ u ] ; boolean check = true ; int cur = cost [ u ] ; for ( int x : g [ u ] ) { if ( vis [ x ] && x != pre ) { check = false ; } else if ( ! vis [ x ] ) { check = dfs ( g , cost , x , u ) ? false : true ; cur = Math . max ( cur , cost [ u ] + dp [ x ] ) ; } } dp [ u ] = cur ; if ( ! check ) { canTake += cost [ u ] ; } else { best = Math . max ( best , dp [ u ] ) ; } return check ; } static void FindMaxCost ( Vector < Integer > [ ] g , int [ ] cost , int source ) { dfs ( g , cost , source , - 1 ) ; System . out . print ( canTake + best ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int cost [ ] = { 2 , 2 , 8 , 6 , 9 } ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] g = new Vector [ n ] ; for ( int i = 0 ; i < g . length ; i ++ ) g [ i ] = new Vector < Integer > ( ) ; g [ 0 ] . add ( 1 ) ; g [ 1 ] . add ( 0 ) ; g [ 0 ] . add ( 2 ) ; g [ 2 ] . add ( 0 ) ; g [ 0 ] . add ( 3 ) ; g [ 3 ] . add ( 0 ) ; g [ 1 ] . add ( 2 ) ; g [ 2 ] . add ( 1 ) ; g [ 1 ] . add ( 4 ) ; g [ 4 ] . add ( 1 ) ; int source = 1 ; FindMaxCost ( g , cost , source ) ; } }
Reverse a subarray of the given array to minimize the sum of elements at even position | Java implementation to reverse a subarray of the given array to minimize the sum of elements at even position ; Function that will give the max negative value ; Check for count greater than 0 as we require only negative solution ; Function to print the minimum sum ; Taking sum of only even index elements ; Initialize two vectors v1 , v2 ; v1 will keep account for change if 1 th index element goes to 0 ; v2 will keep account for change if 1 th index element goes to 2 ; Get the max negative value ; Driver code
import java . util . * ; class GFG { static final int N = 5 ; static int after_rev ( Vector < Integer > v ) { int mini = 0 , count = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { count += v . get ( i ) ; if ( count > 0 ) count = 0 ; if ( mini > count ) mini = count ; } return mini ; } static void print ( int arr [ ] ) { int sum = 0 ; for ( int i = 0 ; i < N ; i += 2 ) sum += arr [ i ] ; Vector < Integer > v1 , v2 ; v1 = new Vector < Integer > ( ) ; v2 = new Vector < Integer > ( ) ; for ( int i = 0 ; i + 1 < N ; i += 2 ) v1 . add ( arr [ i + 1 ] - arr [ i ] ) ; for ( int i = 1 ; i + 1 < N ; i += 2 ) v2 . add ( arr [ i ] - arr [ i + 1 ] ) ; int change = Math . min ( after_rev ( v1 ) , after_rev ( v2 ) ) ; if ( change < 0 ) sum += change ; System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 4 , 3 , 0 } ; print ( arr ) ; } }
Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal | Java program for the above approach ; Utility function to find the answer ; Stores the frequencies of array elements ; Stores the possible values of X ; Generate possible positive values of X ; Initialize answer to MAX value ; Flag to check if the current element of the set can be considered ; If the frequency of an element in A [ ] is not equal to that in B [ ] after the operation ; Current set element cannot be considered ; Update minimum value of X ; Driver Code
import java . util . * ; class GFG { static int moduloEquality ( int A [ ] , int B [ ] , int n , int m ) { HashMap < Integer , Integer > mapA = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > mapB = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mapA . containsKey ( A [ i ] ) ) { mapA . put ( A [ i ] , mapA . get ( A [ i ] ) + 1 ) ; } else { mapA . put ( A [ i ] , 1 ) ; } if ( mapB . containsKey ( B [ i ] ) ) { mapB . put ( B [ i ] , mapB . get ( B [ i ] ) + 1 ) ; } else { mapB . put ( B [ i ] , 1 ) ; } } HashSet < Integer > possibleValues = new HashSet < Integer > ( ) ; int FirstElement = B [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = A [ i ] ; possibleValues . add ( cur > FirstElement ? m - cur + FirstElement : FirstElement - cur ) ; } int ans = Integer . MAX_VALUE ; for ( int it : possibleValues ) { boolean possible = true ; for ( Map . Entry < Integer , Integer > it2 : mapA . entrySet ( ) ) { if ( it2 . getValue ( ) != mapB . get ( ( it2 . getKey ( ) + it ) % m ) ) { possible = false ; break ; } } if ( possible ) { ans = Math . min ( ans , it ) ; } } return ans ; } public static void main ( String [ ] args ) { int n = 4 ; int m = 3 ; int A [ ] = { 0 , 0 , 2 , 1 } ; int B [ ] = { 2 , 0 , 1 , 1 } ; System . out . print ( moduloEquality ( A , B , n , m ) + "NEW_LINE"); } }
Count all indices of cyclic regular parenthesis | Java program for the above approach ; Function to find all indices which cyclic shift leads to get balanced parenthesis ; Create auxiliary array ; Finding prefix sum and minimum element ; Update the minimum element ; Check if count of ' ( ' and ' ) ' are equal ; Find count of minimum element ; Find the frequency of mn ; Return the count ; Driver code ; Given string S ; length of the string S
import java . util . * ; class GFG { static int countCyclicShifts ( String S , int n ) { int [ ] aux = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { if ( S . charAt ( i ) == ' ( ' ) aux [ i ] = 1 ; else aux [ i ] = - 1 ; } int mn = aux [ 0 ] ; for ( int i = 1 ; i < n ; ++ i ) { aux [ i ] += aux [ i - 1 ] ; mn = Math . min ( mn , aux [ i ] ) ; } if ( aux [ n - 1 ] != 0 ) return 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( aux [ i ] == mn ) count ++ ; } return count ; } public static void main ( String [ ] args ) { String S = " ) ( ) ( " ; int N = S . length ( ) ; System . out . print ( countCyclicShifts ( S , N ) ) ; } }
Maximize Profit by trading stocks based on given rate per day | Java program for the above approach ; Function to find the maximum profit ; Start from the last day ; Traverse and keep adding the profit until a day with price of stock higher than currentDay is obtained ; Set this day as currentDay with maximum cost of stock currently ; Return the profit ; Driver Code ; Given array of prices ; Function Call
class GFG { static int maxProfit ( int [ ] prices , int n ) { int profit = 0 , currentDay = n - 1 ; while ( currentDay > 0 ) { int day = currentDay - 1 ; while ( day >= 0 && ( prices [ currentDay ] > prices [ day ] ) ) { profit += ( prices [ currentDay ] - prices [ day ] ) ; day -- ; } currentDay = day ; } return profit ; } public static void main ( String [ ] args ) { int prices [ ] = { 2 , 3 , 5 } ; int N = prices . length ; System . out . print ( maxProfit ( prices , N ) ) ; } }
Check given string is oddly palindrome or not | Set 2 | Java program for the above approach ; Function to check if String formed by odd indices is palindromic or not ; Check if length of OddString odd , to consider edge case ; Push odd index character of first half of str in stack ; Middle element of odd length palindromic String is not compared ; If stack is empty then return true ; Driver Code ; Given String
import java . util . * ; class GFG { static boolean isOddStringPalindrome ( String str , int n ) { int oddStringSize = n / 2 ; boolean lengthOdd = ( ( oddStringSize % 2 == 1 ) ? true : false ) ; Stack < Character > s = new Stack < Character > ( ) ; int i = 1 ; int c = 0 ; while ( i < n && c < oddStringSize / 2 ) { s . add ( str . charAt ( i ) ) ; i += 2 ; c ++ ; } if ( lengthOdd ) i = i + 2 ; while ( i < n && s . size ( ) > 0 ) { if ( s . peek ( ) == str . charAt ( i ) ) s . pop ( ) ; else break ; i = i + 2 ; } if ( s . size ( ) == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int N = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , N ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
Check given string is oddly palindrome or not | Set 2 | Java program for the above approach ; Functions checks if characters at odd index of the String forms palindrome or not ; Initialise two pointers ; Iterate till left <= right ; If there is a mismatch occurs then return false ; Increment and decrement the left and right pointer by 2 ; Driver Code ; Given String ; Function Call
import java . util . * ; class GFG { static boolean isOddStringPalindrome ( String str , int n ) { int left , right ; if ( n % 2 == 0 ) { left = 1 ; right = n - 1 ; } else { left = 1 ; right = n - 2 ; } while ( left < n && right >= 0 && left < right ) { if ( str . charAt ( left ) != str . charAt ( right ) ) return false ; left += 2 ; right -= 2 ; } return true ; } public static void main ( String [ ] args ) { int n = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , n ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }