codeparrot/xlcost-text-to-code huggingface dataset:
Length of largest common subarray in all the rows of given Matrix | Function to find longest common subarray in all the rows of the matrix ; Array to store the position of element in every row ; Traverse the matrix ; Store the position of every element in every row ; Variable to store length of largest common Subarray ; Traverse through the matrix column ; Variable to check if every row has arr [ i ] [ j ] next to arr [ i - 1 ] [ j ] or not ; Traverse through the matrix rows ; Check if arr [ i ] [ j ] is next to arr [ i ] [ j - 1 ] in every row or not ; If every row has arr [ 0 ] [ j ] next to arr [ 0 ] [ j - 1 ] increment len by 1 and update the value of ans ; Given Input ; Function Call
function largestCommonSubarray ( arr , n , m ) { let dp = Array ( n ) . fill ( ) . map ( ( ) => Array ( m + 1 ) ) ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { dp [ i ] [ arr [ i ] [ j ] ] = j ; } } let ans = 1 ; let len = 1 ; for ( let i = 1 ; i < m ; i ++ ) { let check = true ; for ( let j = 1 ; j < n ; j ++ ) { if ( dp [ j ] [ arr [ 0 ] [ i - 1 ] ] + 1 != dp [ j ] [ arr [ 0 ] [ i ] ] ) { check = false ; break ; } } if ( check ) { len ++ ; ans = Math . max ( ans , len ) ; } else { len = 1 ; } } return ans ; } let n = 4 ; let m = 5 ; let arr = [ [ 4 , 5 , 1 , 2 , 3 , 6 , 7 ] , [ 1 , 2 , 4 , 5 , 7 , 6 , 3 ] , [ 2 , 7 , 3 , 4 , 5 , 1 , 6 ] ] ; let N = arr . length ; let M = arr [ 0 ] . length ; document . write ( largestCommonSubarray ( arr , N , M ) ) ;
Maximize sum that can be obtained from two given arrays based on given conditions | Function to find the maximum sum that can be obtained from two given based on the following conditions ; Stores the maximum sum from 0 to i ; Initialize the value of dp [ 0 ] [ 0 ] and dp [ 0 ] [ 1 ] ; Traverse the array A [ ] and B [ ] ; If A [ i ] is considered ; If B [ i ] is not considered ; If B [ i ] is considered ; If i = 1 , then consider the value of dp [ i ] [ 1 ] as b [ i ] ; Return maximum Sum ; Given Input ; Function Call
function MaximumSum ( a , b , n ) { let dp = new Array ( n ) . fill ( 0 ) . map ( ( ) => new Array ( 2 ) ) ; dp [ 0 ] [ 0 ] = a [ 0 ] ; dp [ 0 ] [ 1 ] = b [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + a [ i ] ; dp [ i ] [ 1 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; if ( i - 2 >= 0 ) { dp [ i ] [ 1 ] = Math . max ( dp [ i ] [ 1 ] , Math . max ( dp [ i - 2 ] [ 0 ] , dp [ i - 2 ] [ 1 ] ) + b [ i ] ) ; } else { dp [ i ] [ 1 ] = Math . max ( dp [ i ] [ 1 ] , b [ i ] ) ; } } return Math . max ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) ; } let A = [ 10 , 1 , 10 , 10 ] ; let B = [ 5 , 50 , 1 , 5 ] ; let N = A . length ; document . write ( MaximumSum ( A , B , N ) ) ;
Maximum Tip Calculator | Function that finds the maximum tips from the given arrays as per the given conditions ; Base Condition ; If both have non - zero count then return max element from both array ; Traverse first array , as y count has become 0 ; Traverse 2 nd array , as x count has become 0 ; Drive Code ; Function Call
function maximumTip ( arr1 , arr2 , n , x , y ) { if ( n == 0 ) return 0 ; if ( x != 0 && y != 0 ) return Math . max ( arr1 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x - 1 , y ) , arr2 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x , y - 1 ) ) ; if ( y == 0 ) return arr1 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x - 1 , y ) ; else return arr2 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x , y - 1 ) ; } let N = 5 ; let X = 3 ; let Y = 3 ; let A = [ 1 , 2 , 3 , 4 , 5 ] ; let B = [ 5 , 4 , 3 , 2 , 1 ] ; document . write ( maximumTip ( A , B , N , X , Y ) ) ;
Number of ways such that only K bars are visible from the left | Function to calculate the number of permutations of N , where only K bars are visible from the left . ; Only ascending order is possible ; N is placed at the first position The nest N - 1 are arranged in ( N - 1 ) ! ways ; Recursing ; Input ; Function call
function KvisibleFromLeft ( N , K ) { if ( N == K ) return 1 ; if ( K == 1 ) { let ans = 1 ; for ( let i = 1 ; i < N ; i ++ ) ans *= i ; return ans ; } return KvisibleFromLeft ( N - 1 , K - 1 ) + ( N - 1 ) * KvisibleFromLeft ( N - 1 , K ) ; } let N = 5 , K = 2 ; document . write ( KvisibleFromLeft ( N , K ) + " " ) ;
Minimum deletions in Array to make difference of adjacent elements non | Function for finding minimum deletions so that the array becomes non decreasing and the difference between adjacent elements also becomes non decreasing ; initialize answer to a large value ; generating all subsets ; checking the first condition ; checking the second condition ; if both conditions are satisfied consider the answer for minimum ; Input ; Function call
function minimumDeletions ( A , N ) { let ans = Number . MAX_SAFE_INTEGER ; for ( let i = 1 ; i < ( 1 << N ) ; i ++ ) { let temp = [ ] ; for ( let j = 0 ; j < N ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) { temp . push ( A [ j ] ) ; } } let flag = 0 ; for ( let j = 1 ; j < temp . length ; j ++ ) if ( temp [ j ] < temp [ j - 1 ] ) flag = 1 ; for ( let j = 1 ; j < temp . length - 1 ; j ++ ) if ( temp [ j ] - temp [ j - 1 ] > temp [ j + 1 ] - temp [ j ] ) flag = 1 ; if ( flag == 0 ) { ans = Math . min ( ans , N - temp . length ) ; } } return ans ; } let A = [ 1 , 4 , 5 , 7 , 20 , 21 ] ; let N = A . length ; document . write ( minimumDeletions ( A , N ) + " " ) ;
Minimum deletions in Array to make difference of adjacent elements non | the maximum value of A [ i ] ; Function for finding minimum deletions so that the array becomes non - decreasing and the difference between adjacent elements is also non - decreasing ; initialize the dp table and set all values to 0 pref [ i ] [ j ] will contain min ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] , ... dp [ i ] [ j ] ) ; find the maximum sized valid set possible and then subtract its size from N to get minimum number of deletions ; when selecting only the current element and deleting all elements from 0 to i - 1 inclusive ; if this is true , moving from index j to i is possible ; we can get min ( dp [ j ] [ 0 ] , . . dp [ j ] ) from pref array ; ; construct the prefix array for this element ; take the max set size from dp [ N - 1 ] [ 0 ] to dp [ N - 1 ] [ MAX ] ; Driver Code ; Function call
var MAX = 100001 ; function minimumDeletions ( A , N ) { var dp = new Array ( N , MAX ) ; var pref = new Array ( N , MAX ) ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = 0 ; pref [ i , j ] = 0 ; } } for ( var i = 0 ; i < N ; i ++ ) { dp [ i , 0 ] = 1 ; for ( var j = i - 1 ; j >= 0 ; j -- ) { if ( A [ i ] >= A [ j ] ) { var diff = A [ i ] - A [ j ] ; dp [ i ] = Math . max ( dp [ i , diff ] , pref [ j , diff ] + 1 ) ; } } pref [ i , 0 ] = dp [ i , 0 ] ; for ( var j = 1 ; j < MAX ; j ++ ) pref [ i , j ] = Math . max ( dp [ i , j ] , pref [ i , j - 1 ] ) ; } var maxSetSize = - 1 ; for ( var i = 0 ; i < MAX ; i ++ ) maxSetSize = Math . max ( maxSetSize , dp [ N - 1 , i ] ) ; return N - maxSetSize ; } var A = [ 1 , 4 , 5 , 7 , 20 , 21 ] ; var N = A . length ; document . write ( minimumDeletions ( A , N ) ) ;
Find maximum subset | Function to calculate maximum sum possible by taking at most K elements that is divisibly by D ; variable to store final answer ; Traverse all subsets ; Update ans if necessary conditions are satisfied ; Input ; Function call
function maximumSum ( A , N , K , D ) { let ans = 0 ; for ( let i = 0 ; i < ( 1 << N ) ; i ++ ) { let sum = 0 ; let c = 0 ; for ( let j = 0 ; j < N ; j ++ ) { if ( i >> j & 1 ) { sum += A [ j ] ; c ++ ; } } if ( sum % D == 0 && c <= K ) ans = Math . max ( ans , sum ) ; } return ans ; } let N = 5 , K = 3 , D = 7 ; let A = [ 1 , 11 , 5 , 5 , 18 ] ; document . write ( maximumSum ( A , N , K , D ) + " " ) ;
Find the longest subsequence of a string that is a substring of another string | Function to find the longest subsequence that matches with the substring of other string ; Stores the lengths of strings X and Y ; Create a matrix ; Initialize the matrix ; Fill all the remaining rows ; If the characters are equal ; If not equal , then just move to next in subsequence string ; Find maximum length of the longest subsequence matching substring of other string ; Iterate through the last row of matrix ; Store the required string ; Backtrack from the cell ; If equal , then add the character to res string ; Return the required string ; Driver Code
function longestSubsequence ( X , Y ) { let n = X . length ; let m = Y . length ; let mat = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { mat [ i ] = new Array ( m + 1 ) ; for ( let j = 0 ; j < m + 1 ; j ++ ) { if ( i == 0 j == 0 ) mat [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i < n + 1 ; i ++ ) { for ( let j = 1 ; j < m + 1 ; j ++ ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { mat [ i ] [ j ] = 1 + mat [ i - 1 ] [ j - 1 ] ; } else { mat [ i ] [ j ] = mat [ i - 1 ] [ j ] ; } } } let len = 0 , col = 0 ; for ( let i = 0 ; i < m + 1 ; i ++ ) { if ( mat [ n ] [ i ] > len ) { len = mat [ n ] [ i ] ; col = i ; } } let res = " " ; let i = n ; let j = col ; while ( len > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { res = X [ i - 1 ] + res ; i -- ; j -- ; len -- ; } else { i -- ; } } return res ; } let X = " " ; let Y = " " ; document . write ( longestSubsequence ( X , Y ) ) ;
Count unordered pairs of equal elements for all subarrays | Function to count all pairs ( i , j ) such that arr [ i ] equals arr [ j ] in all possible subarrays of the array ; Stores the size of the array ; Stores the positions of all the distinct elements ; Append index corresponding to arr [ i ] in the map ; Traverse the map M ; Traverse the array ; Update the value of ans ; Update the value of the sum ; Print the value of ans ; Driver Code
function countPairs ( arr ) { let N = arr . length ; let ans = 0 ; let M = new Map ( ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( M . has ( arr [ i ] ) ) { M . get ( arr [ i ] ) . push ( i ) ; } else { M . set ( arr [ i ] , [ i ] ) } } for ( let it of M ) { let v = it [ 1 ] ; let sum = 0 ; for ( let j = 0 ; j < v . length ; j ++ ) { ans += sum * ( N - v [ j ] ) ; sum += v [ j ] + 1 ; } } document . write ( ans ) ; } let arr = [ 1 , 2 , 1 , 1 ] ; countPairs ( arr ) ;
Number of alternating substrings from a given Binary String | Function to count number of alternating substrings from a given binary string ; Initialize dp array , where dp [ i ] [ j ] stores the number of alternating strings starts with i and having j elements . ; Traverse the string from the end ; If i is equal to N - 1 ; Otherwise , ; Increment count of substrings starting at i and has 0 in the beginning ; Increment count of substrings starting at i and has 1 in the beginning ; Stores the result ; Iterate in the range [ 0 , N - 1 ] ; Update ans ; Return the ans ; Given Input ; Function call
function countAlternatingSubstrings ( S , N ) { var dp = new Array ( 2 ) ; dp [ 0 ] = Array ( N ) . fill ( 0 ) ; dp [ 1 ] = Array ( N ) . fill ( 0 ) ; var i ; for ( i = N - 1 ; i >= 0 ; i -- ) { if ( i == N - 1 ) { if ( S [ i ] == ' ' ) dp [ 1 ] [ i ] = 1 ; else dp [ 0 ] [ i ] = 1 ; } else { if ( S [ i ] == ' ' ) dp [ 0 ] [ i ] = 1 + dp [ 1 ] [ i + 1 ] ; else dp [ 1 ] [ i ] = 1 + dp [ 0 ] [ i + 1 ] ; } } var ans = 0 ; for ( i = 0 ; i < N ; i ++ ) { ans += Math . max ( dp [ 0 ] [ i ] , dp [ 1 ] [ i ] ) ; } return ans ; } var S = " " ; var N = S . length ; document . write ( countAlternatingSubstrings ( S , N ) ) ;
Count ways to place ' + ' and ' | Function to count number of ways ' + ' and ' - ' operators can be placed in front of array elements to make the sum of array elements equal to K ; Stores sum of the array ; Stores count of 0 s in A [ ] ; Traverse the array ; Update sum ; Update count of 0 s ; Conditions where no arrangements are possible which adds up to K ; Required sum ; Dp array ; Base cases ; Fill the dp array ; Return answer ; Input ; Function call
function solve ( A , N , K ) { let sum = 0 ; let c = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( A [ i ] == 0 ) c ++ ; } if ( K > sum || ( sum + K ) % 2 ) return 0 ; sum = ( sum + K ) / 2 ; let dp = new Array ( N + 1 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( sum + 1 ) ; } for ( let i = 0 ; i <= sum ; i ++ ) dp [ 0 ] [ i ] = 0 ; for ( let i = 0 ; i <= N ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= sum ; j ++ ) { if ( A [ i - 1 ] <= j && A [ i - 1 ] ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - A [ i - 1 ] ] ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } return dp [ N ] [ sum ] + Math . pow ( 2 , c ) ; } let A = [ 1 , 1 , 2 , 3 ] ; let N = A . length ; let K = 3 ; document . write ( solve ( A , N , K ) ) ;
Rearrange an array to maximize sum of Bitwise AND of same | Function to implement recursive DP ; If i is equal to N ; If dp [ i ] [ mask ] is not equal to - 1 ; Iterate over the array B [ ] ; If current element is not yet selected ; Update dp [ i ] [ mask ] ; Return dp [ i ] [ mask ] ; Function to obtain maximum sum of Bitwise AND of same - indexed elements from the arrays A [ ] and B [ ] ; Stores all dp - states ; Returns the maximum value returned by the function maximizeAnd ( ) ; Driver Code
function maximizeAnd ( i , mask , A , B , N , dp ) { if ( i == N ) return 0 ; if ( dp [ i ] [ mask ] != - 1 ) return dp [ i ] [ mask ] ; for ( var j = 0 ; j < N ; ++ j ) { if ( ! ( mask & ( 1 << j ) ) ) { dp [ i ] [ mask ] = Math . max ( dp [ i ] [ mask ] , ( A [ i ] & B [ j ] ) + maximizeAnd ( i + 1 , mask | ( 1 << j ) , A , B , N , dp ) ) ; } } return dp [ i ] [ mask ] ; } function maximizeAndUtil ( A , B , N ) { var dp = Array . from ( Array ( N ) , ( ) => Array ( 1 << N + 1 ) . fill ( - 1 ) ) ; return maximizeAnd ( 0 , 0 , A , B , N , dp ) ; } var A = [ 3 , 5 , 7 , 11 ] ; var B = [ 2 , 6 , 10 , 12 ] ; var N = A . length document . write ( maximizeAndUtil ( A , B , N ) ) ;
Count ways to reach the Nth station | Javascript program for the above approach ; Function to find the number of ways to reach Nth station ; Declares the DP [ ] array ; Initialize dp [ ] [ ] array ; Only 1 way to reach station 1 ; Find the remaining states from the 2 nd station ; If the train A is present at station i - 1 ; If the train B is present at station i - 2 ; If train C is present at station i - 3 ; The total number of ways to reach station i ; Return the total count of ways ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int numberOfWays ( int N ) { int DP [ ] [ ] = new int [ N + 1 ] [ 5 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { for ( int j = 0 ; j < 5 ; j ++ ) { DP [ i ] [ j ] = 0 ; } } DP [ 1 ] [ 1 ] = 1 ; DP [ 1 ] [ 2 ] = 1 ; DP [ 1 ] [ 3 ] = 1 ; DP [ 1 ] [ 4 ] = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( i - 1 > 0 && DP [ i - 1 ] [ 1 ] > 0 ) DP [ i ] [ 1 ] = DP [ i - 1 ] [ 4 ] ; if ( i - 2 > 0 && DP [ i - 2 ] [ 2 ] > 0 ) DP [ i ] [ 2 ] = DP [ i - 2 ] [ 4 ] ; if ( i - 3 > 0 && DP [ i - 3 ] [ 3 ] > 0 ) DP [ i ] [ 3 ] = DP [ i - 3 ] [ 4 ] ; DP [ i ] [ 4 ] = ( DP [ i ] [ 1 ] + DP [ i ] [ 2 ] + DP [ i ] [ 3 ] ) ; } return DP [ N ] [ 4 ] ; } public static void main ( String [ ] args ) { int N = 15 ; System . out . print ( numberOfWays ( N ) ) ; } }
Number of M | Function to find the number of M - length sorted arrays possible using numbers from the range [ 1 , N ] ; If size becomes equal to m , that means an array is found ; Include current element , increase size by 1 and remain on the same element as it can be included again ; Exclude current element ; Return the sum obtained in both the cases ; Given Input ; Function Call
function countSortedArrays ( start , m , size , n ) { if ( size === m ) return 1 ; if ( start > n ) return 0 ; var notTaken = 0 , taken = 0 ; taken = countSortedArrays ( start , m , size + 1 , n ) ; notTaken = countSortedArrays ( start + 1 , m , size , n ) ; return taken + notTaken ; } var n = 2 , m = 3 ; document . write ( countSortedArrays ( 1 , m , 0 , n ) ) ;
Maximum subarray sum possible after removing at most one subarray | Function to find maximum subarray sum possible after removing at most one subarray ; Calculate the preSum [ ] array ; Update the value of sum ; Update the value of maxSum ; Update the value of preSum [ i ] ; Calculate the postSum [ ] array ; Update the value of sum ; Update the value of maxSum ; Update the value of postSum [ i ] ; Stores the resultant maximum sum of subarray ; Update the value of ans ; Print the resultant sum ; Driver Code
function maximumSum ( arr , n ) { let preSum = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let sum = 0 ; let maxSum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = Math . max ( arr [ i ] , sum + arr [ i ] ) ; maxSum = Math . max ( maxSum , sum ) ; preSum [ i ] = maxSum ; } sum = 0 ; maxSum = 0 ; let postSum = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = n - 1 ; i >= 0 ; i -- ) { sum = Math . max ( arr [ i ] , sum + arr [ i ] ) ; maxSum = Math . max ( maxSum , sum ) ; postSum [ i ] = maxSum ; } let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , preSum [ i ] + postSum [ i + 1 ] ) ; } document . write ( ans ) ; } let arr = [ 7 , 6 , - 1 , - 4 , - 5 , 7 , 1 ] ; maximumSum ( arr , arr . length ) ;
Count all unique outcomes possible by performing S flips on N coins | Function to recursively count the number of unique outcomes possible S flips are performed on N coins ; Base Cases ; Recursive Calls ; Driver Code
function numberOfUniqueOutcomes ( N , S ) { if ( S < N ) return 0 ; if ( N == 1 N == S ) return 1 ; return ( numberOfUniqueOutcomes ( N - 1 , S - 1 ) + numberOfUniqueOutcomes ( N - 1 , S - 2 ) ) ; } let N = 3 , S = 4 ; document . write ( numberOfUniqueOutcomes ( N , S ) )
Length of longest increasing subsequence in a string | Function to find length of longest increasing subsequence in a string ; Stores at every i - th index , the length of the longest increasing subsequence ending with character i ; Size of string ; Stores the length of LIS ; Iterate over each character of the string ; Store position of the current character ; Stores the length of LIS ending with current character ; Check for all characters less then current character ; Include current character ; Update length of longest increasing subsequence ; Updating LIS for current character ; Return the length of LIS ; Driver Code
function lisOtimised ( s ) { let dp = new Array ( 30 ) . fill ( 0 ) ; let N = s . length ; let lis = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { let val = s . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ; let curr = 0 ; for ( let j = 0 ; j < val ; j ++ ) { curr = Math . max ( curr , dp [ j ] ) ; } curr ++ ; lis = Math . max ( lis , curr ) ; dp [ val ] = Math . max ( dp [ val ] , curr ) ; } return lis ; } let s = " " ; document . write ( lisOtimised ( s ) ) ;
Sum of length of two smallest subsets possible from a given array with sum at least K | javascript program for the above approach ; Function to calculate sum of lengths of two smallest subsets with sum >= K ; Sort the array in ascending order ; Stores suffix sum of the array ; Update the suffix sum array ; Stores all dp - states ; Initialize all dp - states with a max1imum possible value ; Base Case ; Traverse the array arr [ ] ; Iterate over the range [ 0 , K ] ; If A [ i ] is equal to at least the required sum j for the current state ; If the next possible state doesn 't exist ; Otherwise , update the current state to the minimum of the next state and state including the current element A [ i ] ; Traverse the suffix sum array ; If suffix [ i ] - dp [ i ] [ K ] >= K ; Sum of lengths of the two smallest subsets is obtained ; Return - 1 , if there doesn 't  exist any subset of sum >= K ; Driver Code
var max1 = 1000000000 ; function MinimumLength ( A , N , K ) { 0 A . sort ( ) ; var suffix = Array ( N + 1 ) . fill ( 0 ) ; var i ; for ( i = N - 1 ; i >= 0 ; i -- ) suffix [ i ] = suffix [ i + 1 ] + A [ i ] ; var dp = new Array ( N + 1 ) ; for ( i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = new Array ( K + 1 ) ; var j ; for ( i = 0 ; i <= N ; i ++ ) { for ( j = 0 ; j <= K ; j ++ ) { dp [ i ] [ j ] = max1 ; } } ; dp [ N ] [ 0 ] = 0 ; for ( i = N - 1 ; i >= 0 ; i -- ) { for ( j = K ; j >= 0 ; j -- ) { if ( j <= A [ i ] ) { dp [ i ] [ j ] = A [ i ] ; continue ; } if ( dp [ i + 1 ] [ j - A [ i ] ] == max1 ) dp [ i ] [ j ] = max1 ; else dp [ i ] [ j ] = Math . min ( dp [ i + 1 ] [ j ] , dp [ i + 1 ] [ j - A [ i ] ] + A [ i ] ) ; } } for ( i = N - 1 ; i >= 0 ; i -- ) { if ( suffix [ i ] - dp [ i ] [ K ] >= K ) { return N - i ; } } return - 1 ; } var arr = [ 7 , 4 , 5 , 6 , 8 ] ; var K = 13 ; var N = arr . length ; document . write ( MinimumLength ( arr , N , K ) ) ;
Count distinct possible Bitwise XOR values of subsets of an array | Javascript program for the above approach ; Stores the mask of the vector ; Stores the current size of dp [ ] ; Function to store the mask of given integer ; Iterate over the range [ 0 , 20 ] ; If i - th bit 0 ; If dp [ i ] is zero ; Store the position in dp ; Increment the answer ; Return from the loop ; mask = mask XOR dp [ i ] ; Function to find the size of the set having Bitwise XOR of all the subset of the given array ; Traverse the array ; Print the answer ; Driver Code ; Function Call
let size = 20 ; var dp = new Array ( size ) . fill ( 0 ) ; let ans = 0 ; function insertVector ( mask ) { for ( let i = 0 ; i < 20 ; i ++ ) { if ( ( mask & 1 << i ) == 0 ) continue ; if ( dp [ i ] == 0 ) { dp [ i ] = mask ; ++ ans ; return ; } mask ^= dp [ i ] ; } } function maxSizeSet ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { insertVector ( arr [ i ] ) ; } document . write ( 1 << ans ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; maxSizeSet ( arr , N ) ;
Maximize sum possible from an array by jumps of length i + K * arr [ i ] from any ith index | Function to find the maximum sum possible by jumps of length i + K * arr [ i ] from any i - th index ; Initialize an array dp [ ] ; Stores the maximum sum ; Iterate over the range [ N - 1 , 0 ] ; If length of the jump exceeds N ; Set dp [ i ] as arr [ i ] ; Otherwise , update dp [ i ] as sum of dp [ i + K * arr [ i ] ] and arr [ i ] ; Update the overall maximum sum ; Print the answer ; Driver Code
function maxSum ( arr , N , K ) { let dp = [ ] ; for ( let i = 0 ; i < N + 2 ; i ++ ) { dp [ i ] = 0 ; } let maxval = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( ( i + K * arr [ i ] ) >= N ) { dp [ i ] = arr [ i ] ; } else { dp [ i ] = dp [ i + K * arr [ i ] ] + arr [ i ] ; } maxval = Math . max ( maxval , dp [ i ] ) ; } document . write ( maxval ) ; } let arr = [ 2 , 1 , 3 , 1 , 2 ] ; let N = arr . length ; let K = 3 ; maxSum ( arr , N , K ) ;
Maximum sum submatrix | Function to find maximum sum submatrix ; Stores the number of rows and columns in the matrix ; Stores maximum submatrix sum ; Take each row as starting row ; Take each column as the starting column ; Take each row as the ending row ; Take each column as the ending column ; Stores the sum of submatrix having topleft index ( i , j ) and bottom right index ( k , l ) ; Iterate the submatrix row - wise and calculate its sum ; Update the maximum sum ; Print the answer ; Driver Code
function maxSubmatrixSum ( matrix ) { var r = matrix . length ; var c = matrix [ 0 ] . length ; var maxSubmatrix = 0 ; for ( i = 0 ; i < r ; i ++ ) { for ( j = 0 ; j < c ; j ++ ) { for ( k = i ; k < r ; k ++ ) { for ( l = j ; l < c ; l ++ ) { var sumSubmatrix = 0 ; for ( m = i ; m <= k ; m ++ ) { for ( n = j ; n <= l ; n ++ ) { sumSubmatrix += matrix [ m ] [ n ] ; } } maxSubmatrix = Math . max ( maxSubmatrix , sumSubmatrix ) ; } } } } document . write ( maxSubmatrix ) ; } var matrix = [ [ 0 , - 2 , - 7 , 0 ] , [ 9 , 2 , - 6 , 2 ] , [ - 4 , 1 , - 4 , 1 ] , [ - 1 , 8 , 0 , - 2 ] ] ; maxSubmatrixSum ( matrix ) ;