codeparrot/xlcost-text-to-code huggingface dataset:
Count half nodes in a Binary tree ( Iterative and Recursive ) | Java program to count half nodes in a Binary Tree using Iterative approach ; Class to represent Tree node ; Class to count half nodes of Tree ; Function to get the count of half Nodes in a binary tree ; If tree is empty ; Do level order traversal starting from root ; Initialize count of half nodes ; Enqueue left child ; Enqueue right child ; Driver Program to test above function
import java . util . Queue ; import java . util . LinkedList ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class BinaryTree { Node root ; int gethalfCount ( ) { if ( root == null ) return 0 ; Queue < Node > queue = new LinkedList < Node > ( ) ; queue . add ( root ) ; int count = 0 ; while ( ! queue . isEmpty ( ) ) { Node temp = queue . poll ( ) ; if ( temp . left != null && temp . right == null temp . left == null && temp . right != null ) count ++ ; if ( temp . left != null ) queue . add ( temp . left ) ; if ( temp . right != null ) queue . add ( temp . right ) ; } return count ; } public static void main ( String args [ ] ) { BinaryTree tree_level = new BinaryTree ( ) ; tree_level . root = new Node ( 2 ) ; tree_level . root . left = new Node ( 7 ) ; tree_level . root . right = new Node ( 5 ) ; tree_level . root . left . right = new Node ( 6 ) ; tree_level . root . left . right . left = new Node ( 1 ) ; tree_level . root . left . right . right = new Node ( 11 ) ; tree_level . root . right . right = new Node ( 9 ) ; tree_level . root . right . right . left = new Node ( 4 ) ; System . out . println ( tree_level . gethalfCount ( ) ) ; } }
Count N | Java program for the above approach ; Function to count N digit numbers whose digits are less than or equal to the absolute difference of previous two digits ; If all digits are traversed ; If the state has already been computed ; If the current digit is 1 , any digit from [ 1 - 9 ] can be placed . If N == 1 , 0 can also be placed . ; If the current digit is 2 , any digit from [ 0 - 9 ] can be placed ; For other digits , any digit from 0 to abs ( prev1 - prev2 ) can be placed ; Return the answer ; Driver Code ; Input ; Function call
import java . util . * ; class GFG { static int dp [ ] [ ] [ ] = new int [ 50 ] [ 10 ] [ 10 ] ; static void initialize ( ) { for ( int i = 0 ; i < 50 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { for ( int k = 0 ; k < 10 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } } static int countOfNumbers ( int digit , int prev1 , int prev2 , int N ) { if ( digit == N + 1 ) return 1 ; if ( dp [ digit ] [ prev1 ] [ prev2 ] != - 1 ) return dp [ digit ] [ prev1 ] [ prev2 ] ; dp [ digit ] [ prev1 ] [ prev2 ] = 0 ; if ( digit == 1 ) { for ( int j = ( N == 1 ? 0 : 1 ) ; j <= 9 ; ++ j ) { dp [ digit ] [ prev1 ] [ prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) ; } } else if ( digit == 2 ) { for ( int j = 0 ; j <= 9 ; ++ j ) { dp [ digit ] [ prev1 ] [ prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) ; } } else { for ( int j = 0 ; j <= Math . abs ( prev1 - prev2 ) ; ++ j ) { dp [ digit ] [ prev1 ] [ prev2 ] += countOfNumbers ( digit + 1 , j , prev1 , N ) ; } } return dp [ digit ] [ prev1 ] [ prev2 ] ; } public static void main ( String [ ] args ) { initialize ( ) ; int N = 3 ; System . out . print ( countOfNumbers ( 1 , 0 , 0 , N ) ) ; } }
Length of longest subset consisting of A 0 s and B 1 s from an array of strings | Java program for the above approach ; Function to count number of 0 s present in the string ; Stores the count of 0 s ; Iterate over characters of string ; If current character is '0' ; Recursive Function to find the length of longest subset from given array of strings with at most A 0 s and B 1 s ; If idx is equal to N or A + B is equal to 0 ; If the state is already calculated ; Stores the count of 0 's ; Stores the count of 1 's ; Stores the length of longest by including arr [ idx ] ; If zero is less than A and one is less than B ; Stores the length of longest subset by excluding arr [ idx ] ; Assign ; Return ; Function to find the length of the longest subset of an array of strings with at most A 0 s and B 1 s ; Stores all Dp - states ; Return ; Driver Code
import java . io . * ; class GFG { static int count0 ( String s ) { int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '0' ) { count ++ ; } } return count ; } static int solve ( String [ ] vec , int A , int B , int idx , int dp [ ] [ ] [ ] ) { if ( idx == vec . length A + B == 0 ) { return 0 ; } if ( dp [ A ] [ B ] [ idx ] > 0 ) { return dp [ A ] [ B ] [ idx ] ; } int zero = count0 ( vec [ idx ] ) ; int one = vec [ idx ] . length ( ) - zero ; int inc = 0 ; if ( zero <= A && one <= B ) { inc = 1 + solve ( vec , A - zero , B - one , idx + 1 , dp ) ; } int exc = solve ( vec , A , B , idx + 1 , dp ) ; dp [ A ] [ B ] [ idx ] = Math . max ( inc , exc ) ; return dp [ A ] [ B ] [ idx ] ; } static int MaxSubsetlength ( String [ ] arr , int A , int B ) { int dp [ ] [ ] [ ] = new int [ A + 1 ] [ B + 1 ] [ arr . length + 1 ] ; return solve ( arr , A , B , 0 , dp ) ; } public static void main ( String [ ] args ) { String arr [ ] = { "1" , "0" , "10" } ; int A = 1 , B = 1 ; System . out . println ( MaxSubsetlength ( arr , A , B ) ) ; } }
Check if a given pattern exists in a given string or not | Java program for the above approach ; Function to check if the pattern consisting of ' * ' , ' . ' and lowercase characters matches the text or not ; Base Case ; Stores length of text ; Stores length of pattern ; dp [ i ] [ j ] : Check if { text [ 0 ] , . . text [ i ] } matches { pattern [ 0 ] , ... pattern [ j ] } or not ; Base Case ; Iterate over the characters of the string pattern ; Update dp [ 0 ] [ i + 1 ] ; Iterate over the characters of both the strings ; If current character in the pattern is ' . ' ; Update dp [ i + 1 ] [ j + 1 ] ; If current character in both the strings are equal ; Update dp [ i + 1 ] [ j + 1 ] ; If current character in the pattern is ' * ' ; Update dp [ i + 1 ] [ j + 1 ] ; Update dp [ i + 1 ] [ j + 1 ] ; Return dp [ M ] [ N ] ; Driver Code
import java . io . * ; class GFG { static boolean isMatch ( String text , String pattern ) { if ( text == null pattern == null ) { return false ; } int N = text . length ( ) ; int M = pattern . length ( ) ; boolean [ ] [ ] dp = new boolean [ N + 1 ] [ M + 1 ] ; dp [ 0 ] [ 0 ] = true ; for ( int i = 0 ; i < M ; i ++ ) { if ( pattern . charAt ( i ) == ' * ' && dp [ 0 ] [ i - 1 ] ) { dp [ 0 ] [ i + 1 ] = true ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( pattern . charAt ( j ) == ' . ' ) { dp [ i + 1 ] [ j + 1 ] = dp [ i ] [ j ] ; } if ( pattern . charAt ( j ) == text . charAt ( i ) ) { dp [ i + 1 ] [ j + 1 ] = dp [ i ] [ j ] ; } if ( pattern . charAt ( j ) == ' * ' ) { if ( pattern . charAt ( j - 1 ) != text . charAt ( i ) && pattern . charAt ( j - 1 ) != ' . ' ) { dp [ i + 1 ] [ j + 1 ] = dp [ i + 1 ] [ j - 1 ] ; } else { dp [ i + 1 ] [ j + 1 ] = ( dp [ i + 1 ] [ j ] dp [ i ] [ j + 1 ] dp [ i + 1 ] [ j - 1 ] ) ; } } } } return dp [ N ] [ M ] ; } public static void main ( String [ ] args ) { String text = " geeksforgeeks " ; String pattern = " ge * ksforgeeks " ; if ( isMatch ( text , pattern ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Count number of unique ways to paint a N x 3 grid | Java program for the above approach ; Function to count the number of ways to paint N * 3 grid based on given conditions ; Count of ways to pain a row with same colored ends ; Count of ways to pain a row with different colored ends ; Traverse up to ( N - 1 ) th row ; For same colored ends ; For different colored ends ; Print the total number of ways ; Driver code ; Function call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void waysToPaint ( int n ) { long same = 6 ; long diff = 6 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { long sameTmp = 3 * same + 2 * diff ; long diffTmp = 2 * same + 2 * diff ; same = sameTmp ; diff = diffTmp ; } System . out . println ( same + diff ) ; } public static void main ( String [ ] args ) { int N = 2 ; waysToPaint ( N ) ; } }
Minimize sum of incompatibilities of K equal | Java program for the above approach ; Function to find the minimum incompatibility sum ; Stores the count of element ; Traverse the array ; If number i not occurs in Map ; Put the element in the Map ; Increment the count of i in the Hash Map ; If count of i in Map is greater than K then return - 1 ; Stores all total state ; Traverse over all the state ; If number of set bit is equal to N / K ; Stores the minimum value at a state ; Initialize the dp state with - 1 ; Call the recursive function ; Recursive function to find the minimum required value ; Base Case ; Stores the minimum value of the current state ; If dp [ state ] [ index ] is already calculated ; return dp [ state ] [ index ] ; Traverse over all the bits ; If count of set bit is N / K ; Store the new state after choosing N / K elements ; Stores the minimum and maximum of current subset ; Stores if the elements have been already selected or not ; Stores if it is possible to select N / K elements ; Traverse over the array ; If not chosen already for another subset ; If already chosen for another subset or current subset ; Mark the good false ; Mark the current number visited ; Mark the current position in mask newstate ; Update the maximum and minimum ; If good is true then Update the res ; Update the current sp state ; Return the res ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class Solution { int k ; int n ; int goal ; int dp [ ] [ ] ; List < Integer > bits = new ArrayList < > ( ) ; public int minimumIncompatibility ( int [ ] A , int k ) { this . n = A . length ; this . k = k ; goal = n / k ; Map < Integer , Integer > map = new HashMap < > ( ) ; for ( int i : A ) { if ( ! map . containsKey ( i ) ) map . put ( i , 0 ) ; map . put ( i , map . get ( i ) + 1 ) ; if ( map . get ( i ) > k ) return - 1 ; } int state = ( 1 << n ) - 1 ; for ( int i = 0 ; i <= state ; i ++ ) { if ( Integer . bitCount ( i ) == goal ) bits . add ( i ) ; } dp = new int [ 1 << n ] [ k ] ; for ( int i = 0 ; i < dp . length ; i ++ ) { Arrays . fill ( dp [ i ] , - 1 ) ; } return dfs ( A , state , 0 ) ; } public int dfs ( int A [ ] , int state , int index ) { if ( index >= k ) { return 0 ; } int res = 1000 ; if ( dp [ state ] [ index ] != - 1 ) { return dp [ state ] [ index ] ; } for ( int bit : bits ) { if ( Integer . bitCount ( bit ) == goal ) { int newstate = state ; int mn = 100 , mx = - 1 ; boolean visit [ ] = new boolean [ n + 1 ] ; boolean good = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( bit & ( 1 << j ) ) != 0 ) { if ( visit [ A [ j ] ] == true || ( state & ( 1 << j ) ) == 0 ) { good = false ; break ; } visit [ A [ j ] ] = true ; newstate = newstate ^ ( 1 << j ) ; mx = Math . max ( mx , A [ j ] ) ; mn = Math . min ( mn , A [ j ] ) ; } } if ( good ) { res = Math . min ( res , mx - mn + dfs ( A , newstate , index + 1 ) ) ; } } } dp [ state ] [ index ] = res ; return res ; } } class GFG { public static void main ( String [ ] args ) { Solution st = new Solution ( ) ; int [ ] arr = { 1 , 2 , 1 , 4 } ; int K = 2 ; System . out . print ( st . minimumIncompatibility ( arr , K ) ) ; } }
Maximize sum by selecting X different | Java program for the above approach ; Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ] ; Base Cases ; Selecting i - th element from A [ ] ; Selecting i - th element from B [ ] ; Selecting i - th element from C [ ] ; i - th elements not selected from any of the arrays ; Select the maximum sum from all the possible calls ; Driver Code ; Given X , Y and Z ; Given A [ ] ; Given B [ ] ; Given C [ ] ; Given Size ; Function Call
class GFG { static int FindMaxS ( int X , int Y , int Z , int n , int A [ ] , int B [ ] , int C [ ] ) { if ( X < 0 Y < 0 Z < 0 ) return Integer . MIN_VALUE ; if ( n < 0 ) return 0 ; int ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; int ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; int co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; int no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; int maximum = Math . max ( ch , Math . max ( ca , Math . max ( co , no ) ) ) ; return maximum ; } public static void main ( String [ ] args ) { int X = 1 ; int Y = 1 ; int Z = 1 ; int A [ ] = { 10 , 0 , 5 } ; int B [ ] = { 5 , 10 , 0 } ; int C [ ] = { 0 , 5 , 10 } ; int n = B . length ; System . out . println ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) ; } }
Rearrange array by interchanging positions of even and odd elements in the given array | Java program for the above approach ; Function to replace each even element by odd and vice - versa in a given array ; Stores length of array ; Traverse array ; If current element is even then swap it with odd ; Perform Swap ; Change the sign ; If current element is odd then swap it with even ; Perform Swap ; Change the sign ; Marked element positive ; Print final array ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static void replace ( int [ ] arr ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] >= 0 && arr [ j ] >= 0 && arr [ i ] % 2 == 0 && arr [ j ] % 2 != 0 ) { int tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; arr [ j ] = - arr [ j ] ; break ; } else if ( arr [ i ] >= 0 && arr [ j ] >= 0 && arr [ i ] % 2 != 0 && arr [ j ] % 2 == 0 ) { int tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; arr [ j ] = - arr [ j ] ; break ; } } } for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = Math . abs ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 4 } ; replace ( arr ) ; } }
Count half nodes in a Binary tree ( Iterative and Recursive ) | Java program to count half nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of half Nodes in a binary tree ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; Driver program ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree shown in above example
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static int gethalfCount ( Node root ) { if ( root == null ) return 0 ; int res = 0 ; if ( ( root . left == null && root . right != null ) || ( root . left != null && root . right == null ) ) res ++ ; res += ( gethalfCount ( root . left ) + gethalfCount ( root . right ) ) ; return res ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 7 ) ; root . right = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 11 ) ; root . right . right = newNode ( 9 ) ; root . right . right . left = newNode ( 4 ) ; System . out . println ( gethalfCount ( root ) ) ; } }
Minimize cost to reduce array to a single element by replacing K consecutive elements by their sum | Java program to implement the above approach ; Function to find the minimum cost to reduce given array to a single element by replacing consecutive K array elements ; If ( N - 1 ) is not multiple of ( K - 1 ) ; Store prefix sum of the array ; Iterate over the range [ 1 , N ] ; Update prefixSum [ i ] ; dp [ i ] [ j ] : Store minimum cost to merge array elements interval [ i , j ] ; L : Stores length of interval [ i , j ] ; Iterate over each interval [ i , j ] of length L in in [ 0 , N ] ; Stores index of last element of the interval [ i , j ] ; If L is greater than K ; Update dp [ i ] [ j ] ; If ( L - 1 ) is multiple of ( K - 1 ) ; Update dp [ i ] [ j ] ; Return dp [ 0 ] [ N - 1 ] ; Driver Code ; Function Call
import java . util . * ; class GFG { static int minimumCostToMergeK ( int arr [ ] , int K , int N ) { if ( ( N - 1 ) % ( K - 1 ) != 0 ) { return - 1 ; } int [ ] prefixSum = new int [ N + 1 ] ; for ( int i = 1 ; i < ( N + 1 ) ; i ++ ) { prefixSum [ i ] = ( prefixSum [ i - 1 ] + arr [ i - 1 ] ) ; } int [ ] [ ] dp = new int [ N ] [ N ] ; for ( int L = K ; L < ( N + 1 ) ; L ++ ) { for ( int i = 0 ; i < ( N - L + 1 ) ; i ++ ) { int j = i + L - 1 ; if ( L > K ) { int temp = Integer . MAX_VALUE ; for ( int x = i ; x < j ; x += K - 1 ) { temp = Math . min ( temp , dp [ i ] [ x ] + dp [ x + 1 ] [ j ] ) ; } dp [ i ] [ j ] = temp ; } if ( ( L - 1 ) % ( K - 1 ) == 0 ) { dp [ i ] [ j ] += ( prefixSum [ j + 1 ] - prefixSum [ i ] ) ; } } } return dp [ 0 ] [ N - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 1 , 2 , 6 } ; int K = 3 ; int N = arr . length ; System . out . print ( minimumCostToMergeK ( arr , K , N ) ) ; } }
Check if end of a sorted Array can be reached by repeated jumps of one more , one less or same number of indices as previous jump | Java program for the above approach ; Utility function to check if it is possible to move from index 1 to N - 1 ; Successfully reached end index ; memo [ i ] [ j ] is already calculated ; Check if there is any index having value of A [ i ] + j - 1 , A [ i ] + j or A [ i ] + j + 1 ; If A [ k ] > A [ i ] + j + 1 , can 't make a move further ; It 's possible to move A[k] ; Check is it possible to move from index k having previously taken A [ k ] - A [ i ] steps ; If yes then break the loop ; Store value of flag in memo ; Return memo [ i ] [ j ] ; Function to check if it is possible to move from index 1 to N - 1 ; Stores the memoized state ; Initialize all values as - 1 ; Initially , starting index = 1 ; Function call ; Driver Code ; Function Call
import java . io . * ; class GFG { static int N = 8 ; static int check ( int [ ] [ ] memo , int i , int j , int [ ] A ) { if ( i == N - 1 ) return 1 ; if ( memo [ i ] [ j ] != - 1 ) return memo [ i ] [ j ] ; int flag = 0 , k ; for ( k = i + 1 ; k < N ; k ++ ) { if ( A [ k ] - A [ i ] > j + 1 ) break ; if ( A [ k ] - A [ i ] >= j - 1 && A [ k ] - A [ i ] <= j + 1 ) flag = check ( memo , k , A [ k ] - A [ i ] , A ) ; if ( flag != 0 ) break ; } memo [ i ] [ j ] = flag ; return memo [ i ] [ j ] ; } static void checkEndReach ( int A [ ] , int K ) { int [ ] [ ] memo = new int [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { memo [ i ] [ j ] = - 1 ; } } int startIndex = 1 ; if ( check ( memo , startIndex , K , A ) != 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int [ ] A = { 0 , 1 , 3 , 5 , 6 , 8 , 12 , 17 } ; int K = 1 ; checkEndReach ( A , K ) ; } }
Maximum subsequence sum such that no K elements are consecutive | Java program for the above approach ; Function to find the maximum sum of a subsequence consisting of no K consecutive array elements ; Stores states of dp ; Initialise dp state ; Stores the prefix sum ; Update the prefix sum ; Base case for i < K ; For indices less than k take all the elements ; For i >= K case ; Skip each element from i to ( i - K + 1 ) to ensure that no K elements are consecutive ; Update the current dp state ; dp [ N ] stores the maximum sum ; Driver Code ; Given array arr [ ] ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static int Max_Sum ( int [ ] arr , int K , int N ) { int [ ] dp = new int [ N + 1 ] ; Arrays . fill ( dp , 0 ) ; int [ ] prefix = new int [ N + 1 ] ; prefix [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i - 1 ] ; } dp [ 0 ] = 0 ; for ( int i = 1 ; i <= K - 1 ; i ++ ) { dp [ i ] = prefix [ i ] ; } for ( int i = K ; i <= N ; ++ i ) { for ( int j = i ; j >= ( i - K + 1 ) ; j -- ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j - 1 ] + prefix [ i ] - prefix [ j ] ) ; } } return dp [ N ] ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 12 , 22 , 18 , 34 , 12 , 25 } ; int N = arr . length ; int K = 5 ; System . out . println ( Max_Sum ( arr , K , N ) ) ; } }
Count possible splits of sum N into K integers such that the minimum is at least P | Java program for the above approach ; Function that finds the value of the Binomial Coefficient C ( n , k ) ; Stores the value of Binomial Coefficient in bottom up manner ; Base Case ; Find the value using previously stored values ; Return the value of C ( N , K ) ; Function that count the number of ways to divide N into K integers >= P such that their sum is N ; Update the value of N ; Find the binomial coefficient recursively ; Driver Code ; Given K , N , and P
import java . util . * ; class GFG { static int binomialCoeff ( int n , int k ) { int [ ] [ ] C = new int [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } static int waysToSplitN ( int k , int n , int P ) { int new_N = n - k * P ; return binomialCoeff ( new_N + k - 1 , new_N ) ; } public static void main ( String [ ] args ) { int K = 3 , N = 8 , P = 2 ; System . out . print ( waysToSplitN ( K , N , P ) ) ; } }
Minimum pair merge operations required to make Array non | Java program to implement the above approach ; Function to find the minimum operations to make the array Non - increasing ; Size of the array ; Dp table initialization ; dp [ i ] : Stores minimum number of operations required to make subarray { A [ i ] , ... , A [ N ] } non - increasing ; Increment the value of j ; Add current value to sum ; Update the dp tables ; Return the answer ; Driver code
import java . util . * ; class GFG { static int solve ( int [ ] a ) { int n = a . length ; int [ ] dp = new int [ n + 1 ] ; int [ ] val = new int [ n + 1 ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int sum = a [ i ] ; int j = i ; while ( j + 1 < n && sum < val [ j + 1 ] ) { j ++ ; sum += a [ j ] ; } dp [ i ] = ( j - i ) + dp [ j + 1 ] ; val [ i ] = sum ; } return dp [ 0 ] ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 9 , 1 } ; System . out . print ( solve ( arr ) ) ; } }
Count full nodes in a Binary tree ( Iterative and Recursive ) | Java program to count full nodes in a Binary Tree using Iterative approach ; Class to represent Tree node ; Class to count full nodes of Tree ; Function to get the count of full Nodes in a binary tree ; If tree is empty ; Initialize empty queue . ; Do level order traversal starting from root ; Initialize count of full nodes ; Enqueue left child ; Enqueue right child ; Driver program ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
import java . util . Queue ; import java . util . LinkedList ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class BinaryTree { Node root ; int getfullCount ( ) { if ( root == null ) return 0 ; Queue < Node > queue = new LinkedList < Node > ( ) ; queue . add ( root ) ; int count = 0 ; while ( ! queue . isEmpty ( ) ) { Node temp = queue . poll ( ) ; if ( temp . left != null && temp . right != null ) count ++ ; if ( temp . left != null ) { queue . add ( temp . left ) ; } if ( temp . right != null ) { queue . add ( temp . right ) ; } } return count ; } public static void main ( String args [ ] ) { BinaryTree tree_level = new BinaryTree ( ) ; tree_level . root = new Node ( 2 ) ; tree_level . root . left = new Node ( 7 ) ; tree_level . root . right = new Node ( 5 ) ; tree_level . root . left . right = new Node ( 6 ) ; tree_level . root . left . right . left = new Node ( 1 ) ; tree_level . root . left . right . right = new Node ( 11 ) ; tree_level . root . right . right = new Node ( 9 ) ; tree_level . root . right . right . left = new Node ( 4 ) ; System . out . println ( tree_level . getfullCount ( ) ) ; } }