codeparrot/xlcost-text-to-code huggingface dataset:
Program to print an array in Pendulum Arrangement | Prints pendulam arrangement of arr [ ] ; sorting the elements ; Auxiliary array to store output ; calculating the middle index ; storing the minimum element in the middle i is index for output array and j is for input array . ; adjustment for when no . of elements is even ; Printing the pendulum arrangement ; input Array ; calling pendulum function
function pendulumArrangement ( arr , n ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; var op = [ ... Array ( n ) ] ; var mid = parseInt ( ( n - 1 ) / 2 ) ; var j = 1 , i = 1 ; op [ mid ] = arr [ 0 ] ; for ( i = 1 ; i <= mid ; i ++ ) { op [ mid + i ] = arr [ j ++ ] ; op [ mid - i ] = arr [ j ++ ] ; } if ( n % 2 == 0 ) op [ mid + i ] = arr [ j ] ; document . write ( " " ) ; for ( i = 0 ; i < n ; i ++ ) document . write ( op [ i ] + " " ) ; document . write ( " " ) ; } var arr = [ 14 , 6 , 19 , 21 , 12 ] ; var n = arr . length ; pendulumArrangement ( arr , n ) ;
Minimize the sum of product of two arrays with permutations allowed | Returns minimum sum of product of two arrays with permutations allowed ; Sort A and B so that minimum and maximum value can easily be fetched . ; Multiplying minimum value of A and maximum value of B ; Driver Code
function minValue ( A , B , n ) { A . sort ( ) ; B . sort ( ) ; let result = 0 ; for ( let i = 0 ; i < n ; i ++ ) result += ( A [ i ] * B [ n - i - 1 ] ) ; return result ; } let A = [ 3 , 1 , 1 ] ; let B = [ 6 , 5 , 4 ] ; let n = A . length ; document . write ( minValue ( A , B , n ) ) ;
Count of numbers in range [ L , R ] which can be represented as sum of two perfect powers | Function to find the number of numbers that can be expressed in the form of the sum of two perfect powers ; Stores all possible powers ; Push 1 and 0 in it ; Iterate over all the exponents ; Iterate over all possible numbers ; This loop will run for a maximum of sqrt ( R ) times ; Push this power in the array pows [ ] ; Increase the number ; Stores if i can be expressed as the sum of perfect power or not ; Iterate over all possible pairs of the array pows [ ] ; The number is valid ; Find the prefix sum of the array ok [ ] ; Return the count of required number ; Driver Code
function TotalPerfectPowerSum ( L , R ) { let pows = [ ] ; pows . push ( 0 ) ; pows . push ( 1 ) ; for ( let p = 2 ; p < 25 ; p ++ ) { let num = 2 ; while ( Math . floor ( Math . pow ( num , p ) + 0.5 ) <= R ) { pows . push ( Math . floor ( Math . pow ( num , p ) + 0.5 ) ) ; num ++ ; } } let ok = new Array ( R + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < pows . length ; i ++ ) { for ( let j = 0 ; j < pows . length ; j ++ ) { if ( pows [ i ] + pows [ j ] <= R && pows [ i ] + pows [ j ] >= L ) { ok [ pows [ i ] + pows [ j ] ] = 1 ; } } } for ( let i = 1 ; i <= R ; i ++ ) { ok [ i ] += ok [ i - 1 ] ; } return ok [ R ] - ok [ L - 1 ] ; } let L = 5 , R = 8 ; document . write ( TotalPerfectPowerSum ( L , R ) ) ;
Minimize absolute difference between sum of subtrees formed after splitting Binary tree into two | Structure of Node ; Function to calculate minimum absolute difference of subtrees after splitting the tree into two parts ; Reference variable to store the answer ; Function to store sum of values of current node , left subtree and right subtree in place of current node 's value ; Function to perform every possible split and calculate absolute difference of subtrees ; Absolute difference in subtrees if left edge is broken ; Absolute difference in subtrees if right edge is broken ; Update minDiff if a difference lesser than it is found ; Construct the tree ; Print the output
class Node { constructor ( val ) { this . left = null ; this . right = null ; this . val = val ; } } function minAbsDiff ( root ) { let minDiff = new Array ( 1 ) ; minDiff [ 0 ] = Number . MAX_VALUE ; postOrder ( root ) ; preOrder ( root , minDiff ) ; return minDiff [ 0 ] ; } function postOrder ( root ) { if ( root == null ) return 0 ; root . val += postOrder ( root . left ) + postOrder ( root . right ) ; return root . val ; } function preOrder ( root , minDiff ) { if ( root == null ) return ; let leftDiff = Math . abs ( root . left . val - ( root . val - root . left . val ) ) ; let rightDiff = Math . abs ( root . right . val - ( root . val - root . right . val ) ) ; minDiff [ 0 ] = Math . min ( minDiff [ 0 ] , Math . min ( leftDiff , rightDiff ) ) ; return ; } let 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 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; document . write ( minAbsDiff ( root ) ) ;
Maximize Array sum after changing sign of any elements for exactly M times | Function to find the maximum sum with M flips ; Declare a priority queue i . e . min heap ; Declare the sum as zero ; Push all elements of the array in it ; Iterate for M times ; Get the top element ; Flip the sign of the top element ; Remove the top element ; Update the sum ; Push the temp into the queue ; Given input
function findMaximumSumWithMflips ( arr , N , M ) { let pq = [ ] let sum = 0 for ( let i = 0 ; i < N ; i ++ ) { pq . push ( arr [ i ] ) sum += arr [ i ] pq . sort ( ( a , b ) => a - b ) } while ( M > 0 ) { sum -= pq [ 0 ] temp = - 1 * pq [ 0 ] pq . shift ( ) sum += temp pq . push ( temp ) pq . sort ( ( a , b ) => a - b ) M -= 1 } document . write ( sum ) } let arr = [ - 3 , 7 , - 1 , - 5 , - 3 ] let N = arr . length let M = 4 findMaximumSumWithMflips ( arr , N , M )
Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other | Function to find the minimum number of operations to make all the elements of the array equal ; Store the sum of the array arr [ ] ; Traverse through the array ; If it is not possible to make all array element equal ; Store the minimum number of operations needed ; Traverse through the array ; Finally , print the minimum number operation to make array elements equal ; Given Input ; Function Call
function miniOperToMakeAllEleEqual ( arr , n , k ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % n ) { document . write ( - 1 ) ; return ; } let valueAfterDivision = sum / n ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( Math . abs ( valueAfterDivision - arr [ i ] ) % k != 0 ) { document . write ( - 1 ) ; return ; } count += Math . abs ( valueAfterDivision - arr [ i ] ) / k ; } document . write ( Math . floor ( count / 2 ) ) ; } let n = 3 , k = 3 ; let arr = [ 5 , 8 , 11 ] ; miniOperToMakeAllEleEqual ( arr , n , k ) ;
Minimum operations to make product of adjacent element pair of prefix sum negative | Function to find minimum operations needed to make the product of any two adjacent elements in prefix sum array negative ; Stores the minimum operations ; Stores the prefix sum and number of operations ; Traverse the array ; Update the value of sum ; Check if i + r is odd ; Check if prefix sum is not positive ; Update the value of ans and sum ; Check if prefix sum is not negative ; Update the value of ans and sum ; Update the value of res ; Print the value of res ; Driver Code
function minOperations ( a ) { let res = Number . MAX_VALUE ; let N = a . length ; for ( let r = 0 ; r < 2 ; r ++ ) { let sum = 0 , ans = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( ( i + r ) % 2 ) { if ( sum <= 0 ) { ans += - sum + 1 ; sum = 1 ; } } else { if ( sum >= 0 ) { ans += sum + 1 ; sum = - 1 ; } } } res = Math . min ( res , ans ) ; } document . write ( res ) ; } let a = [ 1 , - 3 , 1 , 0 ] ; minOperations ( a ) ;
Minimize sum of product of same | Function to print1 the arrays ; Function to reverse1 the subarray ; Function to calculate the minimum product of same - indexed elements of two given arrays ; Calculate initial product ; Traverse all odd length subarrays ; Remove the previous product ; Add the current product ; Check if current product is minimum or not ; Traverse all even length subarrays ; Remove the previous product ; Add to the current product ; Check if current product is minimum or not ; Update the poleters ; reverse1 the subarray ; print1 the subarray ; Driver Code
function print1 ( a , b ) { let minProd = 0 ; for ( let i = 0 ; i < a . length ; ++ i ) { document . write ( a [ i ] + " " ) ; } document . write ( " " ) ; for ( let i = 0 ; i < b . length ; ++ i ) { document . write ( b [ i ] + " " ) ; minProd += a [ i ] * b [ i ] ; } document . write ( " " ) ; document . write ( minProd ) ; } function reverse1 ( left , right , arr ) { while ( left < right ) { let temp = arr [ left ] ; arr [ left ] = arr [ right ] ; arr [ right ] = temp ; ++ left ; -- right ; } } function minimumProdArray ( a , b , l ) { let total = 0 ; for ( let i = 0 ; i < a . length ; ++ i ) { total += a [ i ] * b [ i ] ; } let min = Number . MAX_SAFE_INTEGER ; let first = 0 ; let second = 0 ; for ( let i = 0 ; i < a . length ; ++ i ) { let left = i - 1 ; let right = i + 1 ; let total1 = total ; while ( left >= 0 && right < a . length ) { total1 -= a [ left ] * b [ left ] + a [ right ] * b [ right ] ; total1 += a [ left ] * b [ right ] + a [ right ] * b [ left ] ; if ( min >= total1 ) { min = total1 ; first = left ; second = right ; } -- left ; ++ right ; } } for ( let i = 0 ; i < a . length ; ++ i ) { let left = i ; let right = i + 1 ; let total1 = total ; while ( left >= 0 && right < a . length ) { total1 -= a [ left ] * b [ left ] + a [ right ] * b [ right ] ; total1 += a [ left ] * b [ right ] + a [ right ] * b [ left ] ; if ( min >= total1 ) { min = total1 ; first = left ; second = right ; } -- left ; ++ right ; } } if ( min < total ) { reverse1 ( first , second , a ) ; print1 ( a , b ) ; } else { print1 ( a , b ) ; } } let n = 4 ; let a = [ 2 , 3 , 1 , 5 ] ; let b = [ 8 , 2 , 4 , 3 ] ; minimumProdArray ( a , b , n ) ;
Maximize cost of removing all occurrences of substrings " ab " and " ba " | Function to find the maximum cost of removing substrings " ab " and " ba " from S ; MaxStr is the substring char array with larger cost ; MinStr is the substring char array with smaller cost ; ; Denotes larger point ; Denotes smaller point ; Stores cost scored ; Stack to keep track of characters ; Traverse the string ; If the substring is maxstr ; Pop from the stack ; Add maxp to cost ; Push the character to the stack ; Remaining string after removing maxstr ; Find remaining string ; Reversing the string retrieved from the stack ; Removing all occurences of minstr ; If the substring is minstr ; Pop from the stack ; Add minp to the cost ; Otherwise ; Return the maximum cost ; Input String ; Costs
function MaxCollection ( S , P , Q ) { let maxstr = ( P >= Q ? " " : " " ) . split ( " " ) ; let minstr = ( P >= Q ? " " : " " ) . split ( " " ) ; let maxp = Math . max ( P , Q ) ; let minp = Math . min ( P , Q ) ; let cost = 0 ; let stack1 = [ ] ; let s = S . split ( " " ) ; for ( let ch = 0 ; ch < s . length ; ch ++ ) { if ( stack1 . length != 0 && ( stack1 [ stack1 . length - 1 ] == maxstr [ 0 ] && s [ ch ] == maxstr [ 1 ] ) ) { stack1 . pop ( ) ; cost += maxp ; } else { stack1 . push ( s [ ch ] ) ; } } let sb = [ ] ; while ( stack1 . length != 0 ) sb . push ( stack1 . pop ( ) ) ; sb = sb . reverse ( ) ; let remstr = sb . join ( " " ) ; for ( let ch = 0 ; ch < remstr . length ; ch ++ ) { if ( stack1 . length != 0 && ( stack1 [ stack1 . length - 1 ] == minstr [ 0 ] && remstr [ ch ] == minstr [ 1 ] ) ) { stack1 . pop ( ) ; cost += minp ; } else { stack1 . push ( remstr [ ch ] ) ; } } return cost ; } let S = " " ; let P = 6 ; let Q = 4 ; document . write ( MaxCollection ( S , P , Q ) ) ;
Maximum value of ( arr [ i ] * arr [ j ] ) + ( arr [ j ] | Function to find the value of the expression a * b + ( b - a ) ; Function to find the maximum value of the expression a * b + ( b - a ) possible for any pair ( a , b ) ; Sohe vector in ascending order ; Stores the maximum value ; Update ans by choosing the pair of the minimum and 2 nd minimum ; Update ans by choosing the pair of maximum and 2 nd maximum ; Return the value of ans ; Given inputs
function calc ( a , b ) { return ( a * b ) + ( b - a ) ; } function findMaximum ( arr , N ) { arr . sort ( function ( a , b ) { return a - b } ) let ans = Number . MIN_VALUE ; ans = Math . max ( ans , calc ( arr [ 0 ] , arr [ 1 ] ) ) ; ans = Math . max ( ans , calc ( arr [ N - 2 ] , arr [ N - 1 ] ) ) ; return ans ; } let arr = [ - 47 , 0 , 12 ] let N = arr . length ; document . write ( findMaximum ( arr , N ) ) ;
Minimize flips on K |  ; store previous flip events ; remove an item which is out range of window . ; In a window , if A [ i ] is a even number with even times fliped , it need to be fliped again . On other hand , if A [ i ] is a odd number with odd times fliped , it need to be fliped again . ; flip . push ( i ) ; insert ; Driver Code
function minKBitFlips ( A , K ) { let flip = [ ] ; let count = 0 ; for ( let i = 0 ; i < A . length ; i ++ ) { if ( flip . length != 0 && ( i - flip [ 0 ] >= K ) ) { flip . shift ( ) ; } if ( A [ i ] % 2 == flip . length % 2 ) { if ( i + K - 1 >= A . length ) { return - 1 ; } count ++ ; } } return count ; } let A = [ 0 , 1 , 0 ] ; let K = 1 ; let ans = minKBitFlips ( A , K ) ; document . write ( ans ) ;
Minimize flips on K |  ; We 're flipping the subarray from A[i] to A[i+K-1]  ; If not flip entire array , impossible ; Driver Code
function minKBitFlips ( A , K ) { let temp = new Array ( A . length ) ; let count = 0 ; let flip = 0 ; for ( let i = 0 ; i < A . length ; ++ i ) { flip ^= temp [ i ] ; if ( A [ i ] == flip ) { count ++ ; if ( i + K > A . length ) { return - 1 ; } flip ^= 1 ; if ( i + K < A . length ) { temp [ i + K ] ^= 1 ; } } } return count ; } let A = [ 0 , 1 , 0 ] ; let K = 1 ; let ans = minKBitFlips ( A , K ) ; document . write ( ans ) ;
Generate an N | JavaScript program for the above approach ; Function to calculate GCD of given array ; Utility function to check for all the combinations ; If a sequence of size N is obtained ; If gcd of current combination is K ; If current element from first array is divisible by K ; Recursively proceed further ; If current combination satisfies given condition ; Remove the element from the combination ; If current element from second array is divisible by K ; Recursively proceed further ; If current combination satisfies given condition ; Remove the element from the combination ; Function to check for all possible combinations ; Stores the subsequence ; If GCD of any sequence is not equal to K ; Given arrays ; size of the array ; Given value of K ; Function call to generate a subsequence whose GCD is K
function GCD ( a , b ) { if ( b === 0 ) return a ; return GCD ( b , a % b ) ; } function GCDArr ( a ) { var ans = a [ 0 ] ; for ( const val of a ) { ans = GCD ( ans , val ) ; } return ans ; } function findSubseqUtil ( a , b , ans , k , i , N ) { if ( ans . length === N ) { if ( GCDArr ( ans ) === k ) { document . write ( " " + ans + " " ) ; return true ; } else { return false ; } } if ( a [ i ] % k === 0 ) { ans . push ( a [ i ] ) ; var temp = findSubseqUtil ( a , b , ans , k , i + 1 , N ) ; if ( temp === true ) return true ; delete ans [ ans . length - 1 ] ; } if ( b [ i ] % k === 0 ) { ans . push ( b [ i ] ) ; var temp = findSubseqUtil ( a , b , ans , k , i + 1 , N ) ; if ( temp === true ) return true ; delete ans [ ans . length - 1 ] ; } return false ; } function findSubseq ( A , B , K , i , N ) { var ans = [ ] ; var ret = findSubseqUtil ( A , B , ans , K , i , N ) ; if ( ret === false ) document . write ( - 1 ) ; } var A = [ 5 , 3 , 6 , 2 , 9 ] ; var B = [ 21 , 7 , 14 , 12 , 28 ] ; var N = A . length ; var K = 3 ; findSubseq ( A , B , K , 0 , N ) ;
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | Function to find the K - th element in the required permutation ; Stores the required permutation ; Insert all the even numbers less than or equal to N ; Now , insert all odd numbers less than or equal to N ; Print the Kth element ; Driver code ; functions call
function findKthElement ( N , K ) { var v = [ ] ; for ( i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { v . push ( i ) ; } } for ( i = 1 ; i <= N ; i ++ ) { if ( i % 2 != 0 ) { v . push ( i ) ; } } document . write ( v [ K - 1 ] ) ; } var N = 10 , K = 3 ; findKthElement ( N , K ) ;
Maximize difference between odd and even indexed array elements by shift operations | Function to minimize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the minimum possible value ; Function to maximize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the maximum possible value ; Function to maximize the absolute difference between even and odd indexed array elements ; To calculate the difference of odd indexed elements and even indexed elements ; To calculate the difference between odd and even indexed array elements ; Print the maximum value ; Driver Code
function minimize ( n ) { let optEle = n ; let strEle = ( n ) . toString ( ) ; for ( let idx = 0 ; idx < strEle . length ; idx ++ ) { let temp = parseInt ( strEle . substring ( idx ) + strEle . substring ( 0 , idx ) ) ; optEle = Math . min ( optEle , temp ) ; } return optEle ; } function maximize ( n ) { let optEle = n ; let strEle = n . toString ( ) ; for ( let idx = 0 ; idx < strEle . length ; idx ++ ) { let temp = parseInt ( strEle . substring ( idx ) + strEle . substring ( 0 , idx ) ) ; optEle = Math . max ( optEle , temp ) ; } return optEle ; } function minimumDifference ( arr ) { let caseOne = 0 ; let minVal = 0 ; let maxVal = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( i % 2 == 0 ) minVal += minimize ( arr [ i ] ) ; else maxVal += maximize ( arr [ i ] ) ; } caseOne = Math . abs ( maxVal - minVal ) ; let caseTwo = 0 ; minVal = 0 ; maxVal = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( i % 2 == 0 ) maxVal += maximize ( arr [ i ] ) ; else minVal += minimize ( arr [ i ] ) ; caseTwo = Math . abs ( maxVal - minVal ) ; } document . write ( Math . max ( caseOne , caseTwo ) + " " ) ; } let arr = [ 332 , 421 , 215 , 584 , 232 ] ; minimumDifference ( arr ) ;
Minimize operations of removing 2 i | Function to find minimum count of steps required to remove all the array elements ; Stores minimum count of steps required to remove all the array elements ; Update N ; Traverse each bit of N ; If current bit is set ; Update cntStep ; Driver Code
function minimumStepReqArr ( arr , N ) { let cntStep = 0 ; N += 1 ; for ( let i = 31 ; i >= 0 ; i -- ) { if ( ( N & ( 1 << i ) ) != 0 ) { cntStep += 1 ; } } return cntStep ; } let arr = [ 1 , 2 , 3 ] ; let N = arr . length ; document . write ( minimumStepReqArr ( arr , N ) ) ;
Minimize removal of alternating subsequences to empty given Binary String | Function to find the minimum number of operations required to empty the string ; Initialize variables ; Traverse the string ; If current character is 0 ; Update maximum consecutive 0 s and 1 s ; Print the minimum operation ; input string ; length of string ; Function Call
function minOpsToEmptyString ( S , N ) { let one = 0 , zero = 0 ; let x0 = 0 , x1 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) { x0 ++ ; x1 = 0 ; } else { x1 ++ ; x0 = 0 ; } zero = Math . max ( x0 , zero ) ; one = Math . max ( x1 , one ) ; } document . write ( Math . max ( one , zero ) ) ; } let S = " " ; let N = S . length ; minOpsToEmptyString ( S , N ) ;
Swap upper and lower triangular halves of a given Matrix | Function to swap laterally inverted images of upper and lower triangular halves of a given matrix ; Store the matrix elements from upper & lower triangular halves ; Traverse the matrix mat [ ] [ ] ; Find the index ; If current element lies on the principal diagonal ; If current element lies below the principal diagonal ; If current element lies above the principal diagonal ; Traverse again to swap values ; Find the index ; Principal diagonal ; Below main diagonal ; Above main diagonal ; Traverse the matrix and print ; Given Matrix mat [ ] [ ] ; Swap the upper and lower triangular halves
function ReverseSwap ( mat , n ) { let lowerEle = new Array ( n ) ; let upperEle = new Array ( n ) ; let index ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { index = Math . abs ( i - j ) ; if ( i == j ) { continue ; } else if ( j < i ) { lowerEle [ index ] = mat [ i ] [ j ] ; } else { upperEle [ index ] = mat [ i ] [ j ] ; } } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { index = Math . abs ( i - j ) ; if ( i == j ) { continue ; } else if ( j < i ) { mat [ i ] [ j ] = upperEle [ index ] ; } else { mat [ i ] [ j ] = lowerEle [ index -- ] ; } } } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let mat = [ [ 1 , 2 ] , [ 4 , 5 ] ] ; let N = mat . length ; ReverseSwap ( mat , N ) ;
Minimize sum of K positive integers with given LCM | Function to find the prime power of X ; Stores prime powers of X ; Iterate over the range [ 2 , Math . sqrt ( X ) ] ; If X is divisible by i ; Stores prime power ; Calculate prime power of X ; Update X ; Update p ; Insert prime powers into primePow [ ] ; If X exceeds 1 ; Function to calculate the sum of array elements ; Stores sum of array elements ; Traverse the array ; Update sum ; Function to partition array into K groups such that sum of elements of the K groups is minimum ; If count of prime powers is equal to pos ; Stores minimum sum of of each partition of arr [ ] into K groups ; Traverse the array arr [ ] ; Include primePow [ pos ] into i - th groups ; Update res ; Remove factors [ pos ] from i - th groups ; Utility function to calculate minimum sum of K positive integers whose LCM is X ; Stores all prime powers of X ; Stores count of prime powers ; Stores minimum sum of K positive integers whose LCM is X ; If n is less than or equal to k ; Traverse primePow [ ] array ; Update sum ; Update sum ; arr [ i ] : Stores element in i - th group by partitioning the primePow [ ] array ; Update sum ; Driver Code
function primePower ( X ) { let primePow = [ ] ; for ( let i = 2 ; i * i <= X ; i ++ ) { if ( X % i == 0 ) { let p = 1 ; while ( X % i == 0 ) { X /= i ; p *= i ; } primePow . push ( p ) ; } } if ( X > 1 ) { primePow . push ( X ) ; } return primePow ; } function getSum ( ar ) { let sum = 0 ; for ( let i = 0 ; i < ar . length ; i ++ ) { sum += ar [ i ] ; } return sum ; } function getMinSum ( pos , arr , primePow ) { if ( pos == primePow . length ) { return getSum ( arr ) ; } let res = Number . MAX_VALUE ; for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] *= primePow [ pos ] ; res = Math . min ( res , getMinSum ( pos + 1 , arr , primePow ) ) ; arr [ i ] /= primePow [ pos ] ; } return res ; } function minimumSumWithGivenLCM ( k , x ) { let primePow = primePower ( x ) ; let n = primePow . length ; let sum = 0 ; if ( n <= k ) { for ( let i = 0 ; i < primePow . length ; i ++ ) { sum += primePow [ i ] ; } sum += k - n ; } else { let arr = new Array ( k ) ; for ( let i = 0 ; i < k ; i ++ ) { arr [ i ] = 1 ; } sum = getMinSum ( 0 , arr , primePow ) ; } return sum ; } let k = 3 , x = 210 ; document . write ( minimumSumWithGivenLCM ( k , x ) ) ;
Minimum removal of subsequences of distinct consecutive characters required to empty a given string | Function to count minimum operations required to make the string an empty string ; Stores count of 1 s by removing consecutive distinct subsequence ; Stores count of 0 s by removing consecutive distinct subsequence ; Stores length of str ; Traverse the string ; If current character is 0 ; Update cntOne ; Update cntZero ; If current character is 1 ; Update cntZero ; Update cntOne ; Driver code
function findMinOperationsReqEmpStr ( str ) { let cntOne = 0 ; let cntZero = 0 ; let N = str . length ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { if ( cntOne != 0 ) { cntOne -- ; } cntZero ++ ; } else { if ( cntZero != 0 ) { cntZero -- ; } cntOne ++ ; } } return ( cntOne + cntZero ) ; } let str = " " ; document . write ( findMinOperationsReqEmpStr ( str ) ) ;