codeparrot/xlcost-text-to-code huggingface dataset:
Smallest subarray with sum greater than a given value | Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize current sum and minimum length ; Initialize starting and ending indexes ; Keep adding array elements while current sum is smaller than or equal to x ; If current sum becomes greater than x . ; Update minimum length if needed ; remove starting elements ; Driver program to test above function
function smallestSubWithSum ( arr , n , x ) { let curr_sum = 0 , min_len = n + 1 ; let start = 0 , end = 0 ; while ( end < n ) { while ( curr_sum <= x && end < n ) curr_sum += arr [ end ++ ] ; while ( curr_sum > x && start < n ) { if ( end - start < min_len ) min_len = end - start ; curr_sum -= arr [ start ++ ] ; } } return min_len ; } let arr1 = [ 1 , 4 , 45 , 6 , 10 , 19 ] ; let x = 51 ; let n1 = arr1 . length ; let res1 = smallestSubWithSum ( arr1 , n1 , x ) ; ( res1 == n1 + 1 ) ? document . write ( " " ) : document . write ( res1 + " " ) ; let arr2 = [ 1 , 10 , 5 , 2 , 7 ] ; let n2 = arr2 . length ; x = 9 ; let res2 = smallestSubWithSum ( arr2 , n2 , x ) ; ( res2 == n2 + 1 ) ? document . write ( " " ) : document . write ( res2 + " " ) ; let arr3 = [ 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ] ; let n3 = arr3 . length ; x = 280 ; let res3 = smallestSubWithSum ( arr3 , n3 , x ) ; ( res3 == n3 + 1 ) ? document . write ( " " ) : document . write ( res3 + " " ) ;
Count L | Fumction to find the number of arrays of length L such that each element divides the next element ; Stores the number of sequences of length i that ends with j ; Base Case ; Iterate over the range [ 0 , l ] ; Iterate for all multiples of j ; Incrementing dp [ i + 1 ] [ k ] by dp [ i ] [ j ] as the next element is multiple of j ; Stores the number of arrays ; Add all array of length L that ends with i ; Return the resultant count ; Driver Code
function numberOfArrays ( n , l ) { let dp = Array ( l + 1 ) . fill ( ) . map ( ( ) => Array ( n + 1 ) . fill ( 0 ) ) ; dp [ 0 ] [ 1 ] = 1 ; for ( let i = 0 ; i < l ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { for ( let k = j ; k <= n ; k += j ) { dp [ i + 1 ] [ k ] += dp [ i ] [ j ] ; } } } let ans = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { ans += dp [ l ] [ i ] ; } return ans ; } let N = 2 , L = 4 ; document . write ( numberOfArrays ( N , L ) ) ;
Count minimum steps to get the given desired array | JavaScript program to count minimum number of operations to get the given arr array ; Returns count of minimum operations to convert a zero array to arr array with increment and doubling operations . This function computes count by doing reverse steps , i . e . , convert arr to zero array . ; Initialize result ( Count of minimum moves ) ; Keep looping while all elements of arr don 't become 0. ; To store count of zeroes in current arr array ; To find first odd element ; If odd number found ; If 0 , then increment zero_count ; All numbers are 0 ; All numbers are even ; Divide the whole array by 2 and increment result ; Make all odd numbers even by subtracting one and increment result . ; Driver Code
let arr = [ 16 , 16 , 16 ] ; function countMinOperations ( n ) { let result = 0 ; while ( true ) { let zero_count = 0 ; let i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) break ; else if ( arr [ i ] == 0 ) zero_count ++ ; } if ( zero_count == n ) return result ; if ( i == n ) { for ( let j = 0 ; j < n ; j ++ ) arr [ j ] = arr [ j ] / 2 ; result ++ ; } for ( let j = i ; j < n ; j ++ ) { if ( arr [ j ] % 2 == 1 ) { arr [ j ] -- ; result ++ ; } } } } document . write ( " " + " " + " " + countMinOperations ( arr . length ) ) ;
Maximize sum of odd | Function to find the maximum sum of array elements chosen by Player A according to the given criteria ; Store the key ; Corner Case ; Check if all the elements can be taken or not ; If the difference is less than or equal to the available chances then pick all numbers ; Find the sum of array elements over the range [ start , N ] ; If yes then return that value ; Traverse over the range [ 1 , 2 * M ] ; Sum of elements for Player A ; Even chance sum can be obtained by subtracting the odd chances sum - total and picking up the maximum from that ; Storing the value in dictionary ; Return the maximum sum of odd chances ; Driver code ; Stores the precomputed values ; Function Call
function recursiveChoosing ( arr , start , M , dp ) { let key = [ start , M ] ; if ( start >= N ) { return 0 ; } if ( N - start <= 2 * M ) { let sum = 0 ; for ( let i = start ; i < arr . length ; i ++ ) { sum = sum + arr [ i ] ; } return sum ; } let psa = 0 ; let sum = 0 ; for ( let i = start ; i < arr . length ; i ++ ) { sum = sum + arr [ i ] ; } let total = sum ; if ( dp . has ( key ) ) { return dp [ key ] ; } for ( let x = 1 ; x < 2 * M + 1 ; x ++ ) { let psb = recursiveChoosing ( arr , start + x , Math . max ( x , M ) , dp ) psa = Math . max ( psa , total - psb ) ; } dp [ key ] = psa ; return dp [ key ] ; } let arr = [ 2 , 7 , 9 , 4 , 4 ] ; let N = arr . length ; let dp = new Map ( ) ; document . write ( recursiveChoosing ( arr , 0 , 1 , dp ) )
Maximum Sum Subsequence made up of consecutive elements of different parity | JavaScript program for the above approach ; Function to find the maximum sum of subsequence with consecutive terms having different parity ; Base Case ; Store the parity of number at the ith position ; If the dp state has already been calculated , return it ; If the array is traversed and no element has been selected yet then select the current element ; If the parity of the current and previously selected element are different , then select the current element ; Skip the current element and move to the next element ; Return the result ; Function to calculate the maximum sum subsequence with consecutive terms having different parity ; Initially the prev value is set to say 2 , as the first element can anyways be selected ; Driver Code
let dp = new Array ( 100 ) . fill ( 0 ) . map ( ( ) => new Array ( 3 ) . fill ( - 1 ) ) ; function maxSum ( arr , i , n , prev , is_selected ) { if ( i == n ) { return 0 ; } let cur = Math . abs ( arr [ i ] ) % 2 ; if ( dp [ i ] [ prev ] != - 1 ) { return dp [ i ] [ prev ] ; } if ( i == n - 1 && is_selected == 0 ) return dp [ i ] [ prev ] = arr [ i ] ; if ( cur != prev ) { dp [ i ] [ prev ] = arr [ i ] + maxSum ( arr , i + 1 , n , cur , 1 ) ; } dp [ i ] [ prev ] = Math . max ( dp [ i ] [ prev ] , maxSum ( arr , i + 1 , n , prev , is_selected ) ) ; return dp [ i ] [ prev ] ; } function maxSumUtil ( arr , n ) { document . write ( maxSum ( arr , 0 , n , 2 , 0 ) ) ; } let arr = [ 1 , 2 , 6 , 8 , - 5 , 10 ] ; let N = arr . length maxSumUtil ( arr , N ) ;
Find minimum number of merge operations to make an array palindrome | Returns minimum number of count operations required to make arr [ ] palindrome ; Initialize result ; Start from two corners ; If corner elements are same , problem reduces arr [ i + 1. . j - 1 ] ; If left element is greater , then we merge right two elements ; need to merge from tail . ; Else we merge left two elements ; Driver Code
function findMinOps ( arr , n ) { let ans = 0 ; for ( let i = 0 , j = n - 1 ; i <= j ; ) { if ( arr [ i ] == arr [ j ] ) { i ++ ; j -- ; } else if ( arr [ i ] > arr [ j ] ) { j -- ; arr [ j ] += arr [ j + 1 ] ; ans ++ ; } else { i ++ ; arr [ i ] += arr [ i - 1 ] ; ans ++ ; } } return ans ; } let arr = [ 1 , 4 , 5 , 9 , 1 ] ; document . write ( " " + findMinOps ( arr , arr . length ) ) ;
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array | Returns the smallest number that cannot be represented as sum of subset of elements from set represented by sorted array arr [ 0. . n - 1 ] ; Initialize result ; Traverse the array and increment ' res ' if arr [ i ] is smaller than or equal to ' res ' . ; Driver program to test above functions
function findSmallest ( arr , n ) { var res = 1 ; for ( i = 0 ; i < n && arr [ i ] <= res ; i ++ ) res = res + arr [ i ] ; return res ; } var arr1 = [ 1 , 3 , 4 , 5 ] ; var n1 = arr1 . length ; document . write ( findSmallest ( arr1 , n1 ) + " " ) ; var arr2 = [ 1 , 2 , 6 , 10 , 11 , 15 ] ; var n2 = arr2 . length ; document . write ( findSmallest ( arr2 , n2 ) + " " ) ; var arr3 = [ 1 , 1 , 1 , 1 ] ; var n3 = arr3 . length ; document . write ( findSmallest ( arr3 , n3 ) + " " ) ; var arr4 = [ 1 , 1 , 3 , 4 ] ; var n4 = arr4 . length ; document . write ( findSmallest ( arr4 , n4 ) + " " ) ;
Count distinct possible Bitwise XOR values of subsets of an array | Stores the Bitwise XOR of every possible subset ; Function to generate all combinations of subsets and store their Bitwise XOR in set S ; If the end of the subset is reached ; Stores the Bitwise XOR of the current subset ; Iterate comb [ ] to find XOR ; Insert the Bitwise XOR of R elements ; Otherwise , iterate to generate all possible subsets ; Recursive call for next index ; Function to find the size of the set having Bitwise XOR of all the subsets of the given array ; Iterate ove the given array ; Generate all possible subsets ; Print the size of the set ; Driver Code ; Function Call
let s ; function countXOR ( arr , comb , start , end , index , r ) { if ( index == r ) { let new_xor = 0 ; for ( let j = 0 ; j < r ; j ++ ) { new_xor ^= comb [ j ] ; } s . add ( new_xor ) ; return ; } for ( let i = start ; i <= end && end - i + 1 >= r - index ; i ++ ) { comb [ index ] = arr [ i ] ; countXOR ( arr , comb , i + 1 , end , index + 1 , r ) ; } } function maxSizeSet ( arr , N ) { for ( let r = 1 ; r <= N ; r ++ ) { let comb = new Array ( r + 1 ) ; countXOR ( arr , comb , 0 , N - 1 , 0 , r ) ; } document . write ( s . size ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; s = new Set ( ) ; maxSizeSet ( arr , N ) ;
Size of The Subarray With Maximum Sum | Function to find maximum subarray sum ; Driver code
function maxSubArraySum ( a , size ) { let max_so_far = Number . MIN_VALUE , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ; for ( let i = 0 ; i < size ; i ++ ) { max_ending_here += a [ i ] ; if ( max_so_far < max_ending_here ) { max_so_far = max_ending_here ; start = s ; end = i ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; s = i + 1 ; } } return ( end - start + 1 ) ; } let a = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] ; let n = a . length ; document . write ( maxSubArraySum ( a , n ) ) ;
Longest subarray of an array which is a subsequence in another array | Function to find the length of the longest subarray in arr1 [ ] which is a subsequence in arr2 [ ] ; Length of the array arr1 [ ] ; Length of the array arr2 [ ] ; Length of the required longest subarray ; Initialize DP [ ] array ; Traverse array arr1 [ ] ; Traverse array arr2 [ ] ; arr1 [ i - 1 ] contributes to the length of the subarray ; Otherwise ; Find the maximum value present in DP [ ] [ ] ; Return the result ; Driver Code ; Function call to find the length of the longest required subarray
function LongSubarrSeq ( arr1 , arr2 ) { let M = arr1 . length ; let N = arr2 . length ; let maxL = 0 ; let DP = new Array ( M + 1 ) ; for ( var i = 0 ; i < DP . length ; i ++ ) { DP [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < DP . length ; i ++ ) { for ( var j = 0 ; j < DP . length ; j ++ ) { DP [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= M ; i ++ ) { for ( let j = 1 ; j <= N ; j ++ ) { if ( arr1 [ i - 1 ] == arr2 [ j - 1 ] ) { DP [ i ] [ j ] = 1 + DP [ i - 1 ] [ j - 1 ] ; } else { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] ; } } } for ( let i = 1 ; i <= M ; i ++ ) { for ( let j = 1 ; j <= N ; j ++ ) { maxL = Math . max ( maxL , DP [ i ] [ j ] ) ; } } return maxL ; } let arr1 = [ 4 , 2 , 3 , 1 , 5 , 6 ] ; let arr2 = [ 3 , 1 , 4 , 6 , 5 , 2 ] ; document . write ( LongSubarrSeq ( arr1 , arr2 ) ) ;
Find minimum difference between any two elements | Returns minimum difference between any pair ; Initialize difference as infinite ; Find the min diff by comparing difference of all possible pairs in given array ; Return min diff ; Driver Code
function findMinDiff ( arr , n ) { let diff = Number . MAX_VALUE ; for ( let i = 0 ; i < n - 1 ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( ( arr [ i ] - arr [ j ] ) ) < diff ) diff = Math . abs ( ( arr [ i ] - arr [ j ] ) ) ; return diff ; } let arr = [ 1 , 5 , 3 , 19 , 18 , 25 ] ; document . write ( " " + findMinDiff ( arr , arr . length ) ) ;
Minimize removal or insertions required to make two strings equal | Function to find the longest substring in string A which is a subsequence in B ; If the indices are out of bounds ; If an already computed subproblem occurred ; Required answer if the all the characters of A and B are the same ; Required answer if there is no substring A which is a subsequence in B ; Required answer if the current character in B is skipped ; The answer for the subproblem is the maximum among the three ; Function to return the minimum strings operations required to make two strings equal ; Initialize the dp vector ; Driver Code
function findLongestSubstring ( posA , posB , A , B , canSkip , n , dp ) { if ( posA >= n posB >= n ) { return 0 ; } if ( dp [ posA ] [ posB ] [ canSkip ] != - 1 ) { return dp [ posA ] [ posB ] [ canSkip ] ; } var op1 = 0 ; var op2 = 0 ; var op3 = 0 ; if ( A [ posA ] == B [ posB ] ) { op1 = 1 + findLongestSubstring ( posA + 1 , posB + 1 , A , B , 0 , n , dp ) ; } if ( canSkip ) { op2 = findLongestSubstring ( posA + 1 , posB , A , B , canSkip , n , dp ) ; } op3 = findLongestSubstring ( posA , posB + 1 , A , B , canSkip , n , dp ) ; return dp [ posA ] [ posB ] [ canSkip ] = Math . max ( op1 , Math . max ( op2 , op3 ) ) ; } function minOperations ( A , B , N ) { var dp = Array . from ( Array ( N ) , ( ) => Array ( N ) ) ; for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < N ; j ++ ) dp [ i ] [ j ] = new Array ( 2 ) . fill ( - 1 ) ; document . write ( N - findLongestSubstring ( 0 , 0 , A , B , 1 , N , dp ) ) ; } var A = " " ; var B = " " ; var N = A . length ; minOperations ( A , B , N ) ;
Count possible N | Macros for modulus ; DP array for memoization ; Utility function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutively ; If number with N digits is generated ; Create a reference variable ; Check if the current state is already computed before ; Initialize ans as zero ; Check if count of previous digit has reached zero or not ; Fill current position only with digits that are unequal to previous digit ; Else set the value of count for this new digit accordingly from max_digit [ ] ; Function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutive times ; Stores the final count ; Print the total count ; Driver Code ; Function Call
let MOD = 1000000007 ; let dp = new Array ( 5005 ) ; for ( let i = 0 ; i < 12 ; i ++ ) { dp [ i ] = new Array ( 12 ) ; for ( let j = 0 ; j < 12 ; j ++ ) { dp [ i ] [ j ] = new Array ( 12 ) ; for ( let k = 0 ; k < 12 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } function findCountUtil ( N , maxDigit , position , previous , count ) { if ( position == N ) { return 1 ; } let ans = dp [ position ] [ previous ] [ count ] ; if ( ans != - 1 ) { return ans ; } ans = 0 ; for ( let i = 0 ; i <= 9 ; ++ i ) { if ( count == 0 && previous != i ) { ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , maxDigit [ i ] - 1 ) ) % MOD ) % MOD ; } else if ( count != 0 ) { ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , ( previous == i && position != 0 ) ? count - 1 : maxDigit [ i ] - 1 ) ) % MOD ) % MOD ; } } return ans ; } function findCount ( N , maxDigit ) { let position = 0 ; let previous = 0 ; let count = 1 ; let ans = findCountUtil ( N , maxDigit , position , previous , count ) ; document . write ( ans ) ; } let N = 2 ; let maxDigit = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] ; findCount ( N , maxDigit ) ;
Find minimum difference between any two elements | Returns minimum difference between any pair ; Sort array in non - decreasing order ; Initialize difference as infinite ; Find the min diff by comparing adjacent pairs in sorted array ; Return min diff ; Driver code
function findMinDiff ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; let diff = Number . MAX_VALUE ; for ( let i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ; return diff ; } let arr = [ 1 , 5 , 3 , 19 , 18 , 25 ] ; document . write ( " " + findMinDiff ( arr , arr . length ) ) ;
Space optimization using bit manipulations | Driver code ; Iterate through a to b , If it is a multiple of 2 or 5 Mark index in array as 1
let a = 2 , b = 10 ; let size = Math . abs ( b - a ) + 1 ; let array = [ ] ; for ( let i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) array [ i - a ] = 1 ; document . write ( " " + " " + " " ) ; for ( let i = a ; i <= b ; i ++ ) if ( array [ i - a ] == 1 ) document . write ( i + " " ) ;
Tree Traversals ( Inorder , Preorder and Postorder ) | Class containing left and right child of current node and key value ; Root of Binary Tree ; * Given a binary tree , print its nodes according to the " bottom - up " postorder * traversal . ; first recur on left subtree ; then recur on right subtree ; now deal with the node ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Given a binary tree , print its nodes in preorder ; first print data of node ; then recur on left sutree ; now recur on right subtree ; Driver method
class Node { constructor ( val ) { this . key = val ; this . left = null ; this . right = null ; } } var root = null ; function printPostorder ( node ) { if ( node == null ) return ; printPostorder ( node . left ) ; printPostorder ( node . right ) ; document . write ( node . key + " " ) ; } function printInorder ( node ) { if ( node == null ) return ; printInorder ( node . left ) ; document . write ( node . key + " " ) ; printInorder ( node . right ) ; } function printPreorder ( node ) { if ( node == null ) return ; document . write ( node . key + " " ) ; printPreorder ( node . left ) ; printPreorder ( node . right ) ; } root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; document . write ( " " ) ; printPreorder ( root ) ; document . write ( " " ) ; printInorder ( root ) ; document . write ( " " ) ; printPostorder ( root ) ;
Rearrange array by interchanging positions of even and odd elements in the given array | Function to replace odd elements with even elements and vice versa ; Push the first element to stack ; Iterate the array and swap even and odd ; pop and swap ; print the arr [ ] ; Given array arr ; Length of the arr ; Function Call
function swapEvenOdd ( arr , N ) { let stack = [ ] ; stack . push ( [ 0 , arr [ 0 ] ] ) ; for ( let i = 1 ; i < N ; i ++ ) { if ( stack . length != 0 ) { if ( arr [ i ] % 2 != stack [ stack . length - 1 ] [ 1 ] % 2 ) { let pop = stack . pop ( ) ; let index = pop [ 0 ] , val = pop [ 1 ] ; arr [ index ] = arr [ i ] ; arr [ i ] = val ; } else stack . push ( [ i , arr [ i ] ] ) ; } else stack . push ( [ i , arr [ i ] ] ) ; } for ( let i = 0 ; i < N ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 1 , 3 , 2 , 4 ] ; let N = arr . length ; swapEvenOdd ( arr , N ) ;
Maximum subarray sum possible after removing at most K array elements | Javascript program to implement the above approach ; Function to find the maximum subarray sum greater than or equal to 0 by removing K array elements ; Base case ; If overlapping subproblems already occurred ; Include current element in the subarray ; If K elements already removed from the subarray ; Remove current element from the subarray ; Utility function to find the maximum subarray sum by removing at most K array elements ; Stores overlapping subproblems of the recurrence relation ; Stores maximum subarray sum by removing at most K elements ; Calculate maximum element in dp [ ] [ ] ; Update res ; If all array elements are negative ; Update res ; Driver Code
var M = 100 ; function mxSubSum ( i , arr , j , dp ) { if ( i == 0 ) { dp [ i ] [ j ] = Math . max ( 0 , arr [ i ] ) ; return dp [ i ] [ j ] ; } if ( dp [ i ] [ j ] != - 1 ) { return dp [ i ] [ j ] ; } var X = Math . max ( 0 , arr [ i ] + mxSubSum ( i - 1 , arr , j , dp ) ) ; if ( j == 0 ) { dp [ i ] [ j ] = X ; return dp [ i ] [ j ] } var Y = mxSubSum ( i - 1 , arr , j - 1 , dp ) ; dp [ i ] [ j ] = Math . max ( X , Y ) ; return dp [ i ] [ j ] } function MaximumSubarraySum ( n , arr , k ) { var dp = Array . from ( Array ( M ) , ( ) => Array ( M ) . fill ( - 1 ) ) ; mxSubSum ( n - 1 , arr , k , dp ) ; var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j <= k ; j ++ ) { res = Math . max ( res , dp [ i ] [ j ] ) ; } } if ( arr . reduce ( ( a , b ) => Math . max ( a , b ) ) < 0 ) { res = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; } return res ; } var arr = [ - 2 , 1 , 3 , - 2 , 4 , - 7 , 20 ] ; var K = 1 ; var N = arr . length ; document . write ( MaximumSubarraySum ( N , arr , K ) ) ;
Minimum number of operations required to make all elements of at least one row of given Matrix prime | Function to generate all prime numbers using Sieve of Eratosthenes ; Function to check if a number is prime or not ; prime [ i ] : Check if i is a prime number or not ; Initialize prime [ ] array to true ; Iterate over the range [ 2 , sqrt ( n ) ] ; If p is a prime number ; Mark all multiples of i to false ; Update i ; Function to find minimum operations to make all elements of at least one row of the matrix as prime numbers ; dp [ i ] : Stores minimum operations to get i prime numbers in a row ; Initialize dp [ ] array to ( n + 1 ) ; Traverse the array ; Stores count of prime numbers in a i - th row ; Iterate over the range [ ( 1 << m ) - 1 , 0 ] ; If a row exist which contains j prime numbers ; Update dp [ j bitmask ] ; Update dp [ bitmask ] ; Return minimum operations to get a row of the matrix with all prime numbers ; Function to count prime numbers in a row ; i - th bit of bitmask check if i - th column is a prime or not ; Traverse the array ; if a [ i ] is a prime number ; Update bitmask ; Driver Code ; Stores count of columns in the matrix ; Stores length ; Calculate all prime numbers in range [ 1 , max ] using sieve ; Function Call
let prime = [ ] ; function sieve ( n ) { prime = new Array ( n + 1 ) ; for ( let i = 0 ; i < prime . length ; i ++ ) prime [ i ] = true ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } function MinWays ( a , n , m ) { let dp = new Array ( 1 << m ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = n + 1 ; } for ( let i = 0 ; i < a . length ; i ++ ) { let bitmask = BitMask ( a [ i ] ) ; for ( let j = ( 1 << m ) - 1 ; j >= 0 ; j -- ) { if ( dp [ j ] != n + 1 ) { dp [ j bitmask ] = Math . min ( dp [ j bitmask ] , dp [ j ] + 1 ) ; } } dp [ bitmask ] = 1 ; } return ( dp [ ( 1 << m ) - 1 ] - 1 ) == ( n + 1 ) ? - 1 : ( dp [ ( 1 << m ) - 1 ] - 1 ) ; } function BitMask ( a ) { let bitmask = 0 ; for ( let i = 0 ; i < a . length ; i ++ ) { if ( prime [ a [ i ] ] ) { bitmask |= ( 1 << i ) ; } } return bitmask ; } let mat = [ [ 4 , 6 , 5 , 8 ] , [ 2 , 9 , 12 , 14 ] , [ 32 , 7 , 18 , 16 ] , [ 12 , 4 , 35 , 17 ] ] ; let m = mat [ 0 ] . length ; let n = mat . length ; let max = 10000 ; sieve ( max ) ; document . write ( MinWays ( mat , n , m ) ) ;