codeparrot/xlcost-text-to-code huggingface dataset:
Count number of increasing sub | Java implementation of the approach ; Segment tree array ; Function for point update in segment tree ; Base case ; If l == r == up ; Mid element ; Updating the segment tree ; Function for the range sum - query ; Base case ; Mid element ; Calling for the left and the right subtree ; Function to return the count ; Copying array arr to sort it ; Sorting array brr ; Map to store the rank of each element ; dp array ; To store the final answer ; Updating the dp array ; Rank of the element ; Solving the dp - states using segment tree ; Updating the final answer ; Updating the segment tree ; Returning the final answer ; Driver code
import java . util . * ; class GFG { static final int N = 10000 ; static int [ ] seg = new int [ 3 * N ] ; static int update ( int in , int l , int r , int up_in , int val ) { if ( r < up_in l > up_in ) return seg [ in ] ; if ( l == up_in && r == up_in ) return seg [ in ] = val ; int m = ( l + r ) / 2 ; return seg [ in ] = update ( 2 * in + 1 , l , m , up_in , val ) + update ( 2 * in + 2 , m + 1 , r , up_in , val ) ; } static int query ( int in , int l , int r , int l1 , int r1 ) { if ( l > r ) return 0 ; if ( r < l1 l > r1 ) return 0 ; if ( l1 <= l && r <= r1 ) return seg [ in ] ; int m = ( l + r ) / 2 ; return query ( 2 * in + 1 , l , m , l1 , r1 ) + query ( 2 * in + 2 , m + 1 , r , l1 , r1 ) ; } static int findCnt ( int [ ] arr , int n ) { int [ ] brr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; Arrays . sort ( brr ) ; HashMap < Integer , Integer > r = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) r . put ( brr [ i ] , i + 1 ) ; int dp [ ] = new int [ n ] ; int ans = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int rank = r . get ( arr [ i ] ) ; dp [ i ] = 1 + query ( 0 , 0 , n - 1 , rank , n - 1 ) ; ans += dp [ i ] ; update ( 0 , 0 , n - 1 , rank - 1 , dp [ i ] + query ( 0 , 0 , n - 1 , rank - 1 , rank - 1 ) ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 10 , 9 } ; int n = arr . length ; System . out . print ( findCnt ( arr , n ) ) ; } }
Minimum cost to build N blocks from one block | Java program to Minimum cost to build N blocks from one block ; Function to calculate min cost to build N blocks ; Initialize base case ; Recurence when i is odd ; Recurence when i is even ; Driver code
class GFG { static int minCost ( int n , int x , int y , int z ) { int dp [ ] = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 1 ) { dp [ i ] = Math . min ( dp [ ( i + 1 ) / 2 ] + x + z , dp [ i - 1 ] + y ) ; } else { dp [ i ] = Math . min ( dp [ i / 2 ] + x , dp [ i - 1 ] + y ) ; } } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 5 , x = 2 , y = 1 , z = 3 ; System . out . print ( minCost ( n , x , y , z ) + "NEW_LINE"); } }
Size of the largest divisible subset in an Array | Java implementation of the above approach ; Function to find the size of the largest divisible subarray ; Mark all elements of the array ; Traverse reverse ; For all multiples of i ; Return the required answer ; Driver code ; Function call
class GFG { final static int N = 1000005 ; static int maximum_set ( int a [ ] , int n ) { int dp [ ] = new int [ N ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ a [ i ] ] = 1 ; int ans = 1 ; for ( int i = N - 1 ; i >= 1 ; i -- ) { if ( dp [ i ] != 0 ) { for ( int j = 2 * i ; j < N ; j += i ) { dp [ i ] = Math . max ( dp [ i ] , 1 + dp [ j ] ) ; ans = Math . max ( ans , dp [ i ] ) ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 6 , 8 , 10 , 18 , 21 , 24 } ; int n = arr . length ; System . out . println ( maximum_set ( arr , n ) ) ; } }
Longest sub | Java implementation of th approach ; Function to return the length of the largest sub - string divisible by 3 ; Base - case ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
class GFG { final static int N = 100 ; final static int INT_MIN = Integer . MIN_VALUE ; static int dp [ ] [ ] = new int [ N ] [ 3 ] ; static int v [ ] [ ] = new int [ N ] [ 3 ] ; static int findLargestString ( String s , int i , int r ) { if ( i == s . length ( ) ) { if ( r == 0 ) return 0 ; else return INT_MIN ; } if ( v [ i ] [ r ] == 1 ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = Math . max ( 1 + findLargestString ( s , i + 1 , ( r * 2 + ( s . charAt ( i ) - '0' ) ) % 3 ) , findLargestString ( s , i + 1 , r ) ) ; return dp [ i ] [ r ] ; } public static void main ( String [ ] args ) { String s = "101" ; System . out . print ( findLargestString ( s , 0 , 0 ) ) ; } }
Number of sub | Java implementation of th approach ; Function to return the number of sub - sequences divisible by 3 ; Base - cases ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code
class GFG { static final int N = 100 ; static int dp [ ] [ ] = new int [ N ] [ 3 ] ; static int v [ ] [ ] = new int [ N ] [ 3 ] ; static int findCnt ( String s , int i , int r ) { if ( i == s . length ( ) ) { if ( r == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ r ] == 1 ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = findCnt ( s , i + 1 , ( r * 2 + ( s . charAt ( i ) - '0' ) ) % 3 ) + findCnt ( s , i + 1 , r ) ; return dp [ i ] [ r ] ; } public static void main ( String [ ] args ) { String s = "11" ; System . out . print ( findCnt ( s , 0 , 0 ) - 1 ) ; } }
Remove minimum elements from ends of array so that sum decreases by at least K | O ( N ) | Java implementation of the approach ; Function to return the count of minimum elements to be removed from the ends of the array such that the sum of the array decrease by at least K ; To store the final answer ; Maximum possible sum required ; Left point ; Right pointer ; Total current sum ; Two pointer loop ; If the sum fits ; Update the answer ; Update the total sum ; Increment the left pointer ; Driver code
class GFG { static int minCount ( int arr [ ] , int n , int k ) { int ans = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; sum -= k ; int l = 0 ; int r = 0 ; int tot = 0 ; while ( l < n ) { if ( tot <= sum ) { ans = Math . max ( ans , r - l ) ; if ( r == n ) break ; tot += arr [ r ++ ] ; } else { tot -= arr [ l ++ ] ; } } return ( n - ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 11 , 5 , 5 } ; int n = arr . length ; int k = 11 ; System . out . println ( minCount ( arr , n , k ) ) ; } }
Minimum cost to merge all elements of List | Java implementation of the approach ; To store the states of DP ; Function to return the minimum merge cost ; Base case ; If the state has been solved before ; Marking the state as solved ; Reference to dp [ i ] [ j ] ; To store the sum of all the elements in the subarray arr [ i ... j ] ; Loop to iterate the recurrence ; Returning the solved value ; Driver code
class GFG { static final int N = 401 ; static int [ ] [ ] dp = new int [ N ] [ N ] ; static boolean [ ] [ ] v = new boolean [ N ] [ N ] ; static int minMergeCost ( int i , int j , int [ ] arr ) { if ( i == j ) return 0 ; if ( v [ i ] [ j ] ) return dp [ i ] [ j ] ; v [ i ] [ j ] = true ; int x = dp [ i ] [ j ] ; x = Integer . MAX_VALUE ; int tot = 0 ; for ( int k = i ; k <= j ; k ++ ) tot += arr [ k ] ; for ( int k = i + 1 ; k <= j ; k ++ ) { x = Math . min ( x , tot + minMergeCost ( i , k - 1 , arr ) + minMergeCost ( k , j , arr ) ) ; } return x ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 7 } ; int n = arr . length ; System . out . print ( minMergeCost ( 0 , n - 1 , arr ) ) ; } }
Expected number of moves to reach the end of a board | Dynamic programming | Java implementation of the approach ; To store the states of dp ; To determine whether a state has been solved before ; Function to return the count ; Base cases ; If a state has been solved before it won 't be evaluated again ; Recurrence relation ; Driver code
class GFG { static int maxSize = 50 ; static double dp [ ] = new double [ maxSize ] ; static int v [ ] = new int [ maxSize ] ; static double expectedSteps ( int x ) { if ( x == 0 ) return 0 ; if ( x <= 5 ) return 6 ; if ( v [ x ] == 1 ) return dp [ x ] ; v [ x ] = 1 ; dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 ; return dp [ x ] ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( expectedSteps ( n - 1 ) ) ; } }
Number of subsequences in a given binary string divisible by 2 | Java implementation of the approach ; Function to return the count of the required subsequences ; To store the final answer ; Multiplier ; Loop to find the answer ; Condition to update the answer ; updating multiplier ; Driver code
class GFG { static int countSubSeq ( String str , int len ) { int ans = 0 ; int mul = 1 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == '0' ) ans += mul ; mul *= 2 ; } return ans ; } public static void main ( String [ ] args ) { String str = "10010" ; int len = str . length ( ) ; System . out . print ( countSubSeq ( str , len ) ) ; } }
Knapsack with large Weights | Java implementation of the approach ; To store the states of DP ; Function to solve the recurrence relation ; Base cases ; Marking state as solved ; Recurrence relation ; Function to return the maximum weight ; Iterating through all possible values to find the the largest value that can be represented by the given weights ; Driver code
class GFG { static final int V_SUM_MAX = 1000 ; static final int N_MAX = 100 ; static final int W_MAX = 10000000 ; static int dp [ ] [ ] = new int [ V_SUM_MAX + 1 ] [ N_MAX ] ; static boolean v [ ] [ ] = new boolean [ V_SUM_MAX + 1 ] [ N_MAX ] ; static int solveDp ( int r , int i , int w [ ] , int val [ ] , int n ) { if ( r <= 0 ) return 0 ; if ( i == n ) return W_MAX ; if ( v [ r ] [ i ] ) return dp [ r ] [ i ] ; v [ r ] [ i ] = true ; dp [ r ] [ i ] = Math . min ( solveDp ( r , i + 1 , w , val , n ) , w [ i ] + solveDp ( r - val [ i ] , i + 1 , w , val , n ) ) ; return dp [ r ] [ i ] ; } static int maxWeight ( int w [ ] , int val [ ] , int n , int c ) { for ( int i = V_SUM_MAX ; i >= 0 ; i -- ) { if ( solveDp ( i , 0 , w , val , n ) <= c ) { return i ; } } return 0 ; } public static void main ( String [ ] args ) { int w [ ] = { 3 , 4 , 5 } ; int val [ ] = { 30 , 50 , 60 } ; int n = w . length ; int C = 8 ; System . out . println ( maxWeight ( w , val , n , C ) ) ; } }
Sum of lengths of all paths possible in a given tree | Java implementation of the approach ; Number of vertices ; Adjacency list representation of the tree ; Array that stores the subtree size ; Array to mark all the vertices which are visited ; Utility function to create an edge between two vertices ; Add a to b 's list ; Add b to a 's list ; Function to calculate the subtree size ; Mark visited ; For every adjacent node ; If not already visited ; Recursive call for the child ; Function to calculate the contribution of each edge ; Mark current node as visited ; For every adjacent node ; If it is not already visisted ; Function to return the required sum ; First pass of the dfs ; Second pass ; Driver code
import java . util . * ; @ SuppressWarnings ( " unchecked " ) class GFG { static int sz = 100005 ; static int n ; static ArrayList [ ] tree = new ArrayList [ sz ] ; static int [ ] subtree_size = new int [ sz ] ; static int [ ] vis = new int [ sz ] ; static void AddEdge ( int a , int b ) { tree [ a ] . add ( b ) ; tree [ b ] . add ( a ) ; } static int dfs ( int node ) { vis [ node ] = 1 ; subtree_size [ node ] = 1 ; for ( int child : ( ArrayList < Integer > ) tree [ node ] ) { if ( vis [ child ] == 0 ) { subtree_size [ node ] += dfs ( child ) ; } } return subtree_size [ node ] ; } static int contribution ( int node , int ans ) { vis [ node ] = 1 ; for ( int child : ( ArrayList < Integer > ) tree [ node ] ) { if ( vis [ child ] == 0 ) { ans += ( subtree_size [ child ] * ( n - subtree_size [ child ] ) ) ; ans = contribution ( child , ans ) ; } } return ans ; } static int getSum ( ) { Arrays . fill ( vis , 0 ) ; dfs ( 0 ) ; int ans = 0 ; Arrays . fill ( vis , 0 ) ; ans = contribution ( 0 , ans ) ; return ans ; } public static void main ( String [ ] args ) { n = 5 ; for ( int i = 0 ; i < sz ; i ++ ) { tree [ i ] = new ArrayList ( ) ; } AddEdge ( 0 , 1 ) ; AddEdge ( 0 , 2 ) ; AddEdge ( 1 , 3 ) ; AddEdge ( 1 , 4 ) ; System . out . println ( getSum ( ) ) ; } }
Number of cells in a matrix that satisfy the given condition | Java implementation of the approach ; Function to return the number of cells in which mirror can be placed ; Update the row array where row [ i ] [ j ] will store whether the current row i contains all 1 s in the columns starting from j ; Update the column array where col [ i ] [ j ] will store whether the current column j contains all 1 s in the rows starting from i ; To store the required result ; For every cell except the last row and the last column ; If the current cell is not blocked and the light can travel from the next row and the next column then the current cell is valid ; For the last column ; For the last row , note that the last column is not taken into consideration as the bottom right element has already been considered in the last column previously ; Driver code
import java . util . * ; class GFG { static int N = 3 ; static int numberOfCells ( int mat [ ] [ ] ) { boolean [ ] [ ] row = new boolean [ N ] [ N ] ; boolean [ ] [ ] col = new boolean [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( mat [ i ] [ j ] == 1 ) { row [ i ] [ j ] = ( j + 1 < N ) ? row [ i ] [ j + 1 ] : true ; } else { row [ i ] [ j ] = false ; } } } for ( int j = 0 ; j < N ; j ++ ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( mat [ i ] [ j ] == 1 ) { col [ i ] [ j ] = ( i + 1 < N ) ? col [ i + 1 ] [ j ] : true ; } else { col [ i ] [ j ] = false ; } } } int cnt = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( row [ i ] [ j ] && col [ i ] [ j ] ) { cnt ++ ; } } } for ( int i = 0 ; i < N ; i ++ ) { if ( col [ i ] [ N - 1 ] ) cnt ++ ; } for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( row [ N - 1 ] [ j ] ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 1 , 1 } , { 0 , 1 , 1 } , { 0 , 1 , 1 } } ; System . out . print ( numberOfCells ( mat ) ) ; } }
Maximum sum subarray after altering the array | Java implementation of the approach ; Function to return the maximum subarray sum ; Function to reverse the subarray arr [ 0. . . i ] ; Function to return the maximum subarray sum after performing the given operation at most once ; To store the result ; When no operation is performed ; Find the maximum subarray sum after reversing the subarray arr [ 0. . . i ] for all possible values of i ; The complete array is reversed so that the subarray can be processed as arr [ 0. . . i ] instead of arr [ i ... N - 1 ] ; Driver code
import java . util . * ; class GFG { static int maxSumSubarray ( int [ ] arr , 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 + arr [ 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 void getUpdatedArray ( int [ ] arr , int [ ] copy , int i ) { for ( int j = 0 ; j <= ( i / 2 ) ; j ++ ) { copy [ j ] = arr [ i - j ] ; copy [ i - j ] = arr [ j ] ; } return ; } static int [ ] reverse ( int a [ ] ) { int i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } static int maxSum ( int [ ] arr , int size ) { int resSum = Integer . MIN_VALUE ; resSum = Math . max ( resSum , maxSumSubarray ( arr , size ) ) ; int [ ] copyArr = arr ; for ( int i = 1 ; i < size ; i ++ ) { getUpdatedArray ( arr , copyArr , i ) ; resSum = Math . max ( resSum , maxSumSubarray ( copyArr , size ) ) ; } arr = reverse ( arr ) ; copyArr = arr ; for ( int i = 1 ; i < size ; i ++ ) { getUpdatedArray ( arr , copyArr , i ) ; resSum = Math . max ( resSum , maxSumSubarray ( copyArr , size ) ) ; } resSum += 6 ; return resSum ; } public static void main ( String [ ] args ) { int [ ] arr = { - 9 , 21 , 24 , 24 , - 51 , - 6 , 17 , - 42 , - 39 , 33 } ; int size = arr . length ; System . out . print ( maxSum ( arr , size ) ) ; } }
Maximum possible GCD after replacing at most one element in the given array | Java implementation of the approach ; Function to return the maximum possible gcd after replacing a single element ; Prefix and Suffix arrays ; Single state dynamic programming relation for storing gcd of first i elements from the left in Prefix [ i ] ; Initializing Suffix array ; Single state dynamic programming relation for storing gcd of all the elements having index greater than or equal to i in Suffix [ i ] ; If first or last element of the array has to be replaced ; If any other element is replaced ; Return the maximized gcd ; Driver code
class GFG { static int MaxGCD ( int a [ ] , int n ) { int [ ] Prefix = new int [ n + 2 ] ; int [ ] Suffix = new int [ n + 2 ] ; Prefix [ 1 ] = a [ 0 ] ; for ( int i = 2 ; i <= n ; i += 1 ) { Prefix [ i ] = __gcd ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; } Suffix [ n ] = a [ n - 1 ] ; for ( int i = n - 1 ; i >= 1 ; i -= 1 ) { Suffix [ i ] = __gcd ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; } int ans = Math . max ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; for ( int i = 2 ; i < n ; i += 1 ) { ans = Math . max ( ans , __gcd ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; } return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a [ ] = { 6 , 7 , 8 } ; int n = a . length ; System . out . println ( MaxGCD ( a , n ) ) ; } }
Sum of products of all possible K size subsets of the given array | Java implementation of the approach ; Function to return the sum of products of all the possible k size subsets ; Initialising all the values to 0 ; To store the answer for current value of k ; For k = 1 , the answer will simply be the sum of all the elements ; Filling the table in bottom up manner ; To store the elements of the current row so that we will be able to use this sum for subsequent values of k ; We will subtract previously computed value so as to get the sum of elements from j + 1 to n in the ( i - 1 ) th row ; Driver code
import java . util . * ; class GFG { static int sumOfProduct ( int arr [ ] , int n , int k ) { int dp [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; int cur_sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ 1 ] [ i ] = arr [ i - 1 ] ; cur_sum += arr [ i - 1 ] ; } for ( int i = 2 ; i <= k ; i ++ ) { int temp_sum = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { cur_sum -= dp [ i - 1 ] [ j ] ; dp [ i ] [ j ] = arr [ j - 1 ] * cur_sum ; temp_sum += dp [ i ] [ j ] ; } cur_sum = temp_sum ; } return cur_sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int k = 2 ; System . out . print ( sumOfProduct ( arr , n , k ) ) ; } }
Find the equal pairs of subsequence of S and subsequence of T | Java implementation of the approach ; Function to return the pairs of subsequences from S [ ] and subsequences from T [ ] such that both have the same content ; Create dp array ; Base values ; Base values ; Keep previous dp value ; If both elements are same ; Return the required answer ; Driver code
import java . util . * ; class GFG { static int subsequence ( int [ ] S , int [ ] T , int n , int m ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; int mod = 1000000007 ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int j = 0 ; j <= m ; j ++ ) dp [ 0 ] [ j ] = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j <= m ; ++ j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ; if ( S [ i - 1 ] == T [ j - 1 ] ) dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] ; dp [ i ] [ j ] += mod ; dp [ i ] [ j ] %= mod ; } } return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int S [ ] = { 1 , 1 } ; int n = S . length ; int T [ ] = { 1 , 1 } ; int m = T . length ; System . out . println ( subsequence ( S , T , n , m ) ) ; } }
Maximum count of elements divisible on the left for any element | Java implementation of the approach ; Function to return the maximum count of required elements ; For every element in the array starting from the second element ; Check all the elements on the left of current element which are divisible by the current element ; Driver Code
class GFG { static int findMax ( int arr [ ] , int n ) { int res = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( j = 0 ; j < i ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) count += 1 ; } res = Math . max ( count , res ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . length ; System . out . println ( findMax ( arr , n ) ) ; } }
Maximum count of elements divisible on the left for any element | Java implementation of the approach ; Function to return the maximum count of required elements ; divisible [ i ] will store true if arr [ i ] is divisible by any element on its right ; To store the maximum required count ; For every element of the array ; If the current element is divisible by any element on its right ; Find the count of element on the left which are divisible by the current element ; If arr [ j ] is divisible then set divisible [ j ] to true ; Update the maximum required count ; Driver code
class GFG { static int findMax ( int arr [ ] , int n ) { boolean [ ] divisible = new boolean [ n ] ; int res = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( divisible [ i ] ) continue ; int cnt = 0 ; for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] % arr [ i ] ) == 0 ) { divisible [ j ] = true ; cnt ++ ; } } res = Math . max ( res , cnt ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . length ; System . out . println ( findMax ( arr , n ) ) ; } }
Number of subsets with sum divisible by M | Set 2 | Java implementation of the approach ; To store the states of DP ; Function to find the required count ; Base case ; If the state has been solved before return the value of the state ; Setting the state as solved ; Recurrence relation ; Driver code
class GFG { static int maxN = 20 ; static int maxM = 10 ; static int [ ] [ ] dp = new int [ maxN ] [ maxM ] ; static boolean [ ] [ ] v = new boolean [ maxN ] [ maxM ] ; static int findCnt ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { if ( curr == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = true ; return dp [ i ] [ curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr + arr [ i ] ) % m , n , m ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 3 , 3 } ; int n = arr . length ; int m = 6 ; System . out . println ( findCnt ( arr , 0 , 0 , n , m ) - 1 ) ; } }