codeparrot/xlcost-text-to-code huggingface dataset:
Minimum count of indices to be skipped for every index of Array to keep sum till that index at most T | Function to calculate minimum indices to be skipped so that sum till i remains smaller than T ; Store the sum of all indices before i ; Store the elements that can be skipped ; Traverse the array , A [ ] ; Store the total sum of elements that needs to be skipped ; Store the number of elements need to be removed ; Traverse from the back of map so as to take bigger elements first ; Update sum ; Update map with the current element ; Given Input ; Function Call
function skipIndices ( N , T , arr ) { let sum = 0 ; let count = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { let d = sum + arr [ i ] - T ; let k = 0 ; if ( d > 0 ) { for ( let u of [ ... count . keys ( ) ] . reverse ( ) ) { let j = u ; let x = j * count . get ( j ) ; if ( d <= x ) { k += Math . floor ( ( d + j - 1 ) / j ) ; break ; } k += count . get ( j ) ; d -= x ; } } sum += arr [ i ] ; if ( count . has ( arr [ i ] ) ) { count . set ( arr [ i ] , count . get ( i ) + 1 ) } else { count . set ( arr [ i ] , 1 ) } document . write ( k + " " ) ; } } let N = 7 ; let T = 15 ; let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; skipIndices ( N , T , arr ) ;
Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | Set 2 | Function to count the number of triplets from the range [ 1 , N - 1 ] having sum of all pairs divisible by K ; If K is even ; Otherwise ; Driver Code
function countTriplets ( N , K ) { if ( K % 2 == 0 ) { var x = parseInt ( N / K ) ; var y = parseInt ( ( N + ( K / 2 ) ) / K ) ; return x * x * x + y * y * y ; } else { var x = parseInt ( N / K ) ; return x * x * x ; } } var N = 2 , K = 2 ; document . write ( countTriplets ( N , K ) ) ;
Maximize count of 3 | Function to count the maximum number oaf palindrome subsequences of length 3 considering the same index only once ; Index of the string S ; Stores the frequency of every character ; Stores the pair of frequency containing same characters ; Number of subsequences having length 3 ; Counts the frequency ; Counts the pair of frequency ; Returns the minimum value ; Driver Code
function maxNumPalindrome ( S ) { let i = 0 ; let freq = new Array ( 26 ) . fill ( 0 ) ; let freqPair = 0 ; let len = ( S . length / 3 ) ; while ( i < S . length ) { freq [ S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; i ++ ; } for ( i = 0 ; i < 26 ; i ++ ) { freqPair += Math . floor ( freq [ i ] / 2 ) ; } return Math . min ( freqPair , len ) ; } let S = " " ; document . write ( maxNumPalindrome ( S ) + " " ) ;
Minimum Bipartite Groups | Function to find the height sizeof the current component with vertex s ; Visit the current Node ; Call DFS recursively to find the maximum height of current CC ; If the node is not visited then the height recursively for next element ; Function to find the minimum Groups ; Initialise with visited array ; To find the minimum groups ; Traverse all the non visited Node and calculate the height of the tree with current node as a head ; If the current is not visited therefore , we get another CC ; Return the minimum bipartite matching ; Function that adds the current edges in the given graph ; Drivers Code ; Adjacency List ; Adding edges to List
function height ( s , adj , visited ) { visited [ s ] = 1 ; var h = 0 ; adj [ s ] . forEach ( child => { if ( visited [ child ] == 0 ) { h = Math . max ( h , 1 + height ( child , adj , visited ) ) ; } } ) ; return h ; } function minimumGroups ( adj , N ) { var visited = Array ( N + 1 ) . fill ( 0 ) ; var groups = - 1000000000 ; for ( var i = 1 ; i <= N ; i ++ ) { if ( visited [ i ] == 0 ) { var comHeight ; comHeight = height ( i , adj , visited ) ; groups = Math . max ( groups , comHeight ) ; } } return groups ; } function addEdge ( adj , u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } var N = 5 ; var adj = Array . from ( Array ( N + 1 ) , ( ) => Array ( ) ) addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 3 , 2 ) ; addEdge ( adj , 4 , 3 ) ; document . write ( minimumGroups ( adj , N ) ) ;
Sum of subsets of all the subsets of an array | O ( 2 ^ N ) | To store the final ans ; Function to sum of all subsets of a given array ; Function to generate the subsets ; Base - case ; Finding the sum of all the subsets of the generated subset ; Recursively accepting and rejecting the current number ; Driver code
var ans = 0 ; function subsetSum ( c ) { var L = c . length ; var mul = parseInt ( Math . pow ( 2 , L - 1 ) ) ; for ( i = 0 ; i < c . length ; i ++ ) ans += c [ i ] * mul ; } function subsetGen ( arr , i , n , c ) { if ( i == n ) { subsetSum ( c ) ; return ; } subsetGen ( arr , i + 1 , n , c ) ; c . push ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n , c ) ; c . pop ( 0 ) ; } var arr = [ 1 , 1 ] ; var n = arr . length ; var c = [ ] ; subsetGen ( arr , 0 , n , c ) ; document . write ( ans ) ;
Find the maximum possible value of a [ i ] % a [ j ] over all pairs of i and j | Function that returns the second largest element in the array if exists , else 0 ; There must be at least two elements ; To store the maximum and the second maximum element from the array ; If current element is greater than first then update both first and second ; If arr [ i ] is in between first and second then update second ; No second maximum found ; Driver Code
function getMaxValue ( arr , arr_size ) { let i , first , second ; if ( arr_size < 2 ) { return 0 ; } first = second = Number . MIN_VALUE ; for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second && arr [ i ] != first ) { second = arr [ i ] ; } } if ( second == Number . MIN_VALUE ) { return 0 ; } else { return second ; } } let arr = [ 4 , 5 , 1 , 8 ] ; let n = arr . length ; document . write ( getMaxValue ( arr , n ) ) ;
Maximize the value of the given expression | Function to return the maximum result ; To store the count of negative integers ; Sum of all the three integers ; Product of all the three integers ; To store the smallest and the largest among all the three integers ; Calculate the count of negative integers ; Depending upon count of negatives ; When all three are positive integers ; For single negative integer ; For two negative integers ; For three negative integers ; Driver Code
function maximumResult ( a , b , c ) { let countOfNegative = 0 ; let sum = a + b + c ; let product = a * b * c ; let largest = Math . max ( a , Math . max ( b , c ) ) ; let smallest = Math . min ( a , Math . min ( b , c ) ) ; if ( a < 0 ) countOfNegative ++ ; if ( b < 0 ) countOfNegative ++ ; if ( c < 0 ) countOfNegative ++ ; switch ( countOfNegative ) { case 0 : return ( sum - largest ) * largest ; case 1 : return ( product / smallest ) + smallest ; case 2 : return ( product / largest ) + largest ; case 3 : return ( sum - smallest ) * smallest ; } } let a = - 2 , b = - 1 , c = - 4 ; document . write ( maximumResult ( a , b , c ) ) ;
Maximum students to pass after giving bonus to everybody and not exceeding 100 marks | Function to return the number of students that can pass ; maximum marks ; maximum bonus marks that can be given ; counting the number of students that can pass ; Driver code
function check ( n , marks ) { let x = Math . max ( ... marks ) ; let bonus = 100 - x ; let c = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( marks [ i ] + bonus >= 50 ) c += 1 ; } return c ; } let n = 5 ; let marks = [ 0 , 21 , 83 , 45 , 64 ] ; document . write ( check ( n , marks ) ) ;
Sum of first N natural numbers which are not powers of K | Function to return the sum of first n natural numbers which are not positive powers of k ; sum of first n natural numbers ; subtract all positive powers of k which are less than n ; next power of k ; Driver code
function find_sum ( n , k ) { let total_sum = ( n * ( n + 1 ) ) / 2 ; let power = k ; while ( power <= n ) { total_sum -= power ; power *= k ; } return total_sum ; } let n = 11 , k = 2 ; document . write ( find_sum ( n , k ) ) ;
Minimum number of operations required to delete all elements of the array | javascript implementation of the above approach ; function to find minimum operations ; sort array ; prepare hash of array ; Driver program
var MAX = 10000 ; var hashTable = Array ( MAX ) . fill ( 0 ) ; function minOperations ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; for ( var i = 0 ; i < n ; i ++ ) hashTable [ arr [ i ] ] ++ ; var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( hashTable [ arr [ i ] ] ) { for ( var j = i ; j < n ; j ++ ) if ( arr [ j ] % arr [ i ] == 0 ) hashTable [ arr [ j ] ] = 0 ; res ++ ; } } return res ; } var arr = [ 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 ] ; var n = arr . length ; document . write ( minOperations ( arr , n ) ) ;
Sorting array with reverse around middle | Javascript program to find possibility to sort by multiple subarray reverse operation ; making the copy of the original array ; sorting the copied array ; checking mirror image of elements of sorted copy array and equivalent element of original array ; driver code
function ifPossible ( arr , n ) { let copy = arr ; copy . sort ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! ( arr [ i ] == copy [ i ] ) && ! ( arr [ n - 1 - i ] == copy [ i ] ) ) return false ; } return true ; } let arr = [ 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 ] ; let n = arr . length ; if ( ifPossible ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ; ;
Program for Shortest Job First ( SJF ) scheduling | Set 2 ( Preemptive ) | Javascript program to implement Shortest Remaining Time First Shortest Remaining Time First ( SRTF ) ; this . pid = pid ; Process ID this . bt = bt ; Burst Time this . art = art ; Arrival Time ; Method to find the waiting time for all processes ; Copy the burst time into rt [ ] ; Process until all processes gets completed ; Find process with minimum remaining time among the processes that arrives till the current time ` ; Reduce remaining time by one ; Update minimum ; If a process gets completely executed ; Increment complete ; Find finish time of current process ; Calculate waiting time ; Increment time ; Method to calculate turn around time ; calculating turnaround time by adding bt [ i ] + wt [ i ] ; Method to calculate average time ; Function to find waiting time of all processes ; Function to find turn around time for all processes ; Display processes along with all details ; Calculate total waiting time and total turnaround time ; Driver Method
class Process { constructor ( pid , bt , art ) { } } function findWaitingTime ( proc , n , wt ) { let rt = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) rt [ i ] = proc [ i ] . bt ; let complete = 0 , t = 0 , minm = Number . MAX_VALUE ; let shortest = 0 , finish_time ; let check = false ; while ( complete != n ) { for ( let j = 0 ; j < n ; j ++ ) { if ( ( proc [ j ] . art <= t ) && ( rt [ j ] < minm ) && rt [ j ] > 0 ) { minm = rt [ j ] ; shortest = j ; check = true ; } } if ( check == false ) { t ++ ; continue ; } rt [ shortest ] -- ; minm = rt [ shortest ] ; if ( minm == 0 ) minm = Number . MAX_VALUE ; if ( rt [ shortest ] == 0 ) { complete ++ ; check = false ; finish_time = t + 1 ; wt [ shortest ] = finish_time - proc [ shortest ] . bt - proc [ shortest ] . art ; if ( wt [ shortest ] < 0 ) wt [ shortest ] = 0 ; } t ++ ; } } function findTurnAroundTime ( proc , n , wt , tat ) { for ( let i = 0 ; i < n ; i ++ ) tat [ i ] = proc [ i ] . bt + wt [ i ] ; } function findavgTime ( proc , n ) { let wt = new Array ( n ) , tat = new Array ( n ) ; let total_wt = 0 , total_tat = 0 ; findWaitingTime ( proc , n , wt ) ; findTurnAroundTime ( proc , n , wt , tat ) ; document . write ( " " + " " + " " + " " ) ; for ( let i = 0 ; i < n ; i ++ ) { total_wt = total_wt + wt [ i ] ; total_tat = total_tat + tat [ i ] ; document . write ( " " + proc [ i ] . pid + " " + proc [ i ] . bt + " " + wt [ i ] + " " + tat [ i ] + " " ) ; } document . write ( " " + total_wt / n + " " ) ; document . write ( " " + total_tat / n + " " ) ; } let proc = [ new Process ( 1 , 6 , 1 ) , new Process ( 2 , 8 , 1 ) , new Process ( 3 , 7 , 2 ) , new Process ( 4 , 3 , 3 ) ] ; findavgTime ( proc , proc . length ) ;
Minimum deletions in Array to make difference of adjacent elements non | The maximum value of A [ i ] ; Function for finding minimum deletions so that the array becomes non - decreasing and the difference between adjacent elements is also non - decreasing ; Initializing the dp table and setting all values to 0 ; Find the maximum size valid set that can be taken and then subtract its size from N to get minimum number of deletions ; When selecting only current element and deleting all elements from 0 to i - 1 inclusive ; If this is true moving from index j to i is possible ; Iterate over all elements from 0 to diff and find the max ; Take the max set size from dp [ N - 1 ] [ 0 ] to dp [ N - 1 ] [ MAX ] ; Input ; Function call
let MAX = 100001 function minimumDeletions ( A , N ) { let dp = new Array ( N ) . fill ( 0 ) . map ( ( ) => new Array ( MAX ) ) ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = 0 ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = 1 ; for ( let j = i - 1 ; j >= 0 ; j -- ) { if ( A [ i ] >= A [ j ] ) { let diff = A [ i ] - A [ j ] ; for ( let k = 0 ; k <= diff ; k ++ ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j ] [ k ] + 1 ) ; } } } } let maxSetSize = - 1 ; for ( let i = 0 ; i < MAX ; i ++ ) maxSetSize = Math . max ( maxSetSize , dp [ N - 1 ] [ i ] ) ; return N - maxSetSize ; } let A = [ 1 , 4 , 5 , 7 , 20 , 21 ] ; let N = A . length ; document . write ( minimumDeletions ( A , N ) + " " ) ;
Count N | Stores the value of overlapping states ; Function to check whether a number have only digits X or Y or not ; Until sum is positive ; If any digit is not X or Y then return 0 ; Return 1 ; Function to find the count of numbers that are formed by digits X and Y and whose sum of digit also have digit X or Y ; Initialize dp array ; Base Case ; Check if sum of digits formed by only X or Y ; Return the already computed ; Place the digit X at the current position ; Place the digit Y at the current position ; Update current state result ; Driver Code ; Function Call
let dp = [ ] ; for ( let i = 0 ; i < 1005 ; i ++ ) { dp [ i ] = [ ] ; for ( let j = 0 ; j < 9005 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } let mod = 1000000007 ; function check ( sum , x , y ) { while ( sum > 0 ) { let ln = sum % 10 ; if ( ln != x && ln != y ) { return 0 ; } sum = Math . floor ( sum / 10 ) ; } return 1 ; } function countNumbers ( n , x , y , sum ) { for ( let i = 0 ; i < 1005 ; i ++ ) { for ( let j = 0 ; j < 9005 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } if ( n == 0 ) { return check ( sum , x , y ) ; } if ( dp [ n ] [ sum ] != - 1 ) { return dp [ n ] [ sum ] % mod ; } let option1 = countNumbers ( n - 1 , x , y , sum + x ) % mod ; let option2 = countNumbers ( n - 1 , x , y , sum + y ) % mod ; return dp [ n ] [ sum ] = ( option1 + option2 ) % mod ; } let N = 3 , X = 1 , Y = 5 ; document . write ( countNumbers ( N , X , Y , 0 ) % mod ) ;
Minimum cost to generate any permutation of the given string | Function that returns true if the current bit is set ; Function to find the minimum cost to form any permutation of string s ; Base Case ; Return the precomputed state ; Iterate over the string and check all possible characters available for current position ; Check if character can be placed at current position ; As there is no previous character so the cost for 1 st character is 0 ; Find the cost of current character and move to next position ; Store the answer for each current state ; Function that generates any permutation of the given string with minimum cost ; Initialize dp table ; Set all the bits of the current character id ; Minimum cost of generating the permutation ; Driver Code ; Function Call
function check ( mask , i ) { var c = ( mask & ( 1 << i ) ) ; return c != 0 ; } function solve ( a , s , n , prev , mask , dp ) { if ( mask == 0 ) return 0 ; if ( dp [ mask ] [ prev + 1 ] != - 1 ) return dp [ mask ] [ prev + 1 ] ; var ans = 10000 ; for ( var i = 0 ; i < s . length ; i ++ ) { var id = s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( check ( mask , id ) ) { if ( prev == - 1 ) { ans = Math . min ( ans , solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } else { ans = Math . min ( ans , a [ prev ] [ id ] + solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } } } dp [ mask ] [ prev + 1 ] = ans ; return ans ; } function generatePermutation ( mask , n , a , s ) { var dp = Array . from ( Array ( ( 1 << n ) + 5 ) , ( ) => Array ( n + 5 ) . fill ( - 1 ) ) ; for ( var i = 0 ; i < s . length ; i ++ ) { var id = s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; mask |= ( 1 << id ) ; } document . write ( solve ( a , s , n , - 1 , mask , dp ) + " " ) ; } var N = 5 ; var str = " " ; var mat = [ [ 0 , 5 , 1 , 5 , 3 ] , [ 4 , 0 , 9 , 4 , 2 ] , [ 7 , 9 , 0 , 10 , 7 ] , [ 1 , 2 , 8 , 0 , 2 ] , [ 3 , 9 , 7 , 7 , 0 ] ] ; generatePermutation ( 0 , N , mat , str ) ;
Minimize Cost to reduce the Array to a single element by given operations | JavaScript program for the above approach ; Function to generate the cost using Prefix Sum Array technique ; Function to combine the sum of the two subproblems ; Function to minimize the cost to add the array elements to a single element ; Check if the value is already stored in the array ; Compute left subproblem ; Compute left subproblem ; Calculate minimum cost ; Store the answer to avoid recalculation ; Driver code ; Initialise dp array ; Preprocessing the array
var inf = 10000000 ; function preprocess ( a , n ) { var p = Array ( n ) ; p [ 0 ] = a [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) { p [ i ] = p [ i - 1 ] + a [ i ] ; } return p ; } function Combine ( p , i , j ) { if ( i == 0 ) return p [ j ] ; else return p [ j ] - p [ i - 1 ] ; } function minCost ( a , i , j , k , prefix , dp ) { if ( i >= j ) return 0 ; if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; var best_cost = inf ; for ( var pos = i ; pos < j ; pos ++ ) { var left = minCost ( a , i , pos , k , prefix , dp ) ; var right = minCost ( a , pos + 1 , j , k , prefix , dp ) ; best_cost = Math . min ( best_cost , left + right + ( k * Combine ( prefix , i , j ) ) ) ; } return dp [ i ] [ j ] = best_cost ; } var n = 4 ; var a = [ 4 , 5 , 6 , 7 ] ; var k = 3 ; var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( n + 1 ) . fill ( - 1 ) ) ; var prefix = preprocess ( a , n ) ; document . write ( minCost ( a , 0 , n - 1 , k , prefix , dp ) )
Minimize operations required to obtain N | Function to find the minimum number of operations ; Storing the minimum operations to obtain all numbers up to N ; Initilal state ; Iterate for the remaining numbers ; If the number can be obtained by multiplication by 2 ; If the number can be obtained by multiplication by 3 ; Obtaining the number by adding 1 ; Return the minm operations to obtain n ; Driver code
function minOperations ( n ) { let dp = new Array ( n + 1 ) ; dp [ 1 ] = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { dp [ i ] = Number . MAX_VALUE ; if ( i % 2 == 0 ) { let x = dp [ parseInt ( i / 2 , 10 ) ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } if ( i % 3 == 0 ) { let x = dp [ parseInt ( i / 3 , 10 ) ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } let x = dp [ i - 1 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } return dp [ n ] ; } let n = 15 ; document . write ( minOperations ( n ) ) ;
Longest Reverse Bitonic Sequence | Function to return the length of the Longest Reverse Bitonic Subsequence in the array ; Allocate memory for LIS [ ] and initialize LIS values as 1 for all indexes ; Compute LIS values from left to right for every index ; Initialize LDS for all indexes as 1 ; Compute LDS values for every index from right to left ; Find the maximum value of lis [ i ] + lds [ i ] - 1 in the array ; Return the maximum ; Driver Code
function ReverseBitonic ( arr , N ) { let i , j ; let lds = [ ] ; for ( i = 0 ; i < N ; i ++ ) { lds [ i ] = 1 ; } for ( i = 1 ; i < N ; i ++ ) { for ( j = 0 ; j < i ; j ++ ) { if ( arr [ i ] < arr [ j ] && lds [ i ] < lds [ j ] + 1 ) { lds [ i ] = lds [ j ] + 1 ; } } } let lis = [ ] ; for ( i = 0 ; i < N ; i ++ ) { lis [ i ] = 1 ; } for ( i = N - 2 ; i >= 0 ; i -- ) { for ( j = N - 1 ; j > i ; j -- ) { if ( arr [ i ] < arr [ j ] && lis [ i ] < lis [ j ] + 1 ) { lis [ i ] = lis [ j ] + 1 ; } } } let max = lis [ 0 ] + lds [ 0 ] - 1 ; for ( i = 1 ; i < N ; i ++ ) { if ( lis [ i ] + lds [ i ] - 1 > max ) { max = lis [ i ] + lds [ i ] - 1 ; } } return max ; } let arr = [ 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 ] ; let N = arr . length ; document . write ( " " + ReverseBitonic ( arr , N ) ) ;
Maze With N doors and 1 Key | Recursive function to check whether there is a path from the top left cell to the bottom right cell of the maze ; Check whether the current cell is within the maze ; If key is required to move further ; If the key hasn 't been used before ; If current cell is the destination ; Either go down or right ; Key has been used before ; If current cell is the destination ; Either go down or right ; Driver code ; If there is a path from the cell ( 0 , 0 )
function findPath ( maze , xpos , ypos , key ) { if ( xpos < 0 xpos >= maze . length ypos < 0 ypos >= maze . length ) return false ; if ( maze [ xpos ] [ ypos ] == ' ' ) { if ( key == true ) if ( xpos == maze . length - 1 && ypos == maze . length - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , false ) || findPath ( maze , xpos , ypos + 1 , false ) ; return false ; } if ( xpos == maze . length - 1 && ypos == maze . length - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , key ) || findPath ( maze , xpos , ypos + 1 , key ) ; } function mazeProb ( maze , xpos , ypos ) { let key = true ; if ( findPath ( maze , xpos , ypos , key ) ) return true ; return false ; } let maze = [ [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] ] ; let n = maze . length ; if ( mazeProb ( maze , 0 , 0 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum cost to reach end of array array when a maximum jump of K index is allowed | Function to return the minimum cost to reach the last index ; If we reach the last index ; Already visited state ; Initially maximum ; Visit all possible reachable index ; If inside range ; We cannot move any further ; Memoize ; Driver Code
function FindMinimumCost ( ind , a , n , k , dp ) { if ( ind == ( n - 1 ) ) return 0 ; else if ( dp [ ind ] != - 1 ) return dp [ ind ] ; else { var ans = Number . MAX_VALUE ; for ( var i = 1 ; i <= k ; i ++ ) { if ( ind + i < n ) ans = Math . min ( ans , Math . abs ( a [ ind + i ] - a [ ind ] ) + FindMinimumCost ( ind + i , a , n , k , dp ) ) ; else break ; } return dp [ ind ] = ans ; } } var a = [ 10 , 30 , 40 , 50 , 20 ] ; var k = 3 ; var n = a . length ; var dp = Array ( n ) . fill ( - 1 ) ; document . write ( FindMinimumCost ( 0 , a , n , k , dp ) ) ;
Sum of all even factors of numbers in the range [ l , r ] | Javascript implementation of the approach ; Function to calculate the prefix sum of all the even factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the even factors of the numbers in the given range ; Driver code
var MAX = 100000 ; prefix = Array ( MAX ) . fill ( 0 ) ; function sieve_modified ( ) { for ( var i = 2 ; i < MAX ; i += 2 ) { for ( var j = i ; j < MAX ; j += i ) prefix [ j ] += i ; } for ( var i = 1 ; i < MAX ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; } function sumEvenFactors ( L , R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } sieve_modified ( ) ; var l = 6 , r = 10 ; document . write ( sumEvenFactors ( l , r ) ) ;