codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to split array into K non | Java program to implement the above approach ; Function to get the value of pow ( K , M ) ; Stores value of pow ( K , M ) ; Calculate value of pow ( K , N ) ; If N is odd , update res ; Update M to M / 2 ; Update K ; Function to print total ways to split the array that satisfies the given condition ; Stores total ways that satisfies the given condition ; Stores count of distinct elements in the given arr ; Store distinct elements of the given array ; Traverse the given array ; Insert current element into set st . ; Update M ; Update cntways ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int power ( int K , int M ) { int res = 1 ; while ( M > 0 ) { if ( ( M & 1 ) == 1 ) { res = ( res * K ) ; } M = M >> 1 ; K = ( K * K ) ; } return res ; } static int cntWays ( int arr [ ] , int N , int K ) { int cntways = 0 ; int M = 0 ; Set < Integer > st = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . add ( arr [ i ] ) ; } M = st . size ( ) ; cntways = power ( K , M ) ; return cntways ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 } ; int N = arr . length ; int K = 2 ; System . out . println ( cntWays ( arr , N , K ) ) ; } }
Minimize product of first N | Java program for the above approach ; Function to find the minimum product of 1 to N - 1 after performing the given operations ; Initialize ans with 1 ; Multiply ans with N - 2 ( ( N - 4 ) / 2 ) times ; Multiply ans with N - 1 and N - 2 once ; Print ans ; Driver Code ; Given Number N ; Function Call
import java . util . * ; class GFG { static int mod = ( int ) 1e9 + 7 ; static void minProduct ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= ( n - 4 ) / 2 ; i ++ ) { ans = ( int ) ( 1L * ans * ( n - 2 ) ) % mod ; } ans = ( int ) ( 1L * ans * ( n - 2 ) * ( n - 1 ) ) % mod ; System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 8 ; minProduct ( N ) ; } }
Bitwise XOR of all unordered pairs from a given array | Java program to implement the above approach ; Function to get bitwise XOR of all possible pairs of the given array ; Stores bitwise XOR of all possible pairs ; Generate all possible pairs and calculate bitwise XOR of all possible pairs ; Calculate bitwise XOR of each pair ; Driver code
class GFG { public static int TotalXorPair ( int arr [ ] , int N ) { int totalXOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { totalXOR ^= arr [ i ] ^ arr [ j ] ; } } return totalXOR ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; System . out . print ( TotalXorPair ( arr , N ) ) ; } }
Count permutations of all integers upto N that can form an acyclic graph based on given conditions | Java implementation of the above approach ; Find the count of possible graphs ; Driver Code
import java . util . * ; class GFG { static void possibleAcyclicGraph ( int N ) { System . out . print ( ( int ) Math . pow ( 2 , N - 1 ) ) ; return ; } public static void main ( String [ ] args ) { int N = 4 ; possibleAcyclicGraph ( N ) ; } }
Minimize positive product of two given numbers by at most N decrements | Java program to implement the above approach ; Function to minimize the product of two numbers ; Reducing X , N times , minimizes the product ; Reduce X to 1 and reduce remaining N from Y ; Reducing Y , N times , minimizes the product ; Reduce Y to 1 and reduce remaining N from X ; Driver Code
import java . io . * ; class GFG { static int minProd ( int X , int Y , int N ) { if ( X <= Y ) { if ( N < X ) return ( X - N ) * Y ; else { return Math . max ( Y - ( N - X + 1 ) , 1 ) ; } } if ( Y >= N ) return ( Y - N ) * X ; return Math . max ( X - ( N - Y + 1 ) , 1 ) ; } public static void main ( String [ ] args ) { int X = 47 , Y = 42 , N = 167 ; System . out . println ( minProd ( X , Y , N ) ) ; } }
Count pairs from an array having product of their sum and difference equal to 1 | Java program for the above approach ; Function to count the desired number of pairs ; Initialize oneCount ; Initialize the desiredPair ; Traverse the given array ; If 1 is encountered ; If 0 is encountered ; Update count of pairs ; Return the final count ; Driver Code ; Given array arr [ ] ; Function call
import java . io . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { int oneCount = 0 ; int desiredPair = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { oneCount ++ ; } if ( arr [ i ] == 0 ) { desiredPair += oneCount ; } } return desiredPair ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 1 , 0 } ; int N = arr . length ; System . out . println ( countPairs ( arr , N ) ) ; } }
Smallest missing non | Java program to implement the above approach ; Function to print the smallest missing non - negative integer up to every array indices ; Stores the smallest missing non - negative integers between start index to current index ; Store the boolean value to check smNonNeg present between start index to each index of the array ; Traverse the array ; Since output always lies in the range [ 0 , N - 1 ] ; Check if smNonNeg is present between start index and current index or not ; Print smallest missing non - negative integer ; Driver Code
import java . io . * ; import java . util . Arrays ; class GFG { static void smlstNonNeg ( int arr [ ] , int N ) { int smNonNeg = 0 ; Boolean [ ] hash = new Boolean [ N + 1 ] ; Arrays . fill ( hash , false ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 && arr [ i ] < N ) { hash [ arr [ i ] ] = true ; } while ( hash [ smNonNeg ] ) { smNonNeg ++ ; } System . out . print ( smNonNeg + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 2 , 3 , 5 } ; int N = arr . length ; smlstNonNeg ( arr , N ) ; } }
Split array into two subsequences having minimum count of pairs with sum equal to X | Java program for the above approach ; Function to split the array into two subsequences ; Stores the two subsequences ; Flag to set / reset to split arrays elements alternately into two arrays ; Traverse the given array ; If 2 * arr [ i ] is less than X ; Push element into the first array ; If 2 * arr [ i ] is greater than X ; Push element into the second array ; If 2 * arr [ i ] is equal to X ; Alternatively place the elements into the two arrays ; Print both the arrays ; Driver Code ; Size of the array ; Function Call
import java . util . * ; class GFG { static void solve ( int arr [ ] , int N , int X ) { Vector < Integer > A = new Vector < Integer > ( ) , B = new Vector < Integer > ( ) ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( 2 * arr [ i ] ) < X ) { A . add ( arr [ i ] ) ; } else if ( ( 2 * arr [ i ] ) > X ) { B . add ( arr [ i ] ) ; } else { if ( c % 2 == 0 ) { A . add ( arr [ i ] ) ; } else { B . add ( arr [ i ] ) ; } c ++ ; } } System . out . print ( " The ▁ First ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) { System . out . print ( A . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; System . out . print ( " The ▁ Second ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < B . size ( ) ; i ++ ) { System . out . print ( B . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 } ; int X = 7 ; int N = arr . length ; solve ( arr , N , X ) ; } }
Split array into minimum number of subsets having maximum pair sum at most K | Java program to implement the above approach ; Function to get the minimum count of subsets that satisfy the given condition ; Store the minimum count of subsets that satisfy the given condition ; Stores start index of the sorted array . ; Stores end index of the sorted array ; Sort the given array ; Traverse the array ; If only two elements of sorted array left ; If only one elements left in the array ; Driver Code
import java . util . * ; class GFG { static int cntMinSub ( int arr [ ] , int N , int K ) { int res = 0 ; int start = 0 ; int end = N - 1 ; Arrays . sort ( arr ) ; while ( end - start > 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { start ++ ; } else { res ++ ; end -- ; } } if ( end - start == 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { res ++ ; start ++ ; end -- ; } else { res ++ ; end -- ; } } if ( start == end ) { res ++ ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 8 , 10 , 20 , 25 } ; int N = arr . length ; int K = 26 ; System . out . print ( cntMinSub ( arr , N , K ) ) ; } }
Count possible values of K such that X | Java program for the above approach ; Function to count integers K satisfying given equation ; Calculate the absoluter difference between a and b ; Iterate till sqrt of the difference ; Return the count ; Driver Code
import java . io . * ; class GFG { static int condition ( int a , int b ) { int d = Math . abs ( a - b ) , count = 0 ; for ( int i = 1 ; i <= Math . sqrt ( d ) ; i ++ ) { if ( d % i == 0 ) { if ( d / i == i ) count += 1 ; else count += 2 ; } } return count ; } public static void main ( String [ ] args ) { int x = 2 , y = 6 ; System . out . println ( condition ( x , y ) ) ; } }
Count pairs in an array whose product is composite number | Java Program to implement the above approach ; Function to check if a number is prime or not ; Check if N is multiple of i or not . ; If N is multiple of i . ; Function to get the count of pairs whose product is a composite number . ; Stores the count of pairs whose product is a composite number ; Generate all possible pairs ; Stores the product of element of current pair ; If prod is a composite number ; Driver Code
import java . io . * ; class GFG { static boolean isComposite ( int N ) { for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { return true ; } } return false ; } static int compositePair ( int arr [ ] , int N ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int prod = arr [ i ] * arr [ j ] ; if ( isComposite ( prod ) ) { res ++ ; } } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 8 } ; int N = arr . length ; System . out . println ( compositePair ( arr , N ) ) ; } }
Count N | Java program for the above approach ; Function for calculate ( x ^ y ) % mod in O ( log y ) ; Base Condition ; Transition state of power Function ; Function for counting total numbers that can be formed such that digits X , Y are present in each number ; Calculate the given expression ; Return the final answer ; Driver Code
import java . util . * ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static long power ( int x , int y ) { if ( y == 0 ) return 1 ; int p = ( int ) ( power ( x , y / 2 ) % mod ) ; p = ( p * p ) % mod ; if ( y % 2 == 1 ) { p = ( x * p ) % mod ; } return p ; } static int TotalNumber ( int N ) { int ans = ( int ) ( ( power ( 10 , N ) - 2 * power ( 9 , N ) + power ( 8 , N ) + 2 * mod ) % mod ) ; return ans ; } public static void main ( String [ ] args ) { int N = 10 , X = 3 , Y = 4 ; System . out . print ( TotalNumber ( N ) + "NEW_LINE"); } }
Array obtained by repeatedly reversing array after every insertion from given array | Java program of the above approach ; Function to generate the array by inserting array elements one by one followed by reversing the array ; Doubly ended Queue ; Iterate over the array ; Push array elements alternately to the front and back ; If size of list is odd ; Reverse the list ; Print the elements of the array ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void generateArray ( int arr [ ] , int n ) { Deque < Integer > ans = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) != 0 ) ans . addFirst ( arr [ i ] ) ; else ans . add ( arr [ i ] ) ; } if ( ( n & 1 ) != 0 ) { Collections . reverse ( Arrays . asList ( ans ) ) ; } for ( int x : ans ) { System . out . print ( x + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int n = 4 ; int arr [ ] = { 1 , 2 , 3 , 4 } ; generateArray ( arr , n ) ; } }
Find the greater number closest to N having at most one non | Java program to implement the above approach ; Function to get closest greater number with at most non zero digit ; Stores the closest greater number with at most one non - zero digit ; Stores length of str ; Append 10 to the end of resultant String ; Append n - 1 times '0' to the end of resultant String ; Driver Code
import java . util . * ; class GFG { static String closestgtNum ( String str ) { String res = " " ; int n = str . length ( ) ; if ( str . charAt ( 0 ) < '9' ) { res += ( char ) ( str . charAt ( 0 ) + 1 ) ; } else { res += ( char ) ( '1' ) ; res += ( char ) ( '0' ) ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { res += ( char ) ( '0' ) ; } return res ; } public static void main ( String [ ] args ) { String str = "120" ; System . out . print ( closestgtNum ( str ) ) ; } }
Count maximum non | Java program for the above approach ; Function to count maximum number of non - overlapping subarrays with sum equals to the target ; Stores the final count ; Next subarray should start from index >= availIdx ; Tracks the prefix sum ; Map to store the prefix sum for respective indices ; Check if cur_sum - target is present in the array or not ; Update the index of current prefix sum ; Return the count of subarrays ; Driver Code ; Given array arr [ ] ; Given sum target ; Function call
import java . util . * ; class GFG { static int maximumSubarrays ( int arr [ ] , int N , int target ) { int ans = 0 ; int availIdx = - 1 ; int cur_sum = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; mp . put ( 0 , 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { cur_sum += arr [ i ] ; if ( mp . containsKey ( cur_sum - target ) && mp . get ( cur_sum - target ) >= availIdx ) { ans ++ ; availIdx = i ; } mp . put ( cur_sum , i ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , - 1 , 4 , 3 , 6 , 4 , 5 , 1 } ; int N = arr . length ; int target = 6 ; System . out . print ( maximumSubarrays ( arr , N , target ) ) ; } }
Maximize array sum by replacing equal adjacent pairs by their sum and X respectively | Java program for the above approach ; Function to calculate x ^ y ; Base Case ; Find the value in temp ; If y is even ; Function that find the maximum possible sum of the array ; Print the result using the formula ; Driver Code ; Function Call
import java . io . * ; class GFG { static int power ( int x , int y ) { int temp ; if ( y == 0 ) return 1 ; temp = power ( x , y / 2 ) ; if ( y % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } public static void maximumPossibleSum ( int N , int X ) { System . out . println ( X * ( power ( 2 , N ) - 1 ) ) ; } public static void main ( String [ ] args ) { int N = 3 , X = 5 ; maximumPossibleSum ( N , X ) ; } }
Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively | Java program for the above approach ; Function to return the count of possible pairs of A and B whose Bitwise XOR is X and Y respectively ; Stores the count of pairs ; Iterate till any bit are set ; Extract i - th bit of X and Y ; Divide X and Y by 2 ; If Xi = 1 and Yi = 2 , multiply counter by 2 ; Increase required count ; If Xi = 1 and Yi = 1 ; No answer exists ; Return the final count ; Driver Code ; Given X and Y ; Function Call
import java . util . * ; class GFG { static int countOfPairs ( int x , int y ) { int counter = 1 ; while ( x > 0 y > 0 ) { int bit1 = x % 2 ; int bit2 = y % 2 ; x >>= 1 ; y >>= 1 ; if ( bit1 == 1 && bit2 == 0 ) { counter *= 2 ; continue ; } if ( ( bit1 & bit2 ) > 0 ) { counter = 0 ; break ; } } return counter ; } public static void main ( String [ ] args ) { int X = 2 , Y = 5 ; System . out . print ( countOfPairs ( X , Y ) ) ; } }
Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum | Java program for the above approach ; Function to return gcd of a and b ; Base Case ; Recursive Call ; Function to find gcd of array ; Initialise the result ; Traverse the array arr [ ] ; Update result as gcd of the result and arr [ i ] ; Return the resultant GCD ; Driver Code ; Given array arr [ ] ; Function Call
public class GCD { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findGCD ( int arr [ ] , int N ) { int result = 0 ; for ( int element : arr ) { result = gcd ( result , element ) ; if ( result == 1 ) { return 1 ; } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 } ; int N = arr . length ; System . out . println ( findGCD ( arr , N ) ) ; } }
Check if any permutation of array contains sum of every adjacent pair not divisible by 3 | Java program for the above approach ; Function to checks if any permutation of the array exists whose sum of adjacent pairs is not divisible by 3 ; Count remainder 0 ; Count remainder 1 ; Count remainder 2 ; Condition for valid arrangements ; Driver Code ; Given array arr [ ] ; Function Call
class GFG { static void factorsOf3 ( int arr [ ] , int N ) { int a = 0 , b = 0 , c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 3 == 0 ) a ++ ; else if ( arr [ i ] % 3 == 1 ) b ++ ; else if ( arr [ i ] % 3 == 2 ) c ++ ; } if ( a >= 1 && a <= b + c + 1 ) System . out . print ( " Yes " + "NEW_LINE"); else if ( a == 0 && b == 0 && c > 0 ) System . out . print ( " Yes " + "NEW_LINE"); else if ( a == 0 && c == 0 && b > 0 ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 } ; int N = arr . length ; factorsOf3 ( arr , N ) ; } }
Check if a number is a perfect square having all its digits as a perfect square | Java program for the above approach ; Function to check if digits of N is a perfect square or not ; Iterate over the digits ; Extract the digit ; Check if digit is a perfect square or not ; Divide N by 10 ; Return true ; Function to check if N is a perfect square or not ; If floor and ceil of n is not same ; Function to check if N satisfies the required conditions or not ; If both the conditions are satisfied ; Driver Code ; Function Call
import java . util . * ; class GFG { static boolean check_digits ( long N ) { while ( N > 0 ) { int n = ( int ) ( N % 10 ) ; if ( ( n != 0 ) && ( n != 1 ) && ( n != 4 ) && ( n != 9 ) ) { return false ; } N = N / 10 ; } return true ; } static boolean is_perfect ( long N ) { double n = Math . sqrt ( N ) ; if ( Math . floor ( n ) != Math . ceil ( n ) ) { return false ; } return true ; } static void isFullSquare ( long N ) { if ( is_perfect ( N ) && check_digits ( N ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { long N = 144 ; isFullSquare ( N ) ; } }
Count substrings with different first and last characters | Java program for the above approach ; Function to count the subStrings having different first & last character ; Stores frequency of each char ; Loop to store frequency of the characters in a Map ; To store final result ; Traversal of String ; Store answer for every iteration ; Map traversal ; Compare current char ; Print the final count ; Driver Code ; Given String ; Length of the String ; Function call
import java . util . * ; class GFG { static void countSubString ( char [ ] s , int n ) { HashMap < Character , Integer > mp = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . containsKey ( s [ i ] ) ) { mp . put ( s [ i ] , mp . get ( s [ i ] ) + 1 ) ; } else { mp . put ( s [ i ] , 1 ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; if ( mp . containsKey ( s [ i ] ) ) { mp . put ( s [ i ] , mp . get ( s [ i ] ) - 1 ) ; for ( Map . Entry < Character , Integer > value : mp . entrySet ( ) ) { if ( value . getKey ( ) == s [ i ] ) { continue ; } else { cnt += value . getValue ( ) ; } } ans += cnt ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String S = " abcab " ; int N = 5 ; countSubString ( S . toCharArray ( ) , N ) ; } }
Maximize count of empty water bottles from N filled bottles | Java program for the above approach ; Function to find the maximum bottles that can be emptied ; Iterate until a is non - zero ; Add the number of bottles that are emptied ; Update a after exchanging empty bottles ; Stores the number of bottles left after the exchange ; Return the answer ; Driver Code ; Function call
import java . util . * ; class GFG { static int maxBottles ( int n , int e ) { int s = 0 , b = 0 ; int a = n ; while ( a != 0 ) { s = s + a ; a = ( a + b ) / e ; b = n - ( a * e ) ; n = a + b ; } return s ; } public static void main ( String [ ] args ) { int n = 9 , e = 3 ; int s = maxBottles ( n , e ) ; System . out . print ( s + "NEW_LINE"); } }
Count all N digit numbers whose digits are multiple of X | Java program for the above approach ; Function to calculate x ^ n using binary - exponentiation ; Stores the resultant power ; Stores the value of x ^ ( n / 2 ) ; Function to count aint N - digit numbers whose digits are multiples of x ; Count aint digits which are multiples of x ; Check if current number is a multiple of X ; Increase count of multiples ; Check if it 's a 1 digit number ; Count the total numbers ; Return the total numbers ; Driver Code ; Given N and X ; Function Call
class GFG { static int power ( int x , int n ) { int temp ; if ( n == 0 ) return 1 ; temp = power ( x , n / 2 ) ; if ( n % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } static int count_Total_Numbers ( int n , int x ) { int total , multiples = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( i % x == 0 ) multiples ++ ; } if ( n == 1 ) return multiples ; total = ( multiples - 1 ) * power ( multiples , n - 1 ) ; return total ; } public static void main ( String [ ] args ) { int N = 1 , X = 3 ; System . out . printf ( " % d ▁ " , count_Total_Numbers ( N , X ) ) ; } }
Find the vertex diagonally opposite to the vertex M from an N | Java program for the above approach ; Function to return the required vertex ; Case 1 : ; Case 2 : ; Driver Code
class GFG { static int getPosition ( int N , int M ) { if ( M > ( N / 2 ) ) { return ( M - ( N / 2 ) ) ; } return ( M + ( N / 2 ) ) ; } public static void main ( String [ ] args ) { int N = 8 , M = 5 ; System . out . print ( getPosition ( N , M ) ) ; } }
Maximize count of strings of length 3 that can be formed from N 1 s and M 0 s | Java program for the above approach ; Function that counts the number of strings of length three that can be made with given m 0 s and n 1 s ; Iterate until N & M are positive ; Case 1 : ; Case 2 : ; Print the count of strings ; Driver Code ; Given count of 1 s and 0 s ; Function call
import java . util . * ; import java . lang . * ; class GFG { static void number_of_strings ( int N , int M ) { int ans = 0 ; while ( N > 0 && M > 0 ) { if ( N > M ) { if ( N >= 2 ) { N -= 2 ; -- M ; ++ ans ; } else { break ; } } else { if ( M >= 2 ) { M -= 2 ; -- N ; ++ ans ; } else { break ; } } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 4 , M = 19 ; number_of_strings ( N , M ) ; } }
Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum | Java program for the above approach ; Function that returns the minimum length of the array after merging unequal adjacent elements ; Stores the first element and its frequency ; Traverse the array ; If all elements are equal ; No merge - pair operations can be performed ; Otherwise ; Driver Code ; Given array ; Length of the array ; Function Call
class GFG { static int minLength ( int A [ ] , int N ) { int elem = A [ 0 ] , count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( A [ i ] == elem ) { count ++ ; } else { break ; } } if ( count == N ) return N ; else return 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 3 , 1 } ; int N = arr . length ; System . out . print ( minLength ( arr , N ) + "NEW_LINE"); } }