codeparrot/xlcost-text-to-code huggingface dataset:
Modular Exponentiation ( Power in Modular Arithmetic ) | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Driver Code
function power ( x , y , p ) { let res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = $y / 2 y = y >> 1 ; x = ( x * x ) % p ; } return res ; } let x = 2 ; let y = 5 ; let p = 13 ; document . write ( " " + power ( x , y , p ) ) ;
Program to calculate angle between two N | Function to find the magnitude of the given vector ; Stores the final magnitude ; Traverse the array ; Return square root of magnitude ; Function to find the dot product of two vectors ; Stores dot product ; Traverse the array ; Return the product ; Stores dot product of two vectors ; Stores magnitude of vector A ; Stores magnitude of vector B ; Stores angle between given vectors ; Print the angle ; Given magnitude arrays ; Size of the array ; Function call to find the angle between two vectors
function magnitude ( arr , N ) { var magnitude = 0 ; for ( var i = 0 ; i < N ; i ++ ) magnitude += arr [ i ] * arr [ i ] ; return Math . sqrt ( magnitude ) ; } function dotProduct ( arr , brr , N ) { var product = 0 ; for ( var i = 0 ; i < N ; i ++ ) product = product + arr [ i ] * brr [ i ] ; return product ; } function angleBetweenVectors ( arr , brr , N ) { var dotProductOfVectors = dotProduct ( arr , brr , N ) ; var magnitudeOfA = magnitude ( arr , N ) ; var magnitudeOfB = magnitude ( brr , N ) ; var angle = dotProductOfVectors / ( magnitudeOfA * magnitudeOfB ) ; document . write ( angle . toFixed ( 6 ) ) ; } var arr = [ - 0.5 , - 2 , 1 ] ; var brr = [ - 1 , - 1 , 0.3 ] ; var N = arr . length ; angleBetweenVectors ( arr , brr , N ) ;
Sum of sides of largest and smallest child polygons possible from a given polygon | Function to find the sum of largest and smallest secondary polygons if possible ; Count edges of primary polygon ; Calculate edges present in the largest secondary polygon ; Given Exterior Angle
function secondary_polygon ( Angle ) { var edges_primary = 360 / Angle ; if ( edges_primary >= 6 ) { var edges_max_secondary = edges_primary / 2 ; document . write ( edges_max_secondary + 3 ) ; } else document . write ( " " ) ; } var Angle = 45 ; secondary_polygon ( Angle ) ;
Area of Circumcircle of an Equilateral Triangle using Median | Function to find the equation of circle whose center is ( x1 , y1 ) and the radius of circle is r ; Function to find the equation of circle which inscribes equilateral triangle of median M ; Function call to find the circle equation ; Driver code ; Function call
function circleArea ( r ) { var pi = 3.14159265358979323846 ; return ( pi * r * r ) ; } function findCircleAreaByMedian ( m ) { var r = 2 * m / 3 ; return circleArea ( r ) ; } var m = 3 ; document . write ( findCircleAreaByMedian ( m ) . toFixed ( 4 ) ) ;
Find the type of triangle from the given sides | Function to find the type of triangle with the help of sides ; Driver Code ; Function Call
function checkTypeOfTriangle ( a , b , c ) { let sqa = Math . floor ( Math . pow ( a , 2 ) ) ; let sqb = Math . floor ( Math . pow ( b , 2 ) ) ; let sqc = Math . floor ( Math . pow ( c , 2 ) ) ; if ( sqa == sqa + sqb sqb == sqa + sqc sqc == sqa + sqb ) { document . write ( " " ) ; } else if ( sqa > sqc + sqb sqb > sqa + sqc sqc > sqa + sqb ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let a , b , c ; a = 2 ; b = 2 ; c = 2 ; checkTypeOfTriangle ( a , b , c ) ;
Check whether the triangle is valid or not if angles are given | Function to check if sum of the three angles is 180 or not ; check condition ; Driver Code ; function calling and print output
function Valid ( a , b , c ) { if ( a + b + c == 180 && a != 0 && b != 0 && c != 0 ) return 1 ; else return 0 ; } var a = 60 , b = 40 , c = 80 ; if ( ( Valid ( a , b , c ) ) == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Area of the Largest Triangle inscribed in a Hexagon | Function to find the area of the triangle ; side cannot be negative ; area of the triangle ; Driver code
function trianglearea ( a ) { if ( a < 0 ) return - 1 ; var area = ( 3 * Math . sqrt ( 3 ) * Math . pow ( a , 2 ) ) / 4 ; return area . toFixed ( 4 ) ; } var a = 6 ; document . write ( trianglearea ( a ) ) ;
Equation of ellipse from its focus , directrix , and eccentricity | Function to find equation of ellipse . ; Driver Code
function equation_ellipse ( x1 , y1 , a , b , c , e ) { var t = a * a + b * b ; var a1 = t - e * ( a * a ) ; var b1 = t - e * ( b * b ) ; var c1 = ( - 2 * t * x1 ) - ( 2 * e * c * a ) ; var d1 = ( - 2 * t * y1 ) - ( 2 * e * c * b ) ; var e1 = - 2 * e * a * b ; var f1 = ( - e * c * c ) + ( t * x1 * x1 ) + ( t * y1 * y1 ) ; document . write ( " " + " " ) ; document . write ( a1 + " " + b1 + " " + c1 + " " + d1 + " " + e1 + " " + f1 + " " ) ; } var x1 = 1 , y1 = 1 , a = 1 , b = - 1 , c = 3 , e = 0.5 * 0.5 ; equation_ellipse ( x1 , y1 , a , b , c , e ) ;
Create loops of even and odd values in a binary tree | Utility function to create a new node ; preorder traversal to place the node pointer in the respective even_ptrs or odd_ptrs list ; place node ptr in even_ptrs list if node contains even number ; else place node ptr in odd_ptrs list ; function to create the even and odd loops ; forming even loop ; for the last element ; Similarly forming odd loop ; traversing the loop from any random node in the loop ; Binary tree formation ; traversing odd loop from any random odd node ; traversing even loop from any random even node
class Node { constructor ( data ) { this . data = data ; this . left = this . right = this . abtr = null ; } } let even_ptrs = [ ] ; let odd_ptrs = [ ] ; function preorderTraversal ( root ) { if ( root == null ) return ; if ( root . data % 2 == 0 ) ( even_ptrs ) . push ( root ) ; else ( odd_ptrs ) . push ( root ) ; preorderTraversal ( root . left ) ; preorderTraversal ( root . right ) ; } function createLoops ( root ) { preorderTraversal ( root ) ; let i ; for ( i = 1 ; i < even_ptrs . length ; i ++ ) even_ptrs [ i - 1 ] . abtr = even_ptrs [ i ] ; even_ptrs [ i - 1 ] . abtr = even_ptrs [ 0 ] ; for ( i = 1 ; i < odd_ptrs . length ; i ++ ) odd_ptrs [ i - 1 ] . abtr = odd_ptrs [ i ] ; odd_ptrs [ i - 1 ] . abtr = odd_ptrs [ 0 ] ; } function traverseLoop ( start ) { let curr = start ; do { document . write ( curr . data + " " ) ; curr = curr . abtr ; } while ( curr != start ) ; } let root = null ; 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 ) ; createLoops ( root ) ; document . write ( " " ) ; traverseLoop ( root . right ) ; document . write ( " " ) ; traverseLoop ( root . left ) ;
Area of circle which is inscribed in equilateral triangle | javascript program to find the area of circle which is inscribed in equilateral triangle ; Function return the area of circle inscribed in equilateral triangle ; Driver code
let PI = 3.14 ; function circle_inscribed ( a ) { return PI * ( a * a ) / 12 ; } let a = 4 ; document . write ( circle_inscribed ( a ) . toFixed ( 5 ) ) ;
Program to find the Volume of an irregular tetrahedron | Function to find the volume ; Steps to calculate volume of a Tetrahedron using formula ; Edge lengths
function findVolume ( u , v , w , U , V , W , b ) { let uPow = Math . pow ( u , 2 ) ; let vPow = Math . pow ( v , 2 ) ; let wPow = Math . pow ( w , 2 ) ; let UPow = Math . pow ( U , 2 ) ; let VPow = Math . pow ( V , 2 ) ; let WPow = Math . pow ( W , 2 ) ; let a = 4 * ( uPow * vPow * wPow ) - uPow * Math . pow ( ( vPow + wPow - UPow ) , 2 ) - vPow * Math . pow ( ( wPow + uPow - VPow ) , 2 ) - wPow * Math . pow ( ( uPow + vPow - WPow ) , 2 ) + ( vPow + wPow - UPow ) * ( wPow + uPow - VPow ) * ( uPow + vPow - WPow ) ; let vol = Math . sqrt ( a ) ; vol /= b ; document . write ( vol . toFixed ( 4 ) ) ; } let u = 1000 , v = 1000 , w = 1000 ; let U = 3 , V = 4 , W = 5 ; let b = 12 ; findVolume ( u , v , w , U , V , W , b ) ;
Check if it is possible to create a polygon with a given angle | Function to check whether it is possible to make a regular polygon with a given angle . ; N denotes the number of sides of polygons possible ; Driver code ; function to print the required answer
function makePolygon ( a ) { var n = parseFloat ( 360 / ( 180 - a ) ) ; if ( n === parseInt ( n ) ) document . write ( " " ) ; else document . write ( " " ) ; } var a = 90 ; makePolygon ( a ) ;
Finding Quadrant of a Coordinate with respect to a Circle | Thus function returns the quadrant number ; Coincides with center ; Outside circle ; 1 st quadrant ; 2 nd quadrant ; 3 rd quadrant ; 4 th quadrant ; Coordinates of centre ; Radius of circle ; Coordinates of the given point
function getQuadrant ( X , Y , R , PX , PY ) { if ( PX == X && PY == Y ) return 0 ; let val = Math . pow ( ( PX - X ) , 2 ) + Math . pow ( ( PY - Y ) , 2 ) ; if ( val > Math . pow ( R , 2 ) ) return - 1 ; if ( PX > X && PY >= Y ) return 1 ; if ( PX <= X && PY > Y ) return 2 ; if ( PX < X && PY <= Y ) return 3 ; if ( PX >= X && PY < Y ) return 4 ; } let X = 0 , Y = 3 ; let R = 2 ; let PX = 1 , PY = 4 ; let ans = getQuadrant ( X , Y , R , PX , PY ) ; if ( ans == - 1 ) document . write ( " " + " " ) ; else if ( ans == 0 ) document . write ( " " + " " ) ; else document . write ( ans + " " + " " ) ;
Hexadecagonal number | Function to calculate hexadecagonal number ; Driver code
function hexadecagonalNum ( n ) { return ( ( 14 * n * n ) - 12 * n ) / 2 ; } var n = 5 ; document . write ( n + " " + " " + hexadecagonalNum ( n ) + " " ) ; n = 9 ; document . write ( n + " " + " " + hexadecagonalNum ( n ) ) ;
Maximum points of intersection n circles | Returns maximum number of intersections ; Driver code
function intersection ( n ) { return n * ( n - 1 ) ; } document . write ( intersection ( 3 ) ) ;
Find the perimeter of a cylinder | Function to calculate perimeter ; Driver Function
function perimeter ( diameter , height ) { return 2 * ( diameter + height ) ; } let diameter = 5 ; let height = 10 ; document . write ( " " + perimeter ( diameter , height ) + " " ) ;
Find all possible coordinates of parallelogram | Coordinates of A ; Coordinates of B ; Coordinates of C
let ax = 5 , ay = 0 ; let bx = 1 , by = 1 ; let cx = 2 , cy = 5 ; document . write ( ax + bx - cx + " " + ( ay + by - cy ) + " " ) ; document . write ( ax + cx - bx + " " + ( ay + cy - by ) + " " ) ; document . write ( cx + bx - ax + " " + ( cy + by - ax ) + " " ) ;
Check whether a given point lies inside a rectangle or not | A utility function to calculate area of triangle formed by ( x1 , y1 ) , ( x2 , y2 ) and ( x3 , y3 ) ; A function to check whether point P ( x , y ) lies inside the rectangle formed by A ( x1 , y1 ) , B ( x2 , y2 ) , C ( x3 , y3 ) and D ( x4 , y4 ) ; Calculate area of rectangle ABCD ; Calculate area of triangle PAB ; Calculate area of triangle PBC ; Calculate area of triangle PCD ; Calculate area of triangle PAD ; Check if sum of A1 , A2 , A3 and A4 is same as A ; Let us check whether the point P ( 10 , 15 ) lies inside the rectangle formed by A ( 0 , 10 ) , B ( 10 , 0 ) C ( 0 , - 10 ) D ( - 10 , 0 )
function area ( x1 , y1 , x2 , y2 , x3 , y3 ) { return Math . abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2.0 ) ; } function check ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 , x , y ) { let A = area ( x1 , y1 , x2 , y2 , x3 , y3 ) + area ( x1 , y1 , x4 , y4 , x3 , y3 ) ; let A1 = area ( x , y , x1 , y1 , x2 , y2 ) ; let A2 = area ( x , y , x2 , y2 , x3 , y3 ) ; let A3 = area ( x , y , x3 , y3 , x4 , y4 ) ; let A4 = area ( x , y , x1 , y1 , x4 , y4 ) ; return ( A == A1 + A2 + A3 + A4 ) ; } if ( check ( 0 , 10 , 10 , 0 , 0 , - 10 , - 10 , 0 , 10 , 15 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Pizza cut problem ( Or Circle Division by Lines ) | Function for finding maximum pieces with n cuts . ; Driver Code
function findMaximumPieces ( n ) { return 1 + n * ( n + 1 ) / 2 ; } document . write ( findMaximumPieces ( 3 ) ) ;
Optimum location of point to minimize total distance | A JavaScript program to find optimum location and total cost ; structure defining a point ; structure defining a line of ax + by + c = 0 form ; method to get distance of point ( x , y ) from point p ; Utility method to compute total distance all points when choose point on given line has x - coordinate value as X ; calculating Y of chosen point by line equation ; Utility method to find minimum total distance ; loop until difference between low and high become less than EPS ; mid1 and mid2 are representative x co - ordiantes of search space ; if mid2 point gives more total distance , skip third part ; if mid1 point gives more total distance , skip first part ; compute optimum distance cost by sending average of low and high as X ; method to find optimum cost ; converting 2D array input to point array ; Driver Code
function sq ( x ) { return x * x ; } let EPS = ( 1e-6 ) + 1 ; let N = 5 ; class point { constructor ( x , y ) { this . x = x ; this . y = y ; } } class line { constructor ( a , b , c ) { this . a = a ; this . b = b ; this . c = c ; } } function dist ( x , y , p ) { return Math . sqrt ( sq ( x - p . x ) + sq ( y - p . y ) ) ; } function compute ( p , n , l , X ) { let res = 0 ; let Y = - 1 * ( l . c + l . a * X ) / l . b ; for ( let i = 0 ; i < n ; i ++ ) res += dist ( X , Y , p [ i ] ) ; return res ; } function findOptimumCostUtil ( p , n , l ) { let low = - 1e6 ; let high = 1e6 ; while ( ( high - low ) > EPS ) { let mid1 = low + ( high - low ) / 3 ; let mid2 = high - ( high - low ) / 3 ; let dist1 = compute ( p , n , l , mid1 ) ; let dist2 = compute ( p , n , l , mid2 ) ; if ( dist1 < dist2 ) high = mid2 ; else low = mid1 ; } return compute ( p , n , l , ( low + high ) / 2 ) ; } function findOptimumCost ( points , l ) { let p = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) p [ i ] = new point ( points [ i ] [ 0 ] , points [ i ] [ 1 ] ) ; return findOptimumCostUtil ( p , N , l ) ; } let l = new line ( 1 , - 1 , - 3 ) ; let points = [ [ - 3 , - 2 ] , [ - 1 , 0 ] , [ - 1 , 2 ] , [ 1 , 2 ] , [ 3 , 4 ] ] ; document . write ( findOptimumCost ( points , l ) ) ;
Find all duplicate and missing numbers in given permutation array of 1 to N | Function to find the duplicate and the missing elements over the range [ 1 , N ] ; Stores the missing and duplicate numbers in the array arr [ ] ; Traverse the given array arr [ ] ; Check if the current element is not same as the element at index arr [ i ] - 1 , then swap ; Otherwise , increment the index ; Traverse the array again ; If the element is not at its correct position ; Stores the missing and the duplicate elements ; Print the Missing Number ; Print the Duplicate Number ; Driver code
function findElements ( arr , N ) { let i = 0 ; let missing = [ ] ; let duplicate = new Set ( ) ; while ( i != N ) { if ( arr [ i ] != arr [ arr [ i ] - 1 ] ) { t = arr [ i ] ; arr [ i ] = arr [ arr [ i ] - 1 ] ; arr [ t - 1 ] = t ; } else { i += 1 ; } } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != i + 1 ) { missing . push ( i + 1 ) ; duplicate . add ( arr [ i ] ) ; } } document . write ( " " ) ; for ( it of missing ) document . write ( it + " " ) ; document . write ( " " ) ; for ( let it of [ ... duplicate ] ) document . write ( it + " " ) ; } let arr = [ 1 , 2 , 2 , 2 , 4 , 5 , 7 ] ; let N = arr . length ; findElements ( arr , N ) ;
Height of Factor Tree for a given number | Function to find the height of the Factor Tree of the integer N ; Stores the height of Factor Tree ; Loop to iterate over values of N ; Stores if there exist a factor of N or not ; Loop to find the smallest factor of N ; If i is a factor of N ; Increment the height ; If there are no factors of N i . e , N is prime , break loop ; Return Answer ; Driver Code
function factorTree ( N ) { let height = 0 ; while ( N > 1 ) { let flag = false ; for ( let i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { N = Math . floor ( N / i ) ; flag = true ; break ; } } height ++ ; if ( ! flag ) { break ; } } return height ; } let N = 48 ; document . write ( factorTree ( N ) ) ;
Maximize the largest number K such that bitwise and of K till N is 0 | Function to find maximum value of k which makes bitwise AND zero . ; Finding the power less than N ; Driver Code
function findMaxK ( N ) { var p = Math . log ( N ) / Math . log ( 2 ) ; return parseInt ( Math . pow ( 2 , p ) ) ; } var N = 5 ; document . write ( findMaxK ( N ) - 1 ) ;
Count of Ks in the Array for a given range of indices after array updates for Q queries | Javascript program for the above approach ; Function to build the segment tree ; Base case ; Since the count of zero is required set leaf node as 1 ; If the value in array is not zero , store 0 in the leaf node ; Find the mid ; Recursive call for left subtree ; Recursive call for right subtree ; Parent nodes contains the count of zero in range tl to tr ; Function to find the count of 0 s in range l to r ; Base Case ; Case when no two segment are combining ; Finding the mid ; When it is required to combine left subtree and right subtree to get the range l to r ; Function that updates the segment tree nodes ; Base Case ; If array element is 0 ; If array element is not 0 ; Otherwise ; Find the mid ; Update the tree or count which is stored in parent node ; Function to solve all the queries ; When query type is 1 ; When query type is 2 ; Driver Code
let a ; let seg_tree ; let query ; function build_tree ( v , tl , tr ) { if ( tl != tr ) { if ( a [ tl ] == 0 ) seg_tree [ v ] = 1 ; else seg_tree [ v ] = 0 ; } else { let tm = ( tl + tr ) / 2 ; build_tree ( v * 2 , tl , tm ) ; build_tree ( v * 2 + 1 , tm + 1 , tr ) ; seg_tree [ v ] = seg_tree [ v * 2 ] + seg_tree [ v * 2 + 1 ] ; } } function frequency_zero ( v , tl , tr , l , r ) { if ( l > r ) return 0 ; if ( l == tl && r == tr ) { return seg_tree [ v ] ; } let tm = ( tl + tr ) / 2 ; return frequency_zero ( v * 2 , tl , tm , l , Math . min ( r , tm ) ) + frequency_zero ( v * 2 + 1 , tm + 1 , tr , Math . max ( l , tm + 1 ) , r ) ; } function update ( v , tl , tr , pos , new_val ) { if ( tl == tr ) { if ( new_val == 0 ) seg_tree [ v ] = 1 ; else seg_tree [ v ] = 0 ; } else { let tm = ( tl + tr ) / 2 ; if ( pos <= tm ) update ( v * 2 , tl , tm , pos , new_val ) ; else update ( v * 2 + 1 , tm + 1 , tr , pos , new_val ) ; seg_tree [ v ] = seg_tree [ v * 2 ] + seg_tree [ v * 2 + 1 ] ; } } function solve ( n , q ) { let qu = [ 5 , 3 , 6 ] ; seg_tree = new Array ( 4 * n + 1 ) ; seg_tree . fill ( 0 ) ; build_tree ( 1 , 0 , n - 1 ) ; for ( let i = 0 ; i < qu . length ; i ++ ) { document . write ( qu [ i ] + " " ) ; } for ( let i = q ; i < q ; i ++ ) { if ( query [ i - 1 ] [ 0 ] == 1 ) { let l = query [ i - 1 ] [ 1 ] ; let r = query [ i - 1 ] [ 2 ] ; document . write ( frequency_zero ( 1 , 0 , n - 1 , l , r ) + " " ) ; } else { a [ query [ i - 1 ] [ 1 ] ] = query [ i - 1 ] [ 2 ] ; let pos = query [ i - 1 ] [ 1 ] ; let new_val = query [ i - 1 ] [ 2 ] ; update ( 1 , 0 , n - 1 , pos , new_val ) ; } } } a = [ 9 , 5 , 7 , 6 , 9 , 0 , 0 , 0 , 0 , 5 , 6 , 7 , 3 , 9 , 0 , 7 , 0 , 9 , 0 ] ; let Q = 5 ; query = [ [ 1 , 5 , 14 ] , [ 2 , 6 , 1 ] , [ 1 , 0 , 8 ] , [ 2 , 13 , 0 ] , [ 1 , 6 , 18 ] ] ; let N = a . length ; solve ( N , Q ) ;
A Space Optimized DP solution for 0 | val [ ] is for storing maximum profit for each weight wt [ ] is for storing weights n number of item W maximum capacity of bag mat [ 2 ] [ W + 1 ] to store final result ; matrix to store final result ; iterate through all items ; while ( i < n ) one by one traverse each element ; if i is odd that mean till now we have odd number of elements so we store result in 1 th indexed row ; while ( ++ j <= W ) check for each value ; if ( wt [ i ] <= j ) include element ; } else exclude element ; if i is even that means till now we have even number of elements so we store result in 0 th indexed row ; Return mat [ 0 ] [ W ] if n is odd , else mat [ 1 ] [ W ]
function KnapSack ( val , wt , n , W ) { let mat = new Array ( 2 ) ; for ( let i = 0 ; i < 2 ; i ++ ) { mat [ i ] = new Array ( W + 1 ) ; } for ( let i = 0 ; i < 2 ; i ++ ) { for ( let j = 0 ; j < W + 1 ; j ++ ) { mat [ i ] [ j ] = 0 ; } } let i = 0 ; { if ( i % 2 != 0 ) { { { mat [ 1 ] [ j ] = Math . max ( val [ i ] + mat [ 0 ] [ j - wt [ i ] ] , mat [ 0 ] [ j ] ) ; { mat [ 1 ] [ j ] = mat [ 0 ] [ j ] ; } } } else { while ( ++ j <= W ) { if ( wt [ i ] <= j ) { mat [ 0 ] [ j ] = Math . max ( val [ i ] + mat [ 1 ] [ j - wt [ i ] ] , mat [ 1 ] [ j ] ) ; } else { mat [ 0 ] [ j ] = mat [ 1 ] [ j ] ; } } } i ++ ; } return ( n % 2 != 0 ) ? mat [ 0 ] [ W ] : mat [ 1 ] [ W ] ; } let val = [ 7 , 8 , 4 ] ; let wt = [ 3 , 8 , 6 ] ; let W = 10 , n = 3 ; document . write ( KnapSack ( val , wt , n , W ) ) ;
Check if all 3 Candy bags can be emptied by removing 2 candies from any one bag and 1 from the other two repeatedly | Javascript code for the above approach ; If total candies are not multiple of 4 then its not possible to be left with 0 candies ; If minimum candies of three bags are less than number of operations required then the task is not possible ; Driver code
function can_empty ( a , b , c ) { if ( ( a + b + c ) % 4 != 0 ) return false ; else { let m = Math . min ( a , Math . min ( b , c ) ) ; if ( m < Math . floor ( ( a + b + c ) / 4 ) ) return false ; } return true ; } let a = 4 , b = 2 , c = 2 ; document . write ( can_empty ( a , b , c ) ? " " : " " ) ; document . write ( " " ) ; ( a = 3 ) , ( b = 4 ) , ( c = 2 ) ; document . write ( can_empty ( a , b , c ) ? " " : " " ) ;
Difference between maximum and minimum average of all K | Function to find the difference between averages of the maximum and the minimum subarrays of length k ; Stores min and max sum ; Iterate through starting points ; Sum up next K elements ; Update max and min moving sum ; Return the difference between max and min average ; Given Input ; Function Call
function Avgdifference ( arr , N , K ) { let min = 1000000 , max = - 1 ; for ( let i = 0 ; i <= N - K ; i ++ ) { let sum = 0 ; for ( let j = 0 ; j < K ; j ++ ) { sum += arr [ i + j ] ; } if ( min > sum ) min = sum ; if ( max < sum ) max = sum ; } return ( max - min ) / K ; } let arr = [ 3 , 8 , 9 , 15 ] ; let N = arr . length ; let K = 2 ; document . write ( Avgdifference ( arr , N , K ) ) ;
Maximum range length such that A [ i ] is maximum in given range for all i from [ 1 , N ] | Function to find maximum range for each i such that arr [ i ] is max in range ; Vector to store the left and right index for each i such that left [ i ] > arr [ i ] and right [ i ] > arr [ i ] ; Traverse the array ; While s . top ( ) [ 0 ] < a [ i ] remove the top element from the stack ; Modify left [ i ] ; Clear the stack ; Traverse the array to find right [ i ] for each i ; While s . top ( ) [ 0 ] < a [ i ] remove the top element from the stack ; Modify right [ i ] ; Print the value range for each i ; Given Input ; Function Call
function MaxRange ( A , n ) { let left = new Array ( n ) . fill ( 0 ) , right = new Array ( n ) . fill ( 0 ) ; let s = [ ] ; s . push ( [ Number . MAX_SAFE_INTEGER , - 1 ] ) ; for ( let i = 0 ; i < n ; i ++ ) { while ( s [ s . length - 1 ] [ 0 ] < A [ i ] ) s . pop ( ) ; left [ i ] = s [ s . length - 1 ] [ 1 ] ; s . push ( [ A [ i ] , i ] ) ; } while ( s . length ) s . pop ( ) ; s . push ( [ Number . MAX_SAFE_INTEGER , n ] ) ; for ( let i = n - 1 ; i >= 0 ; i -- ) { while ( s [ s . length - 1 ] [ 0 ] < A [ i ] ) s . pop ( ) ; right [ i ] = s [ s . length - 1 ] [ 1 ] ; s . push ( [ A [ i ] , i ] ) ; } for ( let i = 0 ; i < n ; i ++ ) { document . write ( left [ i ] + 1 + " " ) document . write ( right [ i ] - 1 + " " ) } } let arr = [ 1 , 3 , 2 ] ; let n = arr . length ; MaxRange ( arr , n ) ;