codeparrot/xlcost-text-to-code huggingface dataset:
Check if a Sequence is a concatenation of two permutations | Java program to check if a given sequence is a concatenation of two permutations or not ; Function to Check if a given sequence is a concatenation of two permutations or not ; Computing the sum of all the elements in the array ; Computing the prefix sum for all the elements in the array ; Iterating through the i from lengths 1 to n - 1 ; Sum of first i + 1 elements ; Sum of remaining n - i - 1 elements ; Lengths of the 2 permutations ; Checking if the sums satisfy the formula or not ; Driver code
import java . util . * ; class GFG { static boolean checkPermutation ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int [ ] prefix = new int [ n + 1 ] ; Arrays . fill ( prefix , 0 ) ; prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int lsum = prefix [ i ] ; int rsum = sum - prefix [ i ] ; int l_len = i + 1 , r_len = n - i - 1 ; if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) && ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) return true ; } return false ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 , 1 , 2 } ; int n = arr . length ; if ( checkPermutation ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find a N | JAVA program to find N digit number such that it is not divisible by any of its digits ; Function that print the answer ; if n == 1 then it is not possible ; loop to n - 1 times ; print 4 as last digit of the number ; Driver code ; Function call
class GFG { static void findTheNumber ( int n ) { if ( n == 1 ) { System . out . print ( " Impossible " + "NEW_LINE"); return ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { System . out . print ( "5" ) ; } System . out . print ( "4" ) ; } public static void main ( String [ ] args ) { int n = 12 ; findTheNumber ( n ) ; } }
Queries to check whether bitwise AND of a subarray is even or odd | Java implementation of the approach ; Function to precompute the count of even and odd numbers ; If the current element is odd then put 1 at odd [ i ] ; If the current element is even then put 1 at even [ i ] ; Taking the prefix sums of these two arrays so we can get the count of even and odd numbers in a range [ L , R ] in O ( 1 ) ; Function that returns true if the bitwise AND of the subarray a [ L ... R ] is odd ; cnt will store the count of odd numbers in the range [ L , R ] ; Check if all the numbers in the range are odd or not ; Function to perform the queries ; Perform queries ; Driver code ; Queries
class GFG { static final int MAXN = 1000005 ; static int even [ ] = new int [ MAXN ] ; static int odd [ ] = new int [ MAXN ] ; static void precompute ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd [ i ] = 1 ; if ( arr [ i ] % 2 == 0 ) even [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) { even [ i ] = even [ i ] + even [ i - 1 ] ; odd [ i ] = odd [ i ] + odd [ i - 1 ] ; } } static boolean isOdd ( int L , int R ) { int cnt = odd [ R ] ; if ( L > 0 ) cnt -= odd [ L - 1 ] ; if ( cnt == R - L + 1 ) return true ; return false ; } static void performQueries ( int a [ ] , int n , int q [ ] [ ] , int m ) { precompute ( a , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] [ 0 ] , R = q [ i ] [ 1 ] ; if ( isOdd ( L , R ) ) System . out . println ( " Odd " ) ; else System . out . println ( " Even " ) ; } } public static void main ( String args [ ] ) { int [ ] a = { 2 , 1 , 5 , 7 , 6 , 8 , 9 } ; int n = a . length ; int q [ ] [ ] = { { 0 , 2 } , { 1 , 2 } , { 2 , 3 } , { 3 , 6 } } ; int m = q . length ; performQueries ( a , n , q , m ) ; } }
Find distinct integers for a triplet with given product | Java implementation of the approach ; Function to find the required triplets ; To store the factors ; Find factors in Math . sqrt ( x ) time ; Choose a factor ; Choose another factor ; These conditions need to be met for a valid triplet ; Print the valid triplet ; Triplet found ; Triplet not found ; Driver code
import java . util . * ; class GFG { static void findTriplets ( int x ) { Vector < Integer > fact = new Vector < Integer > ( ) ; HashSet < Integer > factors = new HashSet < Integer > ( ) ; for ( int i = 2 ; i <= Math . sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { fact . add ( i ) ; if ( x / i != i ) fact . add ( x / i ) ; factors . add ( i ) ; factors . add ( x / i ) ; } } boolean found = false ; int k = fact . size ( ) ; for ( int i = 0 ; i < k ; i ++ ) { int a = fact . get ( i ) ; for ( int j = 0 ; j < k ; j ++ ) { int b = fact . get ( j ) ; if ( ( a != b ) && ( x % ( a * b ) == 0 ) && ( x / ( a * b ) != a ) && ( x / ( a * b ) != b ) && ( x / ( a * b ) != 1 ) ) { System . out . print ( a + " ▁ " + b + " ▁ " + ( x / ( a * b ) ) ) ; found = true ; break ; } } if ( found ) break ; } if ( ! found ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int x = 105 ; findTriplets ( x ) ; } }
Count of pairs satisfying the given condition | Java implementation of the approach ; Function to return the number of pairs satisfying the equation ; Converting integer b to String by using to_String function ; Loop to check if all the digits of b are 9 or not ; If '9' doesn 't appear  then break the loop ; If all the digits of b contain 9 then multiply a with String length else multiply a with String length - 1 ; Return the number of pairs ; Driver code
class GFG { static int countPair ( int a , int b ) { String s = String . valueOf ( b ) ; int i ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != '9' ) break ; } int result ; if ( i == s . length ( ) ) result = a * s . length ( ) ; else result = a * ( s . length ( ) - 1 ) ; return result ; } public static void main ( String [ ] args ) { int a = 5 , b = 101 ; System . out . print ( countPair ( a , b ) ) ; } }
Partition the digits of an integer such that it satisfies a given condition | Java implementation of the approach ; Function to generate sequence from the given String ; Initialize vector to store sequence ; First add all the digits of group A from left to right ; Then add all the digits of group B from left to right ; Return the sequence ; Function that returns true if the sequence is non - decreasing ; Initialize result ; Function to partition the digits of an integer such that it satisfies the given conditions ; Convert the integer to String ; Length of the String ; Array to store the digits ; Storing the digits of X in array ; Initialize the result ; Loop through the digits ; Put into group A if digit less than D ; Put into group B if digit greater than D ; Put into group C if digit equal to D ; Loop through the digits to decide for group C digits ; Set flag equal to true if group B digit present ; If flag is true put in group A or else put in B ; Generate the sequence from partition ; Check if the sequence is non decreasing ; Return - 1 if no such partition is possible ; Driver code
import java . util . * ; class GFG { static Vector < Integer > makeSeq ( String s , int a [ ] ) { Vector < Integer > seq = new Vector < Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i ) == ' A ' ) seq . add ( a [ i ] ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i ) == ' B ' ) seq . add ( a [ i ] ) ; return seq ; } static boolean checkSeq ( Vector < Integer > v ) { boolean check = true ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) if ( v . get ( i ) < v . get ( i - 1 ) ) check = false ; return check ; } static String digitPartition ( int X ) { String num = String . valueOf ( X ) ; int l = num . length ( ) ; int [ ] a = new int [ l ] ; for ( int i = 0 ; i < l ; i ++ ) a [ i ] = ( num . charAt ( i ) - '0' ) ; for ( int D = 0 ; D < 10 ; D ++ ) { String res = " " ; for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] < D ) res += ' A ' ; else if ( a [ i ] > D ) res += ' B ' ; else res += ' C ' ; } boolean flag = false ; for ( int i = 0 ; i < l ; i ++ ) { if ( res . charAt ( i ) == ' B ' ) flag = true ; if ( res . charAt ( i ) == ' C ' ) res = res . substring ( 0 , i ) + ( flag ? ' A ' : ' B ' ) + res . substring ( i + 1 ) ; } Vector < Integer > seq = makeSeq ( res , a ) ; if ( checkSeq ( seq ) ) return res ; } return " - 1" ; } public static void main ( String [ ] args ) { int X = 777147777 ; System . out . print ( digitPartition ( X ) ) ; } }
Area of the circle that has a square and a circle inscribed in it | Java implementation of the approach ; Function to return the required area ; Calculate the area ; Driver code
class GFG { static float getArea ( int a ) { float area = ( float ) ( Math . PI * a * a ) / 4 ; return area ; } public static void main ( String args [ ] ) { int a = 3 ; System . out . println ( getArea ( a ) ) ; } }
Check if all the elements can be made of same parity by inverting adjacent elements | Java implementation of the approach ; Function to check if parity can be made same or not ; Iterate and count the parity ; Odd ; Even ; Condition check ; Drivers code
public class GFG { static boolean flipsPossible ( int [ ] a , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & 1 ) == 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 == 1 && count_even % 2 == 1 ) return false ; else return true ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 0 , 1 , 1 , 0 , 1 } ; int n = a . length ; if ( flipsPossible ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Queries On Array with disappearing and reappearing elements | Java implementation of the approach ; Function to perform the queries ; Size of the array with 1 - based indexing ; Number of queries ; Iterating through the queries ; If m is more than the size of the array ; Count of turns ; Find the remainder ; If the remainder is 0 and turn is odd then the array is empty ; If the remainder is 0 and turn is even then array is full and is in its initial state ; If the remainder is not 0 and the turn is even ; Current size of the array ; Current size of the array ; Print the result ; Driver code ; The initial array , - 1 is for 1 base indexing ; Queries in the form of the pairs of ( t , M )
import java . util . * ; class GFG { static void PerformQueries ( int [ ] a , int [ ] [ ] vec ) { Vector < Integer > ans = new Vector < > ( ) ; int n = ( int ) a . length - 1 ; int q = ( int ) vec . length ; for ( int i = 0 ; i < q ; ++ i ) { long t = vec [ i ] [ 0 ] ; int m = vec [ i ] [ 1 ] ; if ( m > n ) { ans . add ( - 1 ) ; continue ; } int turn = ( int ) ( t / n ) ; int rem = ( int ) ( t % n ) ; if ( rem == 0 && turn % 2 == 1 ) { ans . add ( - 1 ) ; continue ; } if ( rem == 0 && turn % 2 == 0 ) { ans . add ( a [ m ] ) ; continue ; } if ( turn % 2 == 0 ) { int cursize = n - rem ; if ( cursize < m ) { ans . add ( - 1 ) ; continue ; } ans . add ( a [ m + rem ] ) ; } else { int cursize = rem ; if ( cursize < m ) { ans . add ( - 1 ) ; continue ; } ans . add ( a [ m ] ) ; } } for ( int i : ans ) System . out . print ( i + "NEW_LINE"); } public static void main ( String [ ] args ) { int [ ] a = { - 1 , 1 , 2 , 3 , 4 , 5 } ; int [ ] [ ] vec = { { 1 , 4 } , { 6 , 1 } , { 3 , 5 } } ; PerformQueries ( a , vec ) ; } }
Number of pairs in an array having sum equal to product | Java implementation of the approach ; Function to return the count of the required pairs ; Find the count of 0 s and 2 s in the array ; Find the count of required pairs ; Return the count ; Driver code
import java . util . * ; class GFG { static int sumEqualProduct ( int a [ ] , int n ) { int zero = 0 , two = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } int cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 2 , 3 , 4 , 2 , 6 } ; int n = a . length ; System . out . print ( sumEqualProduct ( a , n ) ) ; } }
Minimize the sum of digits of A and B such that A + B = N | Java implementation of the approach ; Function to return the minimum possible sum of digits of A and B such that A + B = n ; Find the sum of digits of n ; If num is a power of 10 ; Driver code
class GFG { static int minSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n /= 10 ; } if ( sum == 1 ) return 10 ; return sum ; } public static void main ( String [ ] args ) { int n = 1884 ; System . out . print ( minSum ( n ) ) ; } }
Multiplication on Array : Range update query in O ( 1 ) | Java implementation of the approach ; Creates a mul [ ] array for A [ ] and returns it after filling initial values . ; Does range update ; Prints updated Array ; Driver code ; ; Array to be updated ; Create and fill mul and div Array
class GFG { static void initialize ( int mul [ ] , int div [ ] , int size ) { for ( int i = 1 ; i < size ; i ++ ) { mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ; } } static void update ( int l , int r , int x , int mul [ ] , int div [ ] ) { mul [ l ] *= x ; div [ r + 1 ] *= x ; } static void printArray ( int ar [ ] , int mul [ ] , int div [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { ar [ i ] = ar [ i ] * mul [ i ] ; System . out . print ( ar [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int ar [ ] = { 10 , 5 , 20 , 40 } ; int n = ar . length ; int [ ] mul = new int [ n + 1 ] ; int [ ] div = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { mul [ i ] = div [ i ] = 1 ; } update ( 0 , 1 , 10 , mul , div ) ; update ( 1 , 3 , 20 , mul , div ) ; update ( 2 , 2 , 2 , mul , div ) ; initialize ( mul , div , n + 1 ) ; printArray ( ar , mul , div , n ) ; } }
Optimal Strategy for a Game | Special Gold Coin | Java implementation of the approach ; Function to return the winner of the game ; To store the count of silver coins ; Update the position of the gold coin ; First player will win the game ; Driver code
import java . util . * ; class GFG { static String getWinner ( String str , int len ) { int total = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == ' S ' ) { total ++ ; } } if ( ( total % 2 ) == 1 ) return " First " ; return " Second " ; } public static void main ( String [ ] args ) { String str = " GSSS " ; int len = str . length ( ) ; System . out . println ( getWinner ( str , len ) ) ; } }
Find the coordinates of a triangle whose Area = ( S / 2 ) | Java implementation of the approach ; Function to find the triangle with area = ( S / 2 ) ; Fix the two pairs of coordinates ; Find ( X3 , Y3 ) with integer coordinates ; Driver code
class GFG { static final long MAX = 1000000000 ; static void findTriangle ( long S ) { long X1 = 0 , Y1 = 0 ; long X2 = MAX , Y2 = 1 ; long X3 = ( MAX - S % MAX ) % MAX ; long Y3 = ( S + X3 ) / MAX ; System . out . println ( " ( " + X1 + " , ▁ " + Y1 + " ) " ) ; System . out . println ( " ( " + X2 + " , ▁ " + Y2 + " ) " ) ; System . out . println ( " ( " + X3 + " , ▁ " + Y3 + " ) " ) ; } public static void main ( String [ ] args ) { long S = 4 ; findTriangle ( S ) ; } }
Rearrange array elements such that Bitwise AND of first N | Java implementation of the approach ; Utility function to print the elements of an array ; Function to find the required arrangement ; There has to be atleast 2 elements ; Minimum element from the array ; Swap any occurrence of the minimum element with the last element ; Find the bitwise AND of the first ( n - 1 ) elements ; If the bitwise AND is equal to the last element then print the arrangement ; Driver code
import java . util . * ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void findArrangement ( int arr [ ] , int n ) { if ( n < 2 ) { System . out . print ( " - 1" ) ; return ; } int minVal = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == minVal ) { swap ( arr , i , n - 1 ) ; break ; } } int andVal = arr [ 0 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { andVal &= arr [ i ] ; } if ( andVal == arr [ n - 1 ] ) printArr ( arr , n ) ; else System . out . print ( " - 1" ) ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 , 3 } ; int n = arr . length ; findArrangement ( arr , n ) ; } }
Maximum prime moves to convert X to Y | Java implementation of the approach ; Function to return the maximum operations required to convert X to Y ; X cannot be converted to Y ; If the difference is 1 ; If the difference is even ; Add 3 to X and the new difference will be even ; Driver code
class GFG { static int maxOperations ( int X , int Y ) { if ( X > Y ) return - 1 ; int diff = Y - X ; if ( diff == 1 ) return - 1 ; if ( diff % 2 == 0 ) return ( diff / 2 ) ; return ( 1 + ( ( diff - 3 ) / 2 ) ) ; } public static void main ( String [ ] args ) { int X = 5 , Y = 16 ; System . out . println ( maxOperations ( X , Y ) ) ; } }
Sum of Digits of the Good Strings | Java implementation of the approach ; To store the states of the dp ; Function to fill the dp table ; Sum of digits of the string of length 1 is i as i is only number in that string and count of good strings of length 1 that end with i is also 1 ; Adjacent digits are different ; Increment the count as digit at ( i - 1 ) 'th index is k and count  of good strings is equal to this  because at the end of the strings of  length (i - 1) we are just  putting digit j as the last digit ; Driver code
class GFG { final static int DIGITS = 10 ; final static int MAX = 10000 ; final static int MOD = 1000000007 ; static int dp [ ] [ ] = new int [ MAX ] [ DIGITS ] ; static int cnt [ ] [ ] = new int [ MAX ] [ DIGITS ] ; static void precompute ( ) { for ( int i = 0 ; i < DIGITS ; i ++ ) { dp [ 1 ] [ i ] = i ; cnt [ 1 ] [ i ] = 1 ; } for ( int i = 2 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < DIGITS ; j ++ ) { for ( int k = 0 ; k < DIGITS ; k ++ ) { if ( j != k ) { dp [ i ] [ j ] = dp [ i ] [ j ] + ( dp [ i - 1 ] [ k ] + ( cnt [ i - 1 ] [ k ] * j ) % MOD ) % MOD ; dp [ i ] [ j ] %= MOD ; cnt [ i ] [ j ] += cnt [ i - 1 ] [ k ] ; cnt [ i ] [ j ] %= MOD ; } } } } } public static void main ( String [ ] args ) { int x = 6 , y = 4 ; precompute ( ) ; System . out . println ( dp [ x ] [ y ] ) ; } }
Count non | Java implementation of the approach ; Function to pre - compute the sequence ; For N = 1 the answer will be 2 ; Starting two terms of the sequence ; Compute the rest of the sequence with the relation F [ i ] = F [ i - 1 ] + F [ i - 2 ] ; Driver code ; Pre - compute the sequence
class GFG { static int N = 10000 ; static int MOD = 1000000007 ; static int [ ] F = new int [ N ] ; static void precompute ( ) { F [ 1 ] = 2 ; F [ 2 ] = 3 ; F [ 3 ] = 4 ; for ( int i = 4 ; i < N ; i ++ ) F [ i ] = ( F [ i - 1 ] + F [ i - 2 ] ) % MOD ; } public static void main ( String [ ] args ) { int n = 8 ; precompute ( ) ; System . out . println ( F [ n ] ) ; } }
Minimize sum by dividing all elements of a subarray by K | Java implementation of the approach ; Function to return the maximum subarray sum ; Function to return the minimized sum of the array elements after performing the given operation ; Find maximum subarray sum ; Find total sum of the array ; Maximum subarray sum is already negative ; Choose the subarray whose sum is maximum and divide all elements by K ; Driver code
import java . util . * ; class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static double minimizedSum ( int a [ ] , int n , int K ) { int sum = maxSubArraySum ( a , n ) ; double totalSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) totalSum += a [ i ] ; if ( sum < 0 ) return totalSum ; totalSum = totalSum - sum + ( double ) sum / ( double ) K ; return totalSum ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , - 2 , 3 } ; int n = a . length ; int K = 2 ; System . out . println ( minimizedSum ( a , n , K ) ) ; } }
Minimum numbers with one 's place as 9 to be added to get N | Java implementation of the approach ; Function to find minimum count of numbers ( with one 's digit 9)  that sum up to N ; Fetch one 's digit ; Apply Cases mentioned in approach ; If no possible answer exists ; Driver code
class GFG { static int findMin ( int N ) { int digit = N % 10 ; switch ( digit ) { case 0 : if ( N >= 90 ) return 10 ; break ; case 1 : if ( N >= 81 ) return 9 ; break ; case 2 : if ( N >= 72 ) return 8 ; break ; case 3 : if ( N >= 63 ) return 7 ; break ; case 4 : if ( N >= 54 ) return 6 ; break ; case 5 : if ( N >= 45 ) return 5 ; break ; case 6 : if ( N >= 36 ) return 4 ; break ; case 7 : if ( N >= 27 ) return 3 ; break ; case 8 : if ( N >= 18 ) return 2 ; break ; case 9 : if ( N >= 9 ) return 1 ; break ; } return - 1 ; } public static void main ( String [ ] args ) { int N = 27 ; System . out . println ( findMin ( N ) ) ; } }
Random list of M non | Java implementation of the approach ; Utility function to print the elements of an array ; Function to generate a list of m random non - negative integers whose sum is n ; Create an array of size m where every element is initialized to 0 ; To make the sum of the final list as n ; Increment any random element from the array by 1 ; Print the generated list ; Driver code
class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void randomList ( int m , int n ) { int arr [ ] = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ ( int ) ( Math . random ( ) * m ) ] ++ ; } printArr ( arr , m ) ; } public static void main ( String args [ ] ) { int m = 4 , n = 8 ; randomList ( m , n ) ; } }
Maximum String Partition | Java implementation of the above approach ; Return the count of string ; P will store the answer ; Current will store current string Previous will store the previous string that has been taken already ; Add a character to current string ; Here we will create a partition and update the previous string with current string ; Now we will clear the current string ; Increment the count of partition . ; Driver code
class GFG { static int maxPartition ( String s ) { int n = s . length ( ) , P = 0 ; String current = " " , previous = " " ; for ( int i = 0 ; i < n ; i ++ ) { current += s . charAt ( i ) ; if ( ! current . equals ( previous ) ) { previous = current ; current = " " ; P ++ ; } } return P ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int ans = maxPartition ( s ) ; System . out . println ( ans ) ; } }
How to learn Pattern printing easily ? |
class GFG { public static void main ( String [ ] args ) { int N = 4 , i , j , min ; System . out . println ( " Value ▁ of ▁ N : ▁ " + N ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; System . out . print ( N - min + 1 ) ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; System . out . print ( N - min + 1 ) ; } System . out . println ( ) ; } } }