codeparrot/xlcost-text-to-code huggingface dataset:
Rat in a Maze Problem when movement in all possible directions is allowed | Vector to store all the possible paths ; Function returns true if the move taken is valid else it will return false . ; Function to print all the possible paths from ( 0 , 0 ) to ( n - 1 , n - 1 ) . ; This will check the initial point ( i . e . ( 0 , 0 ) ) to start the paths . ; If reach the last cell ( n - 1 , n - 1 ) then store the path and return ; Mark the cell as visited ; Check if downward move is valid ; Check if the left move is valid ; Check if the right move is valid ; Check if the upper move is valid ; Mark the cell as unvisited for other possible paths ; Function to store and print all the valid paths ; Call the utility function to find the valid paths ; Print all possible paths ; Driver code
let possiblePaths = [ ] ; let path = " " ; let MAX = 5 ; function isSafe ( row , col , m , n , visited ) { if ( row == - 1 row == n col == - 1 col == n visited [ row ] [ col ] m [ row ] [ col ] == 0 ) return false ; return true ; } function printPathUtil ( row , col , m , n , visited ) { if ( row == - 1 row == n col == - 1 col == n visited [ row ] [ col ] m [ row ] [ col ] == 0 ) return ; if ( row == n - 1 && col == n - 1 ) { possiblePaths . push ( path ) ; return ; } visited [ row ] [ col ] = true ; if ( isSafe ( row + 1 , col , m , n , visited ) ) { path += ' ' ; printPathUtil ( row + 1 , col , m , n , visited ) ; path = path . substring ( 0 , path . length - 1 ) ; } if ( isSafe ( row , col - 1 , m , n , visited ) ) { path += ' ' ; printPathUtil ( row , col - 1 , m , n , visited ) ; path = path . substring ( 0 , path . length - 1 ) ; } if ( isSafe ( row , col + 1 , m , n , visited ) ) { path += ' ' ; printPathUtil ( row , col + 1 , m , n , visited ) ; path = path . substring ( 0 , path . length - 1 ) ; } if ( isSafe ( row - 1 , col , m , n , visited ) ) { path += ' ' ; printPathUtil ( row - 1 , col , m , n , visited ) ; path = path . substring ( 0 , path . length - 1 ) ; } visited [ row ] [ col ] = false ; } function printPath ( m , n ) { let visited = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { visited [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) visited [ i ] [ j ] = false ; } printPathUtil ( 0 , 0 , m , n , visited ) ; for ( let i = 0 ; i < possiblePaths . length ; i ++ ) document . write ( possiblePaths [ i ] + " " ) ; } let m = [ [ 1 , 0 , 0 , 0 , 0 ] , [ 1 , 1 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 1 ] ] ; let n = m . length ; printPath ( m , n ) ;
N Queen in O ( n ) space | JavaScript program to for n Queen placement ; Function to check queens placement ; Helper Function to check if queen can be placed ; Function to display placed queen ; Driver code
function breakLine ( ) { document . write ( " " ) ; document . write ( " " ) ; document . write ( " " ) ; } let MAX = 10 ; let arr = [ ] ; let no = 0 ; function nQueens ( k , n ) { for ( let i = 1 ; i <= n ; i ++ ) { if ( canPlace ( k , i ) ) { arr [ k ] = i ; if ( k == n ) { display ( n ) ; } else { nQueens ( k + 1 , n ) ; } } } } function canPlace ( k , i ) { for ( let j = 1 ; j <= k - 1 ; j ++ ) { if ( arr [ j ] == i || ( Math . abs ( arr [ j ] - i ) == Math . abs ( j - k ) ) ) { return false ; } } return true ; } function display ( n ) { breakLine ( ) ; document . write ( " " + ++ no ) ; breakLine ( ) ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= n ; j ++ ) { if ( arr [ i ] != j ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } document . write ( " " ) ; } breakLine ( ) ; } let n = 4 ; nQueens ( 1 , n ) ;
Collect all coins in minimum number of steps | recursive method to collect coins from height array l to r , with height h already collected ; if l is more than r , no steps needed ; loop over heights to get minimum height index ; choose minimum from , 1 ) collecting coins using all vertical lines ( total r - l ) 2 ) collecting coins using lower horizontal lines and recursively on left and right segments ; method returns minimum number of step to collect coin from stack , with height in height [ ] array ; Driver program to test above function
function minStepsRecur ( height , l , r , h ) { if ( l >= r ) return 0 ; let m = l ; for ( let i = l ; i < r ; i ++ ) if ( height [ i ] < height [ m ] ) m = i ; return Math . min ( r - l , minStepsRecur ( height , l , m , height [ m ] ) + minStepsRecur ( height , m + 1 , r , height [ m ] ) + height [ m ] - h ) ; } function minSteps ( height , N ) { return minStepsRecur ( height , 0 , N , 0 ) ; } let height = [ 2 , 1 , 2 , 5 , 1 ] ; let N = height . length ; document . write ( minSteps ( height , N ) ) ;
Count of Unique Direct Path Between N Points On a Plane | Function to count the total number of direct paths ; Driver Code
function countDirectPath ( N ) { return N + Math . floor ( ( N * ( N - 3 ) ) / 2 ) ; } let N = 5 ; document . write ( countDirectPath ( N ) ) ;
Triacontagon Number | Finding the nth triacontagonal number ; Driver code
function triacontagonalNum ( n ) { return ( 28 * n * n - 26 * n ) / 2 ; } var n = 3 ; document . write ( " " + triacontagonalNum ( n ) ) ;
Hexacontagon Number | Finding the nth hexacontagon number ; Driver code
function hexacontagonNum ( n ) { return ( 58 * n * n - 56 * n ) / 2 ; } var n = 3 ; document . write ( " " + hexacontagonNum ( n ) ) ;
Enneacontagon Number | Finding the nth enneacontagon Number ; Driver Code
function enneacontagonNum ( n ) { return ( 88 * n * n - 86 * n ) / 2 ; } var n = 3 ; document . write ( " " + enneacontagonNum ( n ) ) ;
Triacontakaidigon Number | Finding the nth triacontakaidigon Number ; Driver code
function triacontakaidigonNum ( n ) { return ( 30 * n * n - 28 * n ) / 2 ; } let n = 3 ; document . write ( " " + triacontakaidigonNum ( n ) ) ;
Icosihexagonal Number | Finding the nth Icosihexagonal Number ; Driver code
function IcosihexagonalNum ( n ) { return ( 24 * n * n - 22 * n ) / 2 ; } let n = 3 ; document . write ( " " + IcosihexagonalNum ( n ) ) ;
Icosikaioctagon or Icosioctagon Number | Finding the nth icosikaioctagonal number ; Driver code
function icosikaioctagonalNum ( n ) { return ( 26 * n * n - 24 * n ) / 2 ; } var n = 3 ; document . write ( " " + icosikaioctagonalNum ( n ) ) ;
Octacontagon Number | Finding the nth octacontagon Number ; Driver Code
function octacontagonNum ( n ) { return ( 78 * n * n - 76 * n ) / 2 ; } var n = 3 ; document . write ( " " + octacontagonNum ( n ) ) ;
Hectagon Number | Finding the nth hectagon Number ; Driver Code
function hectagonNum ( n ) { return ( 98 * n * n - 96 * n ) / 2 ; } var n = 3 ; document . write ( " " + hectagonNum ( n ) ) ;
Tetracontagon Number | Finding the nth tetracontagon Number ; Driver code
function tetracontagonNum ( n ) { return ( 38 * n * n - 36 * n ) / 2 ; } let n = 3 ; document . write ( " " + tetracontagonNum ( n ) ) ;
Perimeter of an Ellipse | Function to find perimeter of an ellipse . ; formula to find the Perimeter of an Ellipse . ; Display the result ; Driver code
function Perimeter ( a , b ) { var Perimeter ; Perimeter = 2 * 3.14 * Math . sqrt ( ( a * a + b * b ) / ( 2 * 1.0 ) ) ; document . write ( " " + Perimeter ) ; } var a = 3 , b = 2 ; Perimeter ( a , b ) ;
Biggest Reuleaux Triangle within A Square | Function to find the Area of the Reuleaux triangle ; Side cannot be negative ; Area of the Reuleaux triangle ; Driver code
function ReuleauxArea ( a ) { if ( a < 0 ) return - 1 ; var A = 0.70477 * Math . pow ( a , 2 ) ; return A ; } var a = 6 ; document . write ( ReuleauxArea ( a ) ) ;
Largest hexagon that can be inscribed within a square | Function to return the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code
function hexagonside ( a ) { if ( a < 0 ) return - 1 ; let x = 0.5176 * a ; return x ; } let a = 6 ; document . write ( hexagonside ( a ) + " " ) ;
Largest hexagon that can be inscribed within an equilateral triangle | Function to find the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code
function hexagonside ( a ) { if ( a < 0 ) return - 1 ; var x = a / 3 ; return x ; } var a = 6 ; document . write ( hexagonside ( a ) ) ;
Find middle point segment from given segment lengths | Function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; Driver code
function findSegment ( n , m , segment_length ) { let meet_point = ( 1.0 * n ) / 2.0 ; let sum = 0 ; let segment_number = 0 , i ; for ( i = 0 ; i < m ; i ++ ) { sum += segment_length [ i ] ; if ( sum == meet_point ) { segment_number = - 1 ; break ; } if ( sum > meet_point ) { segment_number = i + 1 ; break ; } } return segment_number ; } let n = 13 ; let m = 3 ; let segment_length = [ 3 , 2 , 8 ] ; let ans = findSegment ( n , m , segment_length ) ; document . write ( ans ) ;
Maximum points of intersection n lines | nC2 = ( n ) * ( n - 1 ) / 2 ; ; n is number of line
function countMaxIntersect ( n ) { return ( n ) * ( n - 1 ) / 2 ; } var n = 8 ; document . write ( countMaxIntersect ( n ) ) ;
Program to find volume and surface area of pentagonal prism | function for surface area ; function for VOlume ; Driver function
function surfaceArea ( a , b , h ) { return 5 * a * b + 5 * b * h ; } function volume ( b , h ) { return ( 5 * b * h ) / 2 ; } let a = 5 ; let b = 3 ; let h = 7 ; document . write ( " " + surfaceArea ( a , b , h ) + " " ) ; document . write ( " " + volume ( b , h ) ) ;
Check if a point is inside , outside or on the parabola | Function to check the point ; checking the equation of parabola with the given point ; driver code
function checkpoint ( h , k , x , y , a ) { var p = parseInt ( Math . pow ( ( y - k ) , 2 ) - 4 * a * ( x - h ) ) ; return p ; } var h = 0 , k = 0 , x = 2 , y = 1 , a = 4 ; if ( checkpoint ( h , k , x , y , a ) > 0 ) document . write ( " " ) ; else if ( checkpoint ( h , k , x , y , a ) == 0 ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a point is inside , outside or on the ellipse | Function to check the point ; checking the equation of ellipse with the given point ; Driver code
function checkpoint ( h , k , x , y , a , b ) { var p = ( parseInt ( Math . pow ( ( x - h ) , 2 ) ) / parseInt ( Math . pow ( a , 2 ) ) ) + ( parseInt ( Math . pow ( ( y - k ) , 2 ) ) / parseInt ( Math . pow ( b , 2 ) ) ) ; return p ; } var h = 0 , k = 0 , x = 2 , y = 1 , a = 4 , b = 5 ; if ( checkpoint ( h , k , x , y , a , b ) > 1 ) document . write ( " " ) ; else if ( checkpoint ( h , k , x , y , a , b ) == 1 ) document . write ( " " ) ; else document . write ( " " ) ;
Area of circle inscribed within rhombus | Function to find the area of the inscribed circle ; the diagonals cannot be negative ; area of the circle ; Driver code
function circlearea ( a , b ) { if ( a < 0 b < 0 ) return - 1 ; var A = ( ( 3.14 * Math . pow ( a , 2 ) * Math . pow ( b , 2 ) ) / ( 4 * ( Math . pow ( a , 2 ) + Math . pow ( b , 2 ) ) ) ) ; return A ; } var a = 8 , b = 10 ; document . write ( circlearea ( a , b ) . toFixed ( 4 ) ) ;
The biggest possible circle that can be inscribed in a rectangle | Function to find the area of the biggest circle ; the length and breadth cannot be negative ; area of the circle ; Driver code
function circlearea ( l , b ) { if ( l < 0 b < 0 ) return - 1 ; if ( l < b ) return ( 3.14 * Math . pow ( l / 2 , 2 ) ) ; else return ( 3.14 * Math . pow ( b / 2 , 2 ) ) ; } var l = 4 , b = 8 ; document . write ( circlearea ( l , b ) ) ;
Centered cube number | Function to find Centered cube number ; Formula to calculate nth Centered cube number & return it into main function . ; Driver Code
function centered_cube ( n ) { return ( 2 * n + 1 ) * ( n * n + n + 1 ) ; } let n = 3 ; document . write ( n + " " ) ; document . write ( centered_cube ( n ) ) ; document . write ( " " ) ; n = 10 ; document . write ( n + " " ) ; document . write ( centered_cube ( n ) ) ;
Find the center of the circle using endpoints of diameter | function to find the center of the circle ; Driver Function
function center ( x1 , x2 , y1 , y2 ) { document . write ( ( x1 + x2 ) / 2 + " " + ( y1 + y2 ) / 2 ) ; } let x1 = - 9 , y1 = 3 , x2 = 5 , y2 = - 7 ; center ( x1 , x2 , y1 , y2 ) ;
Program to calculate volume of Octahedron | utility Function ; Driver Function
function vol_of_octahedron ( side ) { return ( ( side * side * side ) * ( Math . sqrt ( 2 ) / 3 ) ) ; } let side = 3 ; document . write ( " " + vol_of_octahedron ( side ) . toFixed ( 4 ) ) ;
Program to calculate volume of Ellipsoid | Function to find the volume ; Driver Code
function volumeOfEllipsoid ( r1 , r2 , r3 ) { let pi = 3.14 ; return 1.33 * pi * r1 * r2 * r3 ; } let r1 = 2.3 , r2 = 3.4 , r3 = 5.7 ; document . write ( " " + volumeOfEllipsoid ( r1 , r2 , r3 ) . toFixed ( 2 ) ) ;
Program to calculate Area Of Octagon | Utility function ; Driver Code
function areaOctagon ( side ) { return ( 2 * ( 1 + Math . sqrt ( 2 ) ) * side * side ) ; } let side = 4 ; document . write ( " " + areaOctagon ( side ) + " " ) ;
Program for Volume and Surface Area of Cube | utility function ; Driver function
function areaCube ( a ) { return ( a * a * a ) ; } function surfaceCube ( a ) { return ( 6 * a * a ) ; } let a = 5 ; document . write ( " " + areaCube ( a ) + " " ) ; document . write ( " " + surfaceCube ( a ) ) ;
Find mirror image of a point in 2 | JavaScript function which finds coordinates of mirror image . This function return a pair of double ; Driver code to test above function
function mirrorImage ( a , b , c , x1 , y1 ) { var temp = ( - 2 * ( a * x1 + b * y1 + c ) ) / ( a * a + b * b ) ; var x = temp * a + x1 ; var y = temp * b + y1 ; return [ x , y ] ; } var a = - 1.0 ; var b = 1.0 ; var c = 0.0 ; var x1 = 1.0 ; var y1 = 0.0 ; var [ x , y ] = mirrorImage ( a , b , c , x1 , y1 ) ; document . write ( " " + x1 + " " + y1 + " " ) ; document . write ( " " + a + " " + b + " " + c + " " ) ; document . write ( " " + x + " " + y + " " ) ;
Minimum revolutions to move center of a circle to a target | Minimum revolutions to move center from ( x1 , y1 ) to ( x2 , y2 ) ; Driver code
function minRevolutions ( r , x1 , y1 , x2 , y2 ) { let d = Math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ; return Math . ceil ( d / ( 2 * r ) ) ; } let r = 2 , x1 = 0 , y1 = 0 , x2 = 0 , y2 = 4 ; document . write ( minRevolutions ( r , x1 , y1 , x2 , y2 ) ) ;
Find all sides of a right angled triangle from given hypotenuse and area | Set 1 | limit for float comparison ; Utility method to get area of right angle triangle , given base and hypotenuse ; Prints base and height of triangle using hypotenuse and area information ; maximum area will be obtained when base and height are equal ( = sqrt ( h * h / 2 ) ) ; if given area itself is larger than maxArea then no solution is possible ; binary search for base ; get height by pythagorean rule ; Driver Code
let eps = 1e-6 ; function getArea ( base , hypotenuse ) { let height = Math . sqrt ( hypotenuse * hypotenuse - base * base ) ; return 0.5 * base * height ; } function printRightAngleTriangle ( hypotenuse , area ) { let hsquare = hypotenuse * hypotenuse ; let sideForMaxArea = Math . sqrt ( hsquare / 2.0 ) ; let maxArea = getArea ( sideForMaxArea , hypotenuse ) ; if ( area > maxArea ) { document . write ( " " ) ; return ; } let low = 0.0 ; let high = sideForMaxArea ; let base = 0 ; while ( Math . abs ( high - low ) > eps ) { base = ( low + high ) / 2.0 ; if ( getArea ( base , hypotenuse ) >= area ) { high = base ; } else { low = base ; } } let height = Math . sqrt ( hsquare - base * base ) ; document . write ( Math . round ( base ) + " " + Math . round ( height ) ) ; } let hypotenuse = 5 ; let area = 6 ; printRightAngleTriangle ( hypotenuse , area ) ;
Circle and Lattice Points | Function to count Lattice points on a circle ; Initialize result as 4 for ( r , 0 ) , ( - r . 0 ) , ( 0 , r ) and ( 0 , - r ) ; Check every value that can be potential x ; Find a potential y ; checking whether square root is an integer or not . Count increments by 4 for four different quadrant values ; Driver code
function countLattice ( r ) { if ( r <= 0 ) return 0 ; var result = 4 ; for ( x = 1 ; x < r ; x ++ ) { var ySquare = r * r - x * x ; var y = parseInt ( Math . sqrt ( ySquare ) ) ; if ( y * y == ySquare ) result += 4 ; } return result ; } var r = 5 ; document . write ( countLattice ( r ) ) ;
Count of subarrays of size K with average at least M | Function to count the subarrays of size K having average at least M ; Stores the resultant count of subarray ; Stores the sum of subarrays of size K ; Add the values of first K elements to the sum ; Increment the count if the current subarray is valid ; Traverse the given array ; Find the updated sum ; Check if current subarray is valid or not ; Return the count of subarrays ; Driver Code
function countSubArrays ( arr , N , K , M ) { let count = 0 ; let sum = 0 ; for ( let i = 0 ; i < K ; i ++ ) { sum += arr [ i ] ; } if ( sum >= K * M ) count ++ ; for ( let i = K ; i < N ; i ++ ) { sum += ( arr [ i ] - arr [ i - K ] ) ; if ( sum >= K * M ) count ++ ; } return count ; } let arr = [ 3 , 6 , 3 , 2 , 1 , 3 , 9 ] ; let K = 2 , M = 4 ; let N = arr . length ; document . write ( countSubArrays ( arr , N , K , M ) ) ;
Count of Ks in the Array for a given range of indices after array updates for Q queries | Function to perform all the queries ; Stores the count of 0 s ; Count the number of 0 s for query of type 1 ; Update the array element for query of type 2 ; Driver Code
function performQueries ( n , q , k , arr , query ) { = for ( let i = 1 ; i <= q ; i ++ ) { let count = 0 ; if ( query [ i - 1 ] [ 0 ] == 1 ) { for ( let j = query [ i - 1 ] [ 1 ] ; j <= query [ i - 1 ] [ 2 ] ; j ++ ) { if ( arr [ j ] == k ) count ++ ; } document . write ( count + " " ) ; } else { arr [ query [ i - 1 ] [ 1 ] ] = query [ i - 1 ] [ 2 ] ; } } } let arr = [ 9 , 5 , 7 , 6 , 9 , 0 , 0 , 0 , 0 , 5 , 6 , 7 , 3 , 9 , 0 , 7 , 0 , 9 , 0 ] ; let Q = 5 ; let query = [ [ 1 , 5 , 14 ] , [ 2 , 6 , 1 ] , [ 1 , 0 , 8 ] , [ 2 , 13 , 0 ] , [ 1 , 6 , 18 ] , ] ; let N = arr . length ; let K = 0 ; performQueries ( N , Q , K , arr , query ) ;
Minimum count of consecutive integers till N whose bitwise AND is 0 with N | Function to count the minimum count of integers such that bitwise AND of that many consecutive elements is equal to 0 ; Stores the binary representation of N ; Stores the MSB bit ; Stores the count of numbers ; Return res ; Given Input ; Function Call
function count ( N ) { var a = N . toString ( 2 ) ; var m = a . length - 1 ; var res = N - ( Math . pow ( 2 , m ) - 1 ) ; return res ; } var N = 18 ; document . write ( count ( N ) ) ;
Find smallest value of K such that bitwise AND of numbers in range [ N , N | Function is to find the largest no which gives the sequence n & ( n - 1 ) & ( n - 2 ) & ... . . & ( n - k ) = 0. ; Since , we need the largest no , we start from n itself , till 0 ; Driver Code
function findSmallestNumK ( n ) { let cummAnd = n ; let i = n - 1 ; while ( cummAnd != 0 ) { cummAnd = cummAnd & i ; if ( cummAnd == 0 ) { return i ; } i -- ; } return - 1 ; } let N = 17 ; let lastNum = findSmallestNumK ( N ) ; let K = lastNum == - 1 ? lastNum : N - lastNum ; document . write ( K + " " ) ;
Program to find the value of P ( N + r ) for a polynomial of a degree N such that P ( i ) = 1 for 1 Ã ¢ â €°¤ i Ã ¢ â €°¤ N and P ( N + 1 ) = a | Function to calculate factorial of N ; Base Case ; Otherwise , recursively calculate the factorial ; Function to find the value of P ( n + r ) for polynomial P ( X ) ; Stores the value of k ; Store the required answer ; Iterate in the range [ 1 , N ] and multiply ( n + r - i ) with answer ; Add the constant value C as 1 ; Return the result ; Driver code
function fact ( n ) { if ( n == 1 n == 0 ) return 1 ; else return n * fact ( n - 1 ) ; } function findValue ( n , r , a ) { let k = ( a - 1 ) / fact ( n ) ; let answer = k ; for ( let i = 1 ; i < n + 1 ; i ++ ) answer = answer * ( n + r - i ) ; answer = answer + 1 ; return answer ; } let N = 1 ; let A = 2 ; let R = 3 ; document . write ( findValue ( N , R , A ) ) ;
Find winner in game of N balls , in which a player can remove any balls in range [ A , B ] in a single move | Function to find the winner of the game ; Stores sum of A and B ; If N is of the form m * ( A + B ) + y ; Otherwise , ; Input ; Function call
function NimGame ( N , A , B ) { let sum = A + B ; if ( N % sum <= A - 1 ) return " " ; else return " " ; } let N = 3 , A = 1 , B = 2 ; document . write ( NimGame ( N , A , B ) + " " ) ;
Find Nth number in a sequence which is not a multiple of a given number | Function to find Nth number not a multiple of A in range [ L , R ] ; Calculate the Nth no ; Check for the edge case ; Input parameters ; Function Call
function countNo ( A , N , L , R ) { var ans = L - 1 + N + Math . floor ( ( N - 1 ) / ( A - 1 ) ) ; if ( ans % A == 0 ) { ans = ans + 1 ; } document . write ( ans ) ; } var A = 5 , N = 10 , L = 4 , R = 20 ; countNo ( A , N , L , R ) ;
Count of paths in given Binary Tree with odd bitwise AND for Q queries | Function to count number of paths in binary tree such that bitwise AND of all nodes is Odd ; vector dp to store the count of bitwise odd paths till that vertex ; Precomputing for each value ; check for odd value ; Number of odd elements will be + 1 till the parent node ; For even case ; Since node is even Number of odd elements will be 0 ; Even value node will not contribute in answer hence dp [ i ] = previous answer ; Printing the answer for each query ; vector to store queries
function compute ( query ) { var v = Array ( 100001 ) . fill ( 0 ) ; var dp = Array ( 100001 ) . fill ( 0 ) ; v [ 1 ] = 1 , v [ 2 ] = 0 ; dp [ 1 ] = 0 , dp [ 2 ] = 0 ; for ( var i = 3 ; i < 100001 ; i ++ ) { if ( i % 2 != 0 ) { if ( parseInt ( i / 2 ) % 2 == 0 ) { v [ i ] = 1 ; dp [ i ] = dp [ i - 1 ] ; } else { v [ i ] = v [ parseInt ( i / 2 ) ] + 1 ; dp [ i ] = dp [ i - 1 ] + v [ i ] - 1 ; } } else { v [ i ] = 0 ; dp [ i ] = dp [ i - 1 ] ; } } query . forEach ( x => { document . write ( dp [ x ] + " " ) ; } ) ; } var query = [ 5 , 2 ] ; compute ( query ) ;
Number of cycles formed by joining vertices of n sided polygon at the center | Function to calculate number of cycles ; BigInteger is used here if N = 10 ^ 9 then multiply will result into value greater than 10 ^ 18 ; BigInteger multiply function ; Return the final result ; Given N ; Function Call
function findCycles ( N ) { let res = 0 ; let finalResult = 0 ; let val = 2 * N - 1 ; let s = val ; res = ( N - 1 ) * ( N - 2 ) ; finalResult = res + s ; return finalResult ; } let N = 5 ; document . write ( findCycles ( N ) ) ;
Split a given array into K subarrays minimizing the difference between their maximum and minimum | Function to find the subarray ; Add the difference to vectors ; Sort vector to find minimum k ; Initialize result ; Adding first k - 1 values ; Return the minimized sum ; Given array arr ; Given K ; Function Call
function find ( a , n , k ) { var v = [ ] ; for ( i = 1 ; i < n ; ++ i ) { v . push ( a [ i - 1 ] - a [ i ] ) ; } v . sort ( ( a , b ) => a - b ) ; var res = a [ n - 1 ] - a [ 0 ] ; for ( i = 0 ; i < k - 1 ; ++ i ) { res += v [ i ] ; } return res ; } var arr = [ 4 , 8 , 15 , 16 , 23 , 42 ] ; var N = arr . length ; var K = 3 ; document . write ( find ( arr , N , K ) + " " ) ;