codeparrot/xlcost-text-to-code huggingface dataset:
Maximize length of subsequence consisting of single distinct character possible by K increments in a string | Java program for the above approach ; Function to find the maximum length of a subsequence of same characters after at most K increment operations ; Store the size of s ; Sort the given string sort ( S . begin ( ) , S . end ( ) ) ; convert input string to char array ; sort tempArray ; Stores the maximum length and the sum of the sliding window ; Traverse the string S ; Add the current character to the window ; Decrease the window size ; Update the value of sum ; Increment the value of start ; Update the maximum window size ; Print the resultant maximum length of the subsequence ; Driver code
import java . io . * ; import java . math . * ; import java . util . Arrays ; class GFG { static void maxSubsequenceLen ( String s , int K ) { int N = s . length ( ) ; int start = 0 , end = 0 ; char S [ ] = s . toCharArray ( ) ; Arrays . sort ( S ) ; int ans = Integer . MIN_VALUE , sum = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum = sum + ( S [ end ] - ' a ' ) ; while ( sum + K < ( S [ end ] - ' a ' ) * ( end - start + 1 ) ) { sum = sum - ( S [ start ] - ' a ' ) ; start ++ ; } ans = Math . max ( ans , end - start + 1 ) ; } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { String S = " acscbcca " ; int K = 1 ; maxSubsequenceLen ( S , K ) ; } }
Check if a given integer is the product of K consecutive integers | Java program for the above approach ; Function to check if N can be expressed as the product of K consecutive integers ; Stores the K - th root of N ; Stores the product of K consecutive integers ; Traverse over the range [ 1 , K ] ; Update the product ; If product is N , then return " Yes " ; Otherwise , traverse over the range [ 2 , Kthroot ] ; Update the value of product ; If product is equal to N ; Otherwise , return " No " ; Driver Code
public class GFG { static String checkPro ( int n , int k ) { double exp = 1.0 / k ; int KthRoot = ( int ) Math . pow ( n , exp ) ; int product = 1 ; for ( int i = 1 ; i < k + 1 ; i ++ ) { product = product * i ; } if ( product == n ) return " Yes " ; else { for ( int j = 2 ; j < KthRoot + 1 ; j ++ ) { product = product * ( j + k - 1 ) ; product = product / ( j - 1 ) ; if ( product == n ) return " Yes " ; } } return " No " ; } public static void main ( String [ ] args ) { int N = 210 ; int K = 3 ; System . out . println ( checkPro ( N , K ) ) ; } }
Convert a Binary String to another by flipping prefixes minimum number of times | Java program for the above approach ; Function to count minimum number of operations required to convert string a to another string b ; Store the lengths of each prefixes selected ; Traverse the string ; If first character is same as b [ i ] ; Insert 1 to ops [ ] ; And , flip the bit ; Reverse the prefix string of length i + 1 ; Flip the characters in this prefix length ; Push ( i + 1 ) to array ops [ ] ; Print the number of operations ; Print the length of each prefixes stored ; Function to reverse a [ ] from start to end ; Driver code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minOperations ( String s1 , String s2 , int n ) { char a [ ] = s1 . toCharArray ( ) ; char b [ ] = s2 . toCharArray ( ) ; ArrayList < Integer > ops = new ArrayList < > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] != b [ i ] ) { if ( a [ 0 ] == b [ i ] ) { ops . add ( 1 ) ; a [ 0 ] = ( a [ 0 ] == '0' ? '1' : '0' ) ; } reverse ( a , 0 , i ) ; for ( int j = 0 ; j <= i ; j ++ ) { a [ j ] = ( a [ j ] == '0' ? '1' : '0' ) ; } ops . add ( i + 1 ) ; } } System . out . println ( ops . size ( ) ) ; for ( int x : ops ) { System . out . print ( x + " ▁ " ) ; } } static void reverse ( char a [ ] , int start , int end ) { while ( start < end ) { char temp = a [ start ] ; a [ start ] = a [ end ] ; a [ end ] = temp ; start ++ ; end -- ; } } public static void main ( String [ ] args ) { String a = "10" , b = "01" ; int N = a . length ( ) ; minOperations ( a , b , N ) ; } }
Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups | Java program for the above approach ; Function to fill the matrix with the given conditions ; Count of parts with size sizeOfPart ; Assigning the cell with no of groups ; Update row ; Update col ; Increment count ; For new group increment start ; Function to return the reference of the matrix to be filled ; Create matrix of size N * M ; Starting index of the matrix ; Size of one group ; Element to assigned to matrix ; Fill the matrix that have rem no of parts with size size + 1 ; Fill the remaining number of parts with each part size is ' size ' ; Function to print the matrix ; Traverse the rows ; Traverse the columns ; Driver Code ; Given N , M , K ; Function Call ; Function Call to print matrix
import java . util . * ; class GFG { static int N , M ; static int [ ] [ ] mat ; static int row , col , start , flag ; static void fillMatrix ( int sizeOfpart , int noOfPart , int m ) { for ( int i = 0 ; i < noOfPart ; ++ i ) { int count = 0 ; while ( count < sizeOfpart ) { mat [ row ] [ col ] = start ; if ( col == m - 1 && flag == 1 ) { row ++ ; col = m ; flag = 0 ; } else if ( col == 0 && flag == 0 ) { row ++ ; col = - 1 ; flag = 1 ; } if ( flag == 1 ) { col ++ ; } else { col -- ; } count ++ ; } start ++ ; } } static void findMatrix ( int k ) { mat = new int [ M ] [ N ] ; row = 0 ; col = 0 ; int size = ( N * M ) / k ; int rem = ( N * M ) % k ; start = 1 ; flag = 1 ; fillMatrix ( size + 1 , rem , M ) ; fillMatrix ( size , k - rem , M ) ; } static void printMatrix ( ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { N = 5 ; M = 5 ; int K = 6 ; findMatrix ( K ) ; printMatrix ( ) ; } }
Replace ' ? ' in a string such that no two adjacent characters are same | Java program for the above approach ; Function that replace all ' ? ' with lowercase alphabets such that each adjacent character is different ; Store the given String ; If the first character is ' ? ' ; Traverse the String [ 1 , N - 1 ] ; If the current character is ' ? ' ; Change the character ; Check equality with the previous character ; Check equality with the next character ; Check equality with the previous character ; If the last character is ' ? ' ; Change character ; Check with previous character ; Return the resultant String ; Driver Code ; Given String S ; Function Call
class GFG { static String changeString ( String S ) { char [ ] s = S . toCharArray ( ) ; int N = ( int ) S . length ( ) ; if ( s [ 0 ] == ' ? ' ) { s [ 0 ] = ' a ' ; if ( s [ 0 ] == s [ 1 ] ) { s [ 0 ] ++ ; } } for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( s [ i ] == ' ? ' ) { s [ i ] = ' a ' ; if ( s [ i ] == s [ i - 1 ] ) { s [ i ] ++ ; } if ( s [ i ] == s [ i + 1 ] ) { s [ i ] ++ ; } if ( s [ i ] == s [ i - 1 ] ) { s [ i ] ++ ; } } } if ( s [ N - 1 ] == ' ? ' ) { s [ N - 1 ] = ' a ' ; if ( s [ N - 1 ] == s [ N - 2 ] ) { s [ N - 1 ] ++ ; } } String ans = " " ; for ( int i = 0 ; i < s . length ; i ++ ) { ans += s [ i ] ; } return ans ; } public static void main ( String [ ] args ) { String S = " ? a ? a " ; System . out . print ( changeString ( S ) ) ; } }
Check if a given string is a Reverse Bitonic String or not | Java program to implement the above approach ; Function to check if the given string is reverse bitonic ; Check for decreasing sequence ; If end of string has been reached ; Check for increasing sequence ; If the end of string hasn 't been reached ; If the string is reverse bitonic ; Driver Code
class GFG { static int checkReverseBitonic ( String s ) { int i , j ; for ( i = 1 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) < s . charAt ( i - 1 ) ) continue ; if ( s . charAt ( i ) >= s . charAt ( i - 1 ) ) break ; } if ( i == s . length ( ) - 1 ) return 1 ; for ( j = i + 1 ; j < s . length ( ) ; j ++ ) { if ( s . charAt ( j ) > s . charAt ( j - 1 ) ) continue ; if ( s . charAt ( j ) <= s . charAt ( j - 1 ) ) break ; } i = j ; if ( i != s . length ( ) ) return 0 ; return 1 ; } public static void main ( String [ ] args ) { String s = " abcdwef " ; if ( checkReverseBitonic ( s ) == 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Smallest number whose square has N digits | Java program for above approach ; Function to return smallest number whose square has N digits ; Calculate N - th term of the series ; Driver code
class GFG { static int smallestNum ( int N ) { float x = ( float ) ( Math . pow ( 10 , ( N - 1 ) / 2.0 ) ) ; return ( int ) ( Math . ceil ( x ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . print ( smallestNum ( N ) ) ; } }
Count of K | Java code for the above program . ; Function to to count the number of K - countdowns for multiple queries ; Flag which stores the current value of value in the countdown ; Count of K - countdowns ; Loop to iterate over the elements of the array ; Condition check if the elements of the array is equal to K ; Condition check if the elements of the array is in continuous order ; Condition check if the elements of the array are not in continuous order ; Condition check to increment the counter if the there is a K - countdown present in the array ; Returning the count of K - countdowns ; Driver code ; Function Call
class GFG { public static int countKCountdown ( int arr [ ] , int N , int K ) { int flag = - 1 ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == K ) flag = K ; if ( arr [ i ] == flag ) flag -- ; else flag = - 1 ; if ( flag == 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int N = 8 ; int K = 3 ; int arr [ ] = { 4 , 3 , 2 , 1 , 5 , 3 , 2 , 1 } ; System . out . print ( countKCountdown ( arr , N , K ) ) ; } }
Convert the given RGB color code to Hex color code | Java code to convert the given RGB color code to Hex color code ; function to convert decimal to hexadecimal ; char array to store hexadecimal number ; counter for hexadecimal number array ; temporary variable to store remainder ; storing remainder in temp variable . ; check if temp < 10 ; Return the equivalent hexadecimal color code ; Function to convert the RGB code to Hex color code ; The hex color code doesn 't exist ; Driver program to test above function
import java . util . * ; class GFG { static String decToHexa ( int n ) { char [ ] hexaDeciNum = new char [ 2 ] ; int i = 0 ; while ( n != 0 ) { int temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = ( char ) ( temp + 48 ) ; i ++ ; } else { hexaDeciNum [ i ] = ( char ) ( temp + 55 ) ; i ++ ; } n = n / 16 ; } String hexCode = " " ; if ( i == 2 ) { hexCode += hexaDeciNum [ 0 ] ; hexCode += hexaDeciNum [ 1 ] ; } else if ( i == 1 ) { hexCode = "0" ; hexCode += hexaDeciNum [ 0 ] ; } else if ( i == 0 ) hexCode = "00" ; return hexCode ; } static String convertRGBtoHex ( int R , int G , int B ) { if ( ( R >= 0 && R <= 255 ) && ( G >= 0 && G <= 255 ) && ( B >= 0 && B <= 255 ) ) { String hexCode = " # " ; hexCode += decToHexa ( R ) ; hexCode += decToHexa ( G ) ; hexCode += decToHexa ( B ) ; return hexCode ; } else return " - 1" ; } public static void main ( String [ ] args ) { int R = 0 , G = 0 , B = 0 ; System . out . print ( convertRGBtoHex ( R , G , B ) + "NEW_LINE"); R = 255 ; G = 255 ; B = 255 ; System . out . print ( convertRGBtoHex ( R , G , B ) + "NEW_LINE"); R = 25 ; G = 56 ; B = 123 ; System . out . print ( convertRGBtoHex ( R , G , B ) + "NEW_LINE"); R = 2 ; G = 3 ; B = 4 ; System . out . print ( convertRGBtoHex ( R , G , B ) + "NEW_LINE"); R = 255 ; G = 255 ; B = 256 ; System . out . print ( convertRGBtoHex ( R , G , B ) + "NEW_LINE"); } }
Strings formed from given characters without any consecutive repeating characters | Java implementation of the above approach ; Function to print the strings which satisfy the mentioned conditions ; Iterate through all the strings in the array . ; check function to check the conditions for every string ; Function to check whether the string contains any consecutive repetitive characters and any characters other than those in str ; Valid characters check ; Nonrepetitive check ; Driver code
import java . util . * ; public class GFG { public static void getStrings ( String str , String [ ] arr ) { for ( int i = 0 ; i < arr . length ; i ++ ) { if ( check ( arr [ i ] , str ) ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static boolean check ( String s , String str ) { char [ ] chars = s . toCharArray ( ) ; for ( char c : chars ) { if ( ! str . contains ( String . valueOf ( c ) ) ) { return false ; } } for ( int i = 0 ; i < chars . length - 1 ; i ++ ) { if ( chars [ i ] == chars [ i + 1 ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = " ABCD " ; String [ ] arr = { " AABCDA " , " ABCDZADC " , " ABCDBCA " , " ABCDABDCA " } ; getStrings ( str , arr ) ; } }
Program to build a DFA to accept strings that start and end with same character | Java Program for DFA that accepts string if it starts and ends with same character ; Function for the state Q1 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q2 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q3 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q4 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q0 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q3 ; Driver Code ; Since q0 is the starting state Send the string to q0
class GFG { static void q1 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " Yes " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q2 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " No " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } static void q3 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " Yes " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q4 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " No " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q4 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } static void q0 ( String s , int i ) { if ( i == s . length ( ) ) { System . out . println ( " No " ) ; return ; } if ( s . charAt ( i ) == ' a ' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } public static void main ( String [ ] args ) { String s = " abbaabb " ; q0 ( s , 0 ) ; } }
Longest prefix in a string with highest frequency | Java implementation of the above approach ; Function to find Longest prefix string with the highest frequency ; storing all indices where first element is found ; if the first letter in the string does not occur again then answer will be the whole string ; loop till second appearance of the first element ; check one letter after every stored index ; If there is no mismatch we move forward ; otherwise we stop ; Driver Code
import java . util . * ; class GFG { static void prefix ( char [ ] str ) { int k = 1 , j ; int n = str . length ; Vector < Integer > g = new Vector < > ( ) ; int flag = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) { g . add ( i ) ; flag = 1 ; } } if ( flag == 0 ) { System . out . println ( String . valueOf ( str ) ) ; } else { int len = g . size ( ) ; while ( k < g . get ( 0 ) ) { int cnt = 0 ; for ( j = 0 ; j < len ; j ++ ) { if ( ( g . get ( j ) + k ) < n && str [ g . get ( j ) + k ] == str [ k ] ) { cnt ++ ; } } if ( cnt == len ) { k ++ ; } else { break ; } } for ( int i = 0 ; i < k ; i ++ ) { System . out . print ( str [ i ] ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { String str = " abcab " ; prefix ( str . toCharArray ( ) ) ; } }
Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers | Java implementation of the above approach ; Function to generate a permutation of integers from 1 to N such that the absolute difference of all the two consecutive integers give K distinct integers ; To store the permutation ; For sequence 1 2 3. . . ; For sequence N , N - 1 , N - 2. . . ; Flag is used to alternate between the above if else statements ; If last element added was r + 1 ; If last element added was l - 1 ; Print the permutation ; Driver code
import java . util . Vector ; class GFG { static void printPermutation ( int N , int K ) { Vector < Integer > res = new Vector < > ( ) ; int l = 1 , r = N , flag = 0 ; for ( int i = 0 ; i < K ; i ++ ) { if ( flag == 0 ) { res . add ( l ) ; l ++ ; } else { res . add ( r ) ; r -- ; } flag ^= 1 ; } if ( flag != 1 ) { for ( int i = r ; i >= l ; i -- ) { res . add ( i ) ; } } else { for ( int i = l ; i <= r ; i ++ ) { res . add ( i ) ; } } for ( Integer i : res ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 10 , K = 4 ; printPermutation ( N , K ) ; } }
Find Nth term of the series 1 , 8 , 54 , 384. . . | Java program to find N - th term of the series : 1 , 8 , 54 , 384. . . ; Main class for main method ; Calculate factorial of N ; By using above formula
import java . io . * ; class GFG { public static int fact ( int N ) { int i , product = 1 ; for ( i = 1 ; i <= N ; i ++ ) product = product * i ; return product ; } public static int nthTerm ( int N ) { return ( N * N ) * fact ( N ) ; } public static void main ( String [ ] args ) { System . out . println ( nthTerm ( N ) ) ; } }
Rabin | Following program is a Java implementation of Rabin Karp Algorithm given in the CLRS book ; d is the number of characters in the input alphabet ; pat -> pattern txt -> text q -> A prime number ; int p = 0 ; hash value for pattern int t = 0 ; hash value for txt ; The value of h would be " pow ( d , ▁ M - 1 ) % q " ; Calculate the hash value of pattern and first window of text ; Slide the pattern over text one by one ; Check the hash values of current window of text and pattern . If the hash values match then only check for characters on by one ; Check for characters one by one ; if p == t and pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Calculate hash value for next window of text : Remove leading digit , add trailing digit ; We might get negative value of t , converting it to positive ; Driver Code ; A prime number ; Function Call
public class Main { public final static int d = 256 ; static void search ( String pat , String txt , int q ) { int M = pat . length ( ) ; int N = txt . length ( ) ; int i , j ; int h = 1 ; for ( i = 0 ; i < M - 1 ; i ++ ) h = ( h * d ) % q ; for ( i = 0 ; i < M ; i ++ ) { p = ( d * p + pat . charAt ( i ) ) % q ; t = ( d * t + txt . charAt ( i ) ) % q ; } for ( i = 0 ; i <= N - M ; i ++ ) { if ( p == t ) { for ( j = 0 ; j < M ; j ++ ) { if ( txt . charAt ( i + j ) != pat . charAt ( j ) ) break ; } if ( j == M ) System . out . println ( " Pattern ▁ found ▁ at ▁ index ▁ " + i ) ; } if ( i < N - M ) { t = ( d * ( t - txt . charAt ( i ) * h ) + txt . charAt ( i + M ) ) % q ; if ( t < 0 ) t = ( t + q ) ; } } } public static void main ( String [ ] args ) { String txt = " GEEKS ▁ FOR ▁ GEEKS " ; String pat = " GEEK " ; int q = 101 ; search ( pat , txt , q ) ; } }
Find the final string after flipping bits at the indices that are multiple of prime factors of array elements | Java program for the above approach ; Stores smallest prime factor ; Function to find the smallest prime factor for every number till MAXN ; Marking smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Function to find all the distinct prime factors of the given number x ; Find the prime factors for X ; Find the spf [ ] of x ; Return the prime factors for x ; Function to find string after flipping the characters at indices of prime factors of array elements arr [ ] ; Precalculating Smallest Prime Factor ; Stores the frequency of each prime factor ; Iterate over all elements of the array arr [ ] ; Stores prime factors of arr [ i ] ; Increment the frequency of all prime factors of arr [ i ] ; Iterate over all elements of the array frequency [ ] ; If frequency [ i ] is odd ; Flip bits of all indices that are multiple of i ; Return Answer ; Driver Code
import java . util . ArrayList ; import java . util . Arrays ; class GFG { public static int MAXN = 100001 ; public static int [ ] spf = new int [ MAXN ] ; public static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) { if ( spf [ j ] == j ) spf [ j ] = i ; } } } } public static ArrayList < Integer > getFactorization ( int x ) { ArrayList < Integer > ret = new ArrayList < Integer > ( ) ; while ( x != 1 ) { ret . add ( spf [ x ] ) ; int value = spf [ x ] ; while ( x % value == 0 ) x = x / value ; } return ret ; } public static String flipString ( String S , int arr [ ] , int M ) { sieve ( ) ; int [ ] frequency = new int [ MAXN ] ; Arrays . fill ( frequency , 0 ) ; for ( int i = 0 ; i < M ; i ++ ) { ArrayList < Integer > primeFactors = getFactorization ( arr [ i ] ) ; for ( int factors : primeFactors ) { frequency [ factors ] ++ ; frequency [ factors ] %= 2 ; } } int N = S . length ( ) ; for ( int i = 0 ; i < MAXN ; i ++ ) { if ( ( frequency [ i ] & 1 ) > 0 ) { for ( int j = i ; j <= N ; j += i ) { S = S . substring ( 0 , j - 1 ) + ( S . charAt ( j - 1 ) == '1' ? '0' : '1' ) + S . substring ( j ) ; } } } return S ; } public static void main ( String args [ ] ) { String S = "000000" ; int arr [ ] = { 2 , 4 , 6 } ; int M = arr . length ; System . out . println ( flipString ( S , arr , M ) ) ; } }
Check if count of 1 s can be made greater in a Binary string by changing 0 s adjacent to 1 s | Java program for the above approach ; Function to check whether in a given binary string can we make number of 1 ' s ▁ greater ▁ than ▁ the ▁ number ▁ of ▁ 0' s by doing the given operation ; Stores the count of 0 's ; Stores the count of 1 's ; Traverse through the string S ; Check current character is 1 ; Update cnt1 ; Update cnt0 ; Traverse through the string S ; Check curretn character is 1 ; Check if left adjacent character is 0 ; Change the left adjacent character to _ ; Update the cnt0 ; Check if right adjacent character is 0 ; Change the right adjacent character to _ ; Update the cnt0 ; Check count of 1 ' s ▁ is ▁ greater ▁ ▁ than ▁ the ▁ count ▁ of ▁ 0' s ; Driver Code
import java . util . * ; class GFG { static void isOnesGreater ( String S , int N ) { char [ ] st = new char [ S . length ( ) ] ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { st [ i ] = S . charAt ( i ) ; } int cnt0 = 0 ; int cnt1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( st [ i ] == '1' ) cnt1 ++ ; else cnt0 ++ ; } for ( int i = 0 ; i < N ; i ++ ) { if ( st [ i ] == '1' ) { if ( i > 0 && st [ i - 1 ] == '0' ) { st [ i - 1 ] = ' _ ' ; cnt0 -- ; } else if ( i < N && st [ i + 1 ] == '0' ) { st [ i + 1 ] = ' _ ' ; cnt0 -- ; } } } if ( cnt1 > cnt0 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { String S = "01" ; int N = S . length ( ) ; isOnesGreater ( S , N ) ; } }