codeparrot/xlcost-text-to-code huggingface dataset:
Minimum Count of Bit flips required to make a Binary String Palindromic | Function to calculate the length of the binary string ; Length ; Right shift of n ; Increment the length ; Return the length ; Function to check if the bit present at i - th position is a set bit or not ; Returns true if the bit is set ; Function to count the minimum number of bit flips required ; Length of the binary form ; Number of flips ; Poleter to the LSB ; Poleter to the MSB ; Check if the bits are equal ; Decrementing the left poleter ; Incrementing the right poleter ; Returns the number of bits to flip . ; Driver Code
function check_length ( n ) { let ans = 0 ; while ( n != 0 ) { n = n >> 1 ; ans ++ ; } return ans ; } function check_ith_bit ( n , i ) { return ( n & ( 1 << ( i - 1 ) ) ) != 0 ? true : false ; } function no_of_flips ( n ) { let len = check_length ( n ) ; let ans = 0 ; let right = 1 ; let left = len ; while ( right < left ) { if ( check_ith_bit ( n , right ) != check_ith_bit ( n , left ) ) ans ++ ; left -- ; right ++ ; } return ans ; } let n = 12 ; document . write ( no_of_flips ( n ) ) ;
Split N into two integers whose addition to A and B makes them equal | Function to calculate the splitted numbers ; Calculate X ; If X is odd ; No pair is possible ; Otherwise ; Calculate X ; Calculate Y ; Driver function
function findPair ( A , B , N ) { var X , Y ; X = N - B + A ; if ( X % 2 != 0 ) { document . write ( " " ) ; } else { X = X / 2 ; Y = N - X ; document . write ( X + " " + Y ) ; } } var A = 1 ; var B = 3 ; var N = 4 ; findPair ( A , B , N ) ;
Find the amplitude and number of waves for the given array | Function to find the amplitude and number of waves for the given array ; Check for both sides adjacent elements that both must be less or both must be greater than current element ; Update amplitude with max value ; Prlet the Amplitude ; Given array a [ ] ; Calculate number of waves ; Function Call
function check ( a , n ) { let ma = a [ 1 ] - a [ 0 ] ; for ( let i = 1 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] > a [ i - 1 ] && a [ i + 1 ] < a [ i ] ) || ( a [ i ] < a [ i - 1 ] && a [ i + 1 ] > a [ i ] ) ) ma = Math . max ( ma , Math . abs ( a [ i ] - a [ i + 1 ] ) ) ; else return false ; } document . write ( " " + ma ) ; document . write ( " " ) ; return true ; } let a = [ 1 , 2 , 1 , 5 , 0 , 7 , - 6 ] ; let n = a . length ; let wave = ( n - 1 ) / 2 ; if ( check ( a , n ) ) document . write ( " " + wave ) ; else document . write ( " " ) ;
Largest string obtained in Dictionary order after deleting K characters | Function to find the largest string after deleting k characters ; Deque dq used to find the largest string in dictionary after deleting k characters ; Iterate till the length of the string ; Condition for popping characters from deque ; To store the resultant string ; To form resultant string ; Return the resultant string ; Driver code ; Given String ; Function call ; Print the answer
function largestString ( n , k , s ) { var deq = [ ] ; for ( var i = 0 ; i < n ; ++ i ) { while ( deq . length > 0 && deq [ deq . length - 1 ] < s [ i ] && k > 0 ) { deq . shift ( ) ; k -- ; } deq . push ( s [ i ] ) ; } var st = " " ; deq . forEach ( c => { st = st + c ; } ) ; return st ; } var n = 4 ; var k = 2 ; var sc = " " ; var result = largestString ( n , k , sc ) ; document . write ( result ) ;
Count total set bits in all numbers from range L to R | Function that counts the set bits from 0 to N ; To store sum of set bits from 0 - N ; Until n >= to 2 ^ i ; This k will get flipped after 2 ^ i iterations ; Change is iterator from 2 ^ i to 1 ; This will loop from 0 to n for every bit position ; When change = 1 flip the bit ; Again set change to 2 ^ i ; Increment the position ; Function that counts the set bit in the range ( L , R ) ; Return the count ; Given L and R ; Function Call
function countSetBit ( n ) { var i = 0 ; var ans = 0 ; while ( ( 1 << i ) <= n ) { var k = 0 ; var change = 1 << i ; for ( var j = 0 ; j <= n ; j ++ ) { ans += k ; if ( change == 1 ) { k = ! k ; change = 1 << i ; } else { change -- ; } } i ++ ; } return ans ; } function countSetBits ( L , R ) { return Math . abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ; } var L = 3 , R = 5 ; document . write ( " " + countSetBits ( L , R ) ) ;
Count total set bits in all numbers from range L to R | Function to count set bit in range ; Count variable ; Find the set bit in Nth number ; If last bit is set ; Left sift by one bit ; Return count ; Given Range L and R ; Function Call
function countSetBits ( L , R ) { let count = 0 ; for ( let i = L ; i <= R ; i ++ ) { let n = i ; while ( n > 0 ) { count += ( n & 1 ) ; n = n >> 1 ; } } return count ; } let L = 3 , R = 5 ; document . write ( " " + countSetBits ( L , R ) ) ;
Count total set bits in all numbers from range L to R | Function to count set bit in [ L , R ] ; Variable for count set bit in range ; Count set bit for all number in range ; Use inbuilt function ; Given range L and R ; Function Call
function countSetBits ( L , R ) { let count = 0 ; for ( let i = L ; i <= R ; i ++ ) { count = Number ( i . toString ( ) . split ( " " ) . sort ( ) ) ; } return count ; } let L = 3 , R = 5 ; document . write ( " " + countSetBits ( L , R ) ) ;
Make max elements in B [ ] equal to that of A [ ] by adding / subtracting integers in range [ 0 , K ] | Function that count the number of integers from array B [ ] such that subtracting element in the range [ 0 , K ] given any element in A [ ] ; To store the count of element ; Traverse the array B [ ] ; Traverse the array A [ ] ; Find the difference ; If difference is atmost K then increment the cnt ; Print the count ; Given array A [ ] and B [ ] ; Given K ; Function call
function countElement ( A , N , B , M , K ) { let cnt = 0 ; for ( let i = 0 ; i < M ; i ++ ) { let currentElement = B [ i ] ; for ( let j = 0 ; j < N ; j ++ ) { let diff = Math . abs ( currentElement - A [ j ] ) ; if ( diff <= K ) { cnt ++ ; break ; } } } document . write ( cnt ) ; } let A = [ 100 , 65 , 35 , 85 , 55 ] ; let B = [ 30 , 60 , 75 , 95 ] ; let K = 5 ; let N = A . length ; let M = B . length ; countElement ( A , N , B , M , K ) ;
Generate a String of having N * N distinct non | Function to construct a string having N * N non - palindromic substrings ; Driver Code
function createString ( N ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( ' ' ) ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( ' ' ) ; } } let N = 4 ; createString ( N ) ;
Maximum Subset Sum possible by negating the entire sum after selecting the first Array element | Function returns maximum subset sum from the given array = ; Case 2 : Negate values from A [ 1 ] to A [ N - 1 ] ; Include only positives for max subset sum ; Return max sum obtained ; Function to return maximum of the maximum subset sum calculated for the two cases ; Case 1 ; Case 2 ; Modifying the sum ; Including first element ; Negating again ; Return the required answer ; Driver Code
function maxSubset ( A , flag ) { let n = A . length ; let sum = 0 ; if ( flag ) { for ( let i = 1 ; i < n ; i ++ ) A [ i ] = - A [ i ] ; } for ( let i = 1 ; i < n ; i ++ ) { if ( A [ i ] > 0 ) { sum += A [ i ] ; } } return sum ; } function findBest ( A ) { let x = maxSubset ( A , false ) ; let y = maxSubset ( A , true ) ; y = - y ; y += A [ 0 ] ; y = - y ; return Math . max ( x , y ) ; } let A = [ 1 , 10 , 4 , - 6 , 3 ] ; document . write ( findBest ( A ) + " " ) ;
Construct an Array of Strings having Longest Common Prefix specified by the given Array | Function to find the array of strings ; Marks the ( N + 1 ) th string ; To generate remaining N strings ; Find i - th string using ( i + 1 ) - th string ; Check if current character is b ; Otherwise ; Insert the string ; Return the answer ; Driver Code ; Print the strings
function solve ( n , arr ) { var s = [ ' ' , ' ' , ' ' ] var ans = [ ] ; ans . push ( s . join ( " " ) ) ; for ( var i = n - 1 ; i >= 0 ; i -- ) { if ( s . length - 1 >= arr [ i ] ) { var ch = s [ arr [ i ] ] ; if ( ch == ' ' ) ch = ' ' ; else ch = ' ' ; s [ arr [ i ] ] = ch ; } ans . push ( s . join ( " " ) ) ; } return ans ; } var arr = [ 2 , 0 , 3 ] ; var n = arr . length ; var ans = solve ( n , arr ) ; for ( var i = ans . length - 1 ; i >= 0 ; i -- ) { document . write ( ans [ i ] + " " ) ; }
Minimize swaps required to maximize the count of elements replacing a greater element in an Array | Function to find the minimum number of swaps required ; Sort the array in ascending order ; Iterate until a greater element is found ; Keep incrementing ind ; If a greater element is found ; Increase count of swap ; Increment ind ; If end of array is reached ; Return the answer ; Driver Code
function countSwaps ( A , n ) { A . sort ( ) ; let ind = 1 , res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { while ( ind < n && A [ ind ] == A [ i ] ) ind ++ ; if ( ind < n && A [ ind ] > A [ i ] ) { res ++ ; ind ++ ; } if ( ind >= n ) break ; } return res ; } let A = [ 4 , 3 , 3 , 2 , 5 ] ; document . write ( countSwaps ( A , 5 ) ) ;
Minimize swaps required to maximize the count of elements replacing a greater element in an Array | Function to find the minimum number of swaps required ; Stores the frequency of the array elements ; Stores maximum frequency ; Find the max frequency ; Update frequency ; Update maximum frequency ; Driver Code ; function call
function countSwaps ( A , n ) { var mp = new Map ( ) ; var max_frequency = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( mp . has ( A [ i ] ) ) mp . set ( A [ i ] , mp . get ( A [ i ] ) + 1 ) else mp . set ( A [ i ] , 1 ) ; max_frequency = Math . max ( max_frequency , mp . get ( A [ i ] ) ) ; } return n - max_frequency ; } var A = [ 6 , 5 , 4 , 3 , 2 , 1 ] ; document . write ( countSwaps ( A , 6 ) ) ;
Minimum number of steps required to obtain the given Array by the given operations | Function to calculate the minimum steps to obtain the desired array ; Initialize variable ; Iterate over the array arr ; Check if i > 0 ; Update the answer ; Return the result ; Driver Code
function min_operation ( a , n ) { var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( i > 0 ) ans += Math . abs ( a [ i ] - a [ i - 1 ] ) ; else ans += Math . abs ( a [ i ] ) ; } return ans ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; document . write ( min_operation ( arr , n ) ) ;
Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y | Function to print the required matrix ; For 1 * 1 matrix ; Greater number ; Smaller number ; Sets / Resets for alternate filling of the matrix ; Print the matrix ; If end of row is reached ; Driver Code
function FindMatrix ( n , m , x , y ) { var a , b , i , j ; if ( n * m == 1 ) { if ( x > y ) { document . write ( y ) ; } else { document . write ( x ) ; } return ; } a = Math . min ( x , y ) ; b = Math . min ( 2 * x , y ) - a ; var flag = true ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( flag ) document . write ( a + " " ) ; else document . write ( b + " " ) ; flag = ! flag ; } if ( ( ( n % 2 != 0 && m % 2 == 0 ) || ( n % 2 == 0 && m % 2 == 0 ) ) ) flag = ! flag ; document . write ( " " ) ; } } var N , M , X , Y ; N = 3 ; M = 3 ; X = 5 ; Y = 3 ; FindMatrix ( N , M , X , Y ) ;
Count distinct elements after adding each element of First Array with Second Array | Function to find Occurrence of each element from 1 to 2 * MAX ; Initialise MAX ; Count vector to store count of each element from 1 to 2 * MAX ; Size of Arr1 and Arr2 ; Find the elements of arr3 [ ] and increase count of element by 1 ; Prlet the result ; Given arrays arr1 [ ] and arr2 [ ] ; Function call
function findCount ( Arr1 , Arr2 ) { let MAX = Math . max ( Math . max ( ... Arr1 ) , Math . max ( ... Arr2 ) ) ; let Count = Array . from ( { length : 2 * MAX + 1 } , ( _ , i ) => 0 ) ; let n = Arr1 . length , m = Arr2 . length ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { let element = Arr1 [ i ] + Arr2 [ j ] ; Count [ element ] ++ ; } } for ( let i = 1 ; i <= 2 * MAX ; i ++ ) { if ( Count [ i ] > 0 ) { document . write ( i + " " + Count [ i ] + " " ) ; } } } let arr1 = [ 1 , 2 ] ; let arr2 = [ 1 , 2 , 1 ] ; findCount ( arr1 , arr2 ) ;
Find two numbers with difference and division both same as N | Function to find two numbers with difference and division both as N ; Condition if the answer is not possible ; Calculate a and b ; Prlet the values ; Given N ; Function call
function findAandB ( N ) { if ( N == 1 ) { document . write ( " " ) ; return ; } let a = N * N / ( N - 1 ) ; let b = a / N ; document . write ( " " + a + " " ) ; document . write ( " " + b + " " ) ; } let N = 6 ; findAandB ( N ) ;
Maximize number of elements from Array with sum at most K | Function to select a maximum number of elements in array whose sum is at most K ; Sort the array ; Calculate the sum and count while iterating the sorted array ; Iterate for all the elements in the array ; Add the current element to sum ; Increment the count ; Return the answer ; Given array ; Given sum k ; Function Call
function maxSelections ( A , n , k ) { A . sort ( ) ; let sum = 0 ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = sum + A [ i ] ; if ( sum > k ) { break ; } count ++ ; } return count ; } let A = [ 3 , 7 , 2 , 9 , 4 ] ; let k = 15 ; let n = A . length ; document . write ( maxSelections ( A , n , k ) ) ;
Pair of integers with difference K having an element as the K | Function to find the required pair ; No pair possible ; Driver Code
function computePair ( K ) { if ( K == 1 ) { document . write ( " " ) ; return ; } else { document . write ( K * K / ( K - 1 ) + " " ) ; document . write ( K / ( K - 1 ) ) ; } } var K = 6 ; computePair ( K ) ;
Generate a Binary String without any consecutive 0 ' s ▁ and ▁ at ▁ most ▁ K ▁ consecutive ▁ 1' s | Function to construct the binary string ; Conditions when string construction is not possible ; Stores maximum 1 's that  can be placed in between ; Place 0 's ; Place 1 's in between ; Count remaining M 's ; Place 1 's at the end ; Place 1 's at the beginning ; Return the final string ; Driver Code
function ConstructBinaryString ( N , M , K ) { if ( M < ( N - 1 ) || M > K * ( N + 1 ) ) return " " ; let ans = " " ; let l = Math . min ( K , M / ( N - 1 ) ) ; let temp = N ; while ( temp != 0 ) { temp -- ; ans += ' ' ; if ( temp == 0 ) break ; for ( let i = 0 ; i < l ; i ++ ) { ans += ' ' ; } } M -= ( N - 1 ) * l ; if ( M == 0 ) return ans ; l = Math . min ( M , K ) ; for ( let i = 0 ; i < l ; i ++ ) ans += ' ' ; M -= l ; while ( M > 0 ) { ans = ' ' + ans ; M -- ; } return ans ; } let N = 5 , M = 9 , K = 2 ; document . write ( ConstructBinaryString ( N , M , K ) ) ;
Find smallest number formed by inverting digits of given number N | Function to invert the digits of integer N to form minimum possible number ; Initialize the array ; Iterate till the number N exists ; Last digit of the number N ; Checking if the digit is smaller than 9 - digit ; Store the smaller digit in the array ; Reduce the number each time ; Check if the digit starts with 0 or not ; Print the answer ; Given Number ; Function Call
function number ( num ) { let a = new Array ( 20 ) ; let r , j ; let i = 0 ; while ( num > 0 ) { r = num % 10 ; if ( 9 - r > r ) a [ i ] = r ; else a [ i ] = 9 - r ; i ++ ; num = parseInt ( num / 10 , 10 ) ; } if ( a [ i - 1 ] == 0 ) { document . write ( 9 ) ; i -- ; } for ( j = i - 1 ; j >= 0 ; j -- ) document . write ( a [ j ] ) ; } let num = 4545 ; number ( num ) ;
Check if the concatenation of first N natural numbers is divisible by 3 | Function that returns True if concatenation of first N natural numbers is divisible by 3 ; Check using the formula ; Given Number ; Function Call
function isDivisible ( N ) { return ( N - 1 ) % 3 != 0 ; } var N = 6 ; if ( isDivisible ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of N digit Numbers whose sum of every K consecutive digits is equal | Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits ; If sum is not equal then break the loop ; Increment the count if it satisfy the given condition ; Given N and K ; Function call
function countDigitSum ( N , K ) { let l = parseInt ( Math . pow ( 10 , N - 1 ) ) , r = parseInt ( Math . pow ( 10 , N ) - 1 ) ; let count = 0 ; for ( let i = l ; i <= r ; i ++ ) { let num = i ; let digits = new Array ( N ) ; for ( let j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num = parseInt ( num / 10 ) ; } let sum = 0 , flag = 0 ; for ( let j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( let j = 1 ; j < N - K + 1 ; j ++ ) { let curr_sum = 0 ; for ( let m = j ; m < j + K ; m ++ ) curr_sum += digits [ m ] ; if ( sum != curr_sum ) { flag = 1 ; break ; } } if ( flag == 0 ) { count ++ ; } } return count ; } let N = 2 , K = 1 ; document . write ( countDigitSum ( N , K ) ) ;
Perfect Square factors of a Number | Function that returns the count of factors that are perfect squares ; Stores the count of number of times a prime number divides N . ; Stores the number of factors that are perfect square ; Count number of 2 's  that divides N ; Calculate ans according to above formula ; Check for all the possible numbers that can divide it ; Check the number of times prime number i divides it ; Calculate ans according to above formula ; Return final count ; Driver Code
function noOfFactors ( N ) { if ( N == 1 ) return 1 ; let count = 0 ; let ans = 1 ; while ( N % 2 == 0 ) { count ++ ; N = N / 2 ; } ans *= ( count / 2 + 1 ) ; for ( let i = 3 ; i * i <= N ; i = i + 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } ans *= ( count / 2 + 1 ) ; } return ans ; } let N = 100 ; document . write ( noOfFactors ( N ) ) ;
Nth angle of a Polygon whose initial angle and per angle increment is given | Function to check if the angle is possible or not ; Angular sum of a N - sided polygon ; Angular sum of N - sided given polygon ; Check if both sum are equal ; Function to find the nth angle ; Calculate nth angle ; Return the nth angle ; Driver Code ; Checks the possibility of the polygon exist or not ; Print nth angle of the polygon
function possible ( N , a , b , n ) { let sum_of_angle = 180 * ( N - 2 ) ; let Total_angle = Math . floor ( ( N * ( ( 2 * a ) + ( N - 1 ) * b ) ) / 2 ) ; if ( sum_of_angle != Total_angle ) return false ; else return true ; } function nth_angle ( N , a , b , n ) { let nth = 0 ; nth = a + ( n - 1 ) * b ; return nth ; } let N = 3 , a = 30 , b = 30 , n = 3 ; if ( possible ( N , a , b , n ) ) document . write ( nth_angle ( N , a , b , n ) ) ; else document . write ( " " ) ;
Check if the given string is linear or not | Function to check if the given string is linear or not ; Iterate over string ; If character is not same as the first character then return false ; Increment the tmp ; Given String str ; Function call
function is_linear ( s ) { let tmp = 0 ; let first = s [ 0 ] ; for ( let pos = 0 ; pos < s . length ; pos += tmp ) { if ( s [ pos ] != first ) { return false ; } tmp ++ ; } return true ; } let str = " " ; if ( is_linear ( str ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Minimize cost to color all the vertices of an Undirected Graph | Function to implement DFS Traversal to marks all the vertices visited from vertex U ; Mark U as visited ; Traverse the adjacency list of U ; Function to find the minimum cost to color all the vertices of graph ; To store adjacency list ; Loop through the edges to create adjacency list ; To check if a vertex of the graph is visited ; Mark visited to all the vertices that can be reached by colored vertices ; Perform DFS ; To store count of uncolored sub - graphs ; Loop through vertex to count uncolored sub - graphs ; If vertex not visited ; Increase count of uncolored sub - graphs ; Perform DFS to mark visited to all vertices of current sub - graphs ; Calculate minimum cost to color all vertices ; Print the result ; Given number of vertices and edges ; Given edges ; Given cost of coloring and adding an edge ; Given array of colored vertices
function DFS ( U , vis , adj ) { vis [ U ] = 1 ; for ( var V of adj [ U ] ) { if ( vis [ V ] == 0 ) DFS ( V , vis , adj ) ; } } function minCost ( N , M , vCost , eCost , sorc , colored , destination ) { var adj = [ ] ; for ( var i = 0 ; i < N + 1 ; i ++ ) adj . push ( new Array ( ) ) ; for ( var i = 0 ; i < M ; i ++ ) { ( adj [ sorc [ i ] ] ) . push ( destination [ i ] ) ; ( adj [ destination [ i ] ] ) . push ( sorc [ i ] ) ; } var vis = Array ( N + 1 ) . fill ( 0 ) ; for ( var i = 0 ; i < colored . length ; i ++ ) { DFS ( colored [ i ] , vis , adj ) ; } var X = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { if ( vis [ i ] == 0 ) { X ++ ; DFS ( i , vis , adj ) ; } } var mincost = X * Math . min ( vCost , eCost ) ; document . write ( mincost ) ; } var N = 3 , M = 1 ; var sorc = [ 1 ] ; var destination = [ 2 ] ; var vCost = 3 , eCost = 2 ; var colored = [ ] ; colored . push ( 1 ) ; minCost ( N , M , vCost , eCost , sorc , colored , destination ) ;
Minimum cost to reduce the integer N to 1 as per given conditions | Function to find the minimum cost to reduce the integer N to 1 by the given operations ; Check if x is 1 ; Print the answer ; Prestore the answer ; Iterate till n exists ; Divide N by x ; Reduce n by x ; Add the cost ; Update the answer ; Return the answer ; Initialize the variables ; Function call
function min_cost ( n , x , p , q ) { if ( x == 1 ) { document . write ( ( n - 1 ) * p + " " ) ; return 0 ; } let ans = ( n - 1 ) * p ; let pre = 0 ; while ( n > 1 ) { let tmp = Math . floor ( n / x ) ; if ( tmp < 0 ) break ; pre += ( n - tmp * x ) * p ; n = Math . floor ( n / x ) pre += q ; ans = Math . min ( ans , pre + ( n - 1 ) * p ) ; } return ans ; } let n = 5 , x = 2 , p = 2 , q = 3 ; document . write ( min_cost ( n , x , p , q ) ) ;
Number of ways to sum up a total of N from limited denominations | Function to find the number of ways to sum up a total of N from limited denominations ; Store the count of denominations ; Stores the final result ; As one of the denominations is rupee 1 , so we can reduce the computation by checking the equality for N - ( A * 1 ) = N - A ; Increment the count for number of ways ; Given denominations ; Function call
function calculateWays ( arr1 , arr2 , N ) { let A = arr2 [ 0 ] , B = arr2 [ 1 ] ; let C = arr2 [ 2 ] , D = arr2 [ 3 ] ; let ans = 0 ; for ( let b = 0 ; b <= B && b * 5 <= ( N ) ; b ++ ) for ( let c = 0 ; c <= C && b * 5 + c * 10 <= ( N ) ; c ++ ) for ( let d = 0 ; d <= D && b * 5 + c * 10 + d * 20 <= ( N ) ; d ++ ) if ( ( b * 5 ) + ( c * 10 ) + ( d * 20 ) >= ( N - A ) ) ans ++ ; return ans ; } let N = 123 ; let arr1 = [ 1 , 5 , 10 , 20 ] ; let arr2 = [ 6 , 4 , 3 , 5 ] ; document . write ( calculateWays ( arr1 , arr2 , N ) ) ;
Number of ways to sum up a total of N from limited denominations | Javascript program for the above approach ; Function to find the number of ways to sum up a total of N from limited denominations ; Store the count of denominations ; Stores the final result ; This will give the number of coins for all combinations of coins with value 1 and 5 ; L2 will sum the values of those indices of ways [ ] which is equal to ( N - ( c * 10 + d * 20 ) ) ; Return the final count ; Given Denominations ; Function Call
var ways = Array ( 1010 ) . fill ( 0 ) ; function calculateWays ( arr1 , arr2 , N ) { var A = arr2 [ 0 ] , B = arr2 [ 1 ] ; var C = arr2 [ 2 ] , D = arr2 [ 3 ] ; var ans = 0 ; for ( var b = 0 ; b <= B && b * 5 <= N ; b ++ ) { for ( var a = 0 ; a <= A && a * 1 + b * 5 <= N ; a ++ ) { ways [ a + b * 5 ] ++ ; } } for ( var c = 0 ; c <= C && c * 10 <= ( N ) ; c ++ ) { for ( var d = 0 ; d <= D && c * 10 + d * 20 <= ( N ) ; d ++ ) { ans += ways [ N - c * 10 - d * 20 ] ; } } return ans ; } var N = 123 ; var arr1 = [ 1 , 5 , 10 , 20 ] ; var arr2 = [ 6 , 4 , 3 , 5 ] ; document . write ( calculateWays ( arr1 , arr2 , N ) ) ;