codeparrot/xlcost-text-to-code huggingface dataset:
Stepping Numbers | This function checks if an integer n is a Stepping Number ; Initalize prevDigit with - 1 ; Iterate through all digits of n and compare difference between value of previous and current digits ; Get Current digit ; Single digit is consider as a Stepping Number ; Check if absolute difference between prev digit and current digit is 1 ; A brute force approach based function to find all stepping numbers . ; Iterate through all the numbers from [ N , M ] and check if its a stepping number . ; Driver code ; Display Stepping Numbers in the range [ n , m ]
function isStepNum ( n ) { let prevDigit = - 1 ; while ( n > 0 ) { let curDigit = n % 10 ; if ( prevDigit == - 1 ) prevDigit = curDigit ; else { if ( Math . abs ( prevDigit - curDigit ) != 1 ) return false ; } prevDigit = curDigit ; n = parseInt ( n / 10 , 10 ) ; } return true ; } function displaySteppingNumbers ( n , m ) { for ( let i = n ; i <= m ; i ++ ) if ( isStepNum ( i ) ) document . write ( i + " " ) ; } let n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ;
Maximum sub | Function to return the maximum sum of the sub - sequence such that two consecutive elements have a difference of at least 3 in their indices in the given array ; If there is a single element in the array ; Either select it or don 't ; If there are two elements ; Either select the first element or don 't ; Either select the first or the second element or don 't select any element ; Either select the first element or don 't ; Either select the first or the second element or don 't select any element ; Either select first , second , third or nothing ; For the rest of the elements ; Either select the best sum till previous_index or select the current element + best_sum till index - 3 ; Driver code
function max_sum ( a , n ) { let dp = new Array ( n ) ; if ( n == 1 ) { dp [ 0 ] = Math . max ( 0 , a [ 0 ] ) ; } else if ( n == 2 ) { dp [ 0 ] = Math . max ( 0 , a [ 0 ] ) ; dp [ 1 ] = Math . max ( a [ 1 ] , dp [ 0 ] ) ; } else if ( n >= 3 ) { dp [ 0 ] = Math . max ( 0 , a [ 0 ] ) ; dp [ 1 ] = Math . max ( a [ 1 ] , Math . max ( 0 , a [ 0 ] ) ) ; dp [ 2 ] = Math . max ( a [ 2 ] , Math . max ( a [ 1 ] , Math . max ( 0 , a [ 0 ] ) ) ) ; let i = 3 ; while ( i < n ) { dp [ i ] = Math . max ( dp [ i - 1 ] , a [ i ] + dp [ i - 3 ] ) ; i ++ ; } } return dp [ n - 1 ] ; } let arr = [ 1 , 2 , - 2 , 4 , 3 ] ; let n = arr . length ; document . write ( max_sum ( arr , n ) ) ;
Minimum count of elements that sums to a given number | Javascript implementation of the above approach ; we will only store min counts of sum upto 100 ; initialize with INT_MAX ; memo [ 0 ] = 0 as 0 is made from 0 elements ; fill memo array with min counts of elements that will constitute sum upto 100 ; min_count will store min count of elements chosen ; starting from end iterate over each 2 digits and add min count of elements to min_count ; Driver code
function minCount ( K ) { let memo = new Array ( 100 ) ; for ( let i = 0 ; i < 100 ; i ++ ) { memo [ i ] = Number . MAX_VALUE ; } memo [ 0 ] = 0 ; for ( let i = 1 ; i < 100 ; i ++ ) { memo [ i ] = Math . min ( memo [ i - 1 ] + 1 , memo [ i ] ) ; } for ( let i = 10 ; i < 100 ; i ++ ) { memo [ i ] = Math . min ( memo [ i - 10 ] + 1 , memo [ i ] ) ; } for ( let i = 25 ; i < 100 ; i ++ ) { memo [ i ] = Math . min ( memo [ i - 25 ] + 1 , memo [ i ] ) ; } let min_count = 0 ; while ( K > 0 ) { min_count += memo [ K % 100 ] ; K = parseInt ( K / 100 , 10 ) ; } return min_count ; } let K = 69 ; document . write ( minCount ( K ) ) ;
Number of sub | Javascript implementation of the approach ; Function to return the number of subsequences which have at least one consecutive pair with difference less than or equal to 1 ; Not required sub - sequences which turn required on adding i ; Required sub - sequence till now will be required sequence plus sub - sequence which turns required ; Similarly for not required ; Also updating total required and not required sub - sequences ; Also , storing values in dp ; Driver code
var N = 10000 ; function count_required_sequence ( n , arr ) { var total_required_subsequence = 0 ; var total_n_required_subsequence = 0 ; var dp = Array . from ( Array ( N ) , ( ) => Array ( 2 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { var turn_required = 0 ; for ( var j = - 1 ; j <= 1 ; j ++ ) turn_required += dp [ arr [ i ] + j ] [ 0 ] ; var required_end_i = ( total_required_subsequence + turn_required ) ; var n_required_end_i = ( 1 + total_n_required_subsequence - turn_required ) ; total_required_subsequence += required_end_i ; total_n_required_subsequence += n_required_end_i ; dp [ arr [ i ] ] [ 1 ] += required_end_i ; dp [ arr [ i ] ] [ 0 ] += n_required_end_i ; } return total_required_subsequence ; } var arr = [ 1 , 6 , 2 , 1 , 9 ] ; var n = arr . length ; document . write ( count_required_sequence ( n , arr ) + " " ) ;
Number of ways to choose elements from the array such that their average is K | javascript implementation of the above approach ; This dp array is used to store our values so that we don 't have to calculate same  values again and again ; Base cases Index can 't be less than 0 ; No element is picked hence average cannot be calculated ; If remainder is non zero , we cannot divide the sum by count i . e . the average will not be an integer ; If we find an average return 1 ; If we have already calculated this function simply return it instead of calculating it again ; If we don 't pick the current element  simple recur for index -1 ; If we pick the current element add it to our current sum and increment count by 1 ; Store the value for the current function ; Function to return the number of ways ; Push - 1 at the beginning to make it 1 - based indexing ; Initialize dp array by - 1 ; Call recursive function waysutil to calculate total ways ; Driver code
var MAX_INDEX = 51 ; var MAX_SUM = 2505 ; var dp = Array ( MAX_INDEX ) . fill ( ) . map ( ( ) => Array ( MAX_SUM ) . fill ( ) . map ( ( ) => Array ( MAX_INDEX ) . fill ( 0 ) ) ) ; ; function waysutil ( index , sum , count , arr , K ) { if ( index < 0 ) { return 0 ; } if ( index == 0 ) { if ( count == 0 ) { return 0 ; } var remainder = sum % count ; if ( remainder != 0 ) { return 0 ; } var average = sum / count ; if ( average == K ) { return 1 ; } } if ( dp [ index ] [ sum ] [ count ] != - 1 ) { return dp [ index ] [ sum ] [ count ] ; } var dontpick = waysutil ( index - 1 , sum , count , arr , K ) ; var pick = waysutil ( index - 1 , sum + arr [ index ] , count + 1 , arr , K ) ; var total = pick + dontpick ; dp [ index ] [ sum ] [ count ] = total ; return total ; } function ways ( N , K , arr ) { var Arr = [ ] ; Arr . push ( - 1 ) ; for ( i = 0 ; i < N ; ++ i ) { Arr . push ( arr [ i ] ) ; } for ( i = 0 ; i < MAX_INDEX ; i ++ ) { for ( j = 0 ; j < MAX_SUM ; j ++ ) { for ( l = 0 ; l < MAX_INDEX ; l ++ ) { dp [ i ] [ j ] [ l ] = - 1 ; } } } var answer = waysutil ( N , 0 , 0 , Arr , K ) ; return answer ; } var arr = [ 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 ] ; var N = arr . length ; var K = 5 ; document . write ( ways ( N , K , arr ) ) ;
Subset with sum closest to zero | Javascript Program for above approach ; Variable to store states of dp ; Function to return the number closer to integer s ; To find the sum closest to zero Since sum can be negative , we will add MAX to it to make it positive ; Base cases ; Checks if a state is already solved ; Recurrence relation ; Returning the value ; Function to calculate the closest sum value ; Calculate the Closest value for every subarray arr [ i - 1 : n ] ; Input array
let arrSize = 51 ; let maxSum = 201 ; let MAX = 100 ; let inf = 999999 ; let dp = new Array ( arrSize ) ; let visit = new Array ( arrSize ) ; for ( let i = 0 ; i < arrSize ; i ++ ) { dp [ i ] = new Array ( maxSum ) ; visit [ i ] = new Array ( maxSum ) ; for ( let j = 0 ; j < maxSum ; j ++ ) { dp [ i ] [ j ] = 0 ; visit [ i ] [ j ] = 0 ; } } function RetClose ( a , b , s ) { if ( Math . abs ( a - s ) < Math . abs ( b - s ) ) return a ; else return b ; } function MinDiff ( i , sum , arr , n ) { if ( i == n ) return 0 ; if ( visit [ i ] [ sum + MAX ] > 0 ) return dp [ i ] [ sum + MAX ] ; visit [ i ] [ sum + MAX ] = 1 ; dp [ i ] [ sum + MAX ] = RetClose ( arr [ i ] + MinDiff ( i + 1 , sum + arr [ i ] , arr , n ) , MinDiff ( i + 1 , sum , arr , n ) , - 1 * sum ) ; return dp [ i ] [ sum + MAX ] ; } function FindClose ( arr , n ) { let ans = inf ; for ( let i = 1 ; i <= n ; i ++ ) ans = RetClose ( arr [ i - 1 ] + MinDiff ( i , arr [ i - 1 ] , arr , n ) , ans , 0 ) ; document . write ( ans ) ; } let arr = [ 25 , - 9 , - 10 , - 4 , - 7 , - 33 ] ; let n = arr . length ; FindClose ( arr , n ) ;
Queries for bitwise OR in the index range [ L , R ] of the given array | Javascript implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code
let MAX = 100000 ; let bitscount = 32 ; let prefix_count = new Array ( bitscount ) ; for ( let i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { prefix_count [ i ] [ j ] = 0 ; } } function findPrefixCount ( arr , n ) { for ( let i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( let j = 1 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] += prefix_count [ i ] [ j - 1 ] ; } } } function rangeOr ( l , r ) { let ans = 0 ; for ( let i = 0 ; i < bitscount ; i ++ ) { let x ; if ( l == 0 ) x = prefix_count [ i ] [ r ] ; else x = prefix_count [ i ] [ r ] - prefix_count [ i ] [ l - 1 ] ; if ( x != 0 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } let arr = [ 7 , 5 , 3 , 5 , 2 , 3 ] ; let n = arr . length ; findPrefixCount ( arr , n ) ; let queries = [ [ 1 , 3 ] , [ 4 , 5 ] ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) document . write ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) + " " ) ;
Distinct palindromic sub | Function to return the count of distinct palindromic sub - strings of the given string s ; To store the positions of palindromic sub - strings ; Map to store the sub - strings ; Sub - strings of length 1 are palindromes ; Store continuous palindromic sub - strings ; Store palindromes of size 2 ; If str [ i ... ( i + 1 ) ] is not a palindromic then set dp [ i ] [ i + 1 ] = 0 ; Find palindromic sub - strings of length >= 3 ; End of palindromic substring ; If s [ start ] = = s [ end ] and dp [ start + 1 ] [ end - 1 ] is already palindrome then s [ start ... . end ] is also a palindrome ; Set dp [ start ] [ end ] = 1 ; Not a palindrome ; Return the count of distinct palindromes ; Driver Code
function palindromeSubStrs ( s ) { let dp = new Array ( s . length ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < dp . length ; i ++ ) { for ( let j = 0 ; j < dp . length ; j ++ ) { dp [ i ] [ j ] = 0 ; } } let st , end , i , len ; let m = new Map ( ) ; for ( i = 0 ; i < s . length ; i ++ ) { dp [ i ] [ i ] = 1 ; m . set ( s . substr ( i , i + 1 ) , true ) ; } for ( i = 0 ; i < s . length - 1 ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { dp [ i ] [ i + 1 ] = 1 ; m . set ( s . substr ( i , i + 2 ) , true ) ; } else dp [ i ] [ i + 1 ] = 0 ; } for ( len = 3 ; len <= s . length ; len ++ ) { for ( st = 0 ; st <= s . length - len ; st ++ ) { end = st + len - 1 ; if ( s [ st ] == s [ end ] && dp [ st + 1 ] [ end - 1 ] == 1 ) { dp [ st ] [ end ] = 1 ; m . set ( s . substr ( st , end + 1 ) , true ) ; } else dp [ st ] [ end ] = 0 ; } } return m . size ; } let s = " " ; document . write ( palindromeSubStrs ( s ) ) ;
Maximum sum path in a matrix from top to bottom and back | Input matrix ; DP matrix ; Function to return the sum of the cells arr [ i1 ] [ j1 ] and arr [ i2 ] [ j2 ] ; Recursive function to return the required maximum cost path ; Column number of second path ; Base Case ; If already calculated , return from DP matrix ; Recurring for neighbouring cells of both paths together ; Saving result to the DP matrix for current state ; set initial value
let n = 4 , m = 4 ; let arr = [ [ 1 , 0 , 3 , - 1 ] , [ 3 , 5 , 1 , - 2 ] , [ - 2 , 0 , 1 , 1 ] , [ 2 , 1 , - 1 , 1 ] ] ; let cache = new Array ( 5 ) ; for ( let i = 0 ; i < 5 ; i ++ ) { cache [ i ] = new Array ( 5 ) ; for ( let j = 0 ; j < 5 ; j ++ ) { cache [ i ] [ j ] = new Array ( 5 ) ; for ( let k = 0 ; k < 5 ; k ++ ) { cache [ i ] [ j ] [ k ] = - 1 ; } } } function sum ( i1 , j1 , i2 , j2 ) { if ( i1 == i2 && j1 == j2 ) { return arr [ i1 ] [ j1 ] ; } return arr [ i1 ] [ j1 ] + arr [ i2 ] [ j2 ] ; } function maxSumPath ( i1 , j1 , i2 ) { let j2 = i1 + j1 - i2 ; if ( i1 >= n i2 >= n j1 >= m j2 >= m ) { return 0 ; } if ( cache [ i1 ] [ j1 ] [ i2 ] != - 1 ) { return cache [ i1 ] [ j1 ] [ i2 ] ; } let ans = Number . MIN_VALUE ; ans = Math . max ( ans , maxSumPath ( i1 + 1 , j1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . max ( ans , maxSumPath ( i1 , j1 + 1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . max ( ans , maxSumPath ( i1 , j1 + 1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . max ( ans , maxSumPath ( i1 + 1 , j1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) ; cache [ i1 ] [ j1 ] [ i2 ] = ans ; return ans ; } document . write ( maxSumPath ( 0 , 0 , 0 ) ) ;
Bellman Ford Algorithm ( Simple Implementation ) | The main function that finds shortest distances from src to all other vertices using Bellman - Ford algorithm . The function also detects negative weight cycle The row graph [ i ] represents i - th edge with three values u , v and w . ; Initialize distance of all vertices as infinite . ; initialize distance of source as 0 ; Relax all edges | V | - 1 times . A simple shortest path from src to any other vertex can have at - most | V | - 1 edges ; check for negative - weight cycles . The above step guarantees shortest distances if graph doesn 't contain  negative weight cycle. If we get a  shorter path, then there is a cycle. ; Every edge has three values ( u , v , w ) where the edge is from vertex u to v . And weight of the edge is w .
function BellmanFord ( graph , V , E , src ) { var dis = Array ( V ) . fill ( 1000000000 ) ; dis [ src ] = 0 ; for ( var i = 0 ; i < V - 1 ; i ++ ) { for ( var j = 0 ; j < E ; j ++ ) { if ( ( dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] ) < dis [ graph [ j ] [ 1 ] ] ) dis [ graph [ j ] [ 1 ] ] = dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] ; } } for ( var i = 0 ; i < E ; i ++ ) { var x = graph [ i ] [ 0 ] ; var y = graph [ i ] [ 1 ] ; var weight = graph [ i ] [ 2 ] ; if ( ( dis [ x ] != 1000000000 ) && ( dis [ x ] + weight < dis [ y ] ) ) document . write ( " " + " " ) ; } document . write ( " " ) ; for ( var i = 0 ; i < V ; i ++ ) document . write ( i + " " + dis [ i ] + " " ) ; } var graph = [ [ 0 , 1 , - 1 ] , [ 0 , 2 , 4 ] , [ 1 , 2 , 3 ] , [ 1 , 3 , 2 ] , [ 1 , 4 , 2 ] , [ 3 , 2 , 5 ] , [ 3 , 1 , 1 ] , [ 4 , 3 , - 3 ] ] ; BellmanFord ( graph , V , E , 0 ) ;
Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal | JavaScript implementation of the approach ; Function to perform simple DFS ; Finding the number of times each bit is set in all the values of a subtree rooted at v ; Checking for each bit whether the numbers with that particular bit as set are either zero in both the resulting trees or greater than zero in both the resulting trees ; Number of nodes ; ArrayList to store the tree ; Array to store the value of nodes ; Array to store the number of times each bit is set in all the values of a subtree ; Array to store the number of times each bit is set in all the values in complete tree ; Finding the set bits in the value of node i ; add edges
var m = [ ] ; var a = [ ] ; var x = [ ] ; var g = [ ] ; var ans = 0 ; function dfs ( u , p ) { for ( var v of g [ u ] ) { if ( v != p ) { dfs ( v , u ) ; for ( var i = 0 ; i < 22 ; i ++ ) a [ u ] [ i ] += a [ v ] [ i ] ; } } var pp = 0 ; for ( var i = 0 ; i < 22 ; i ++ ) { if ( ! ( ( a [ u ] [ i ] > 0 && x [ i ] - a [ u ] [ i ] > 0 ) || ( a [ u ] [ i ] == 0 && x [ i ] == 0 ) ) ) { pp = 1 ; break ; } } if ( pp == 0 ) ans ++ ; } var n = 4 ; g = Array . from ( Array ( n + 1 ) , ( ) => Array ( ) . fill ( 0 ) ) ; m = Array ( n + 1 ) ; m [ 1 ] = 1 ; m [ 2 ] = 3 ; m [ 3 ] = 2 ; m [ 4 ] = 3 ; a = Array . from ( Array ( n + 1 ) , ( ) => Array ( 22 ) . fill ( 0 ) ) ; x = Array ( 22 ) . fill ( 0 ) ; for ( var i = 1 ; i <= n ; i ++ ) { g [ i ] = [ ] ; var y = m [ i ] ; var k = 0 ; while ( y != 0 ) { var p = y % 2 ; if ( p == 1 ) { x [ k ] ++ ; a [ i ] [ k ] ++ ; } y = parseInt ( y / 2 ) ; k ++ ; } } g [ 1 ] . push ( 2 ) ; g [ 2 ] . push ( 1 ) ; g [ 1 ] . push ( 3 ) ; g [ 3 ] . push ( 1 ) ; g [ 1 ] . push ( 4 ) ; g [ 4 ] . push ( 1 ) ; dfs ( 1 , 0 ) ; document . write ( ans ) ;
Find the maximum number of composite summands of a number | JavaScript implementation of the above approach ; Function to generate the dp array ; combination of three integers ; take the maximum number of summands ; Function to find the maximum number of summands ; If n is a smaller number , less than 16 return dp [ n ] ; Else , find a minimal number t as explained in solution ; Driver code ; Generate dp array
let maxn = 16 ; function precompute ( ) { let dp = new Array ( maxn ) ; let arr = [ 4 , 6 , 9 ] ; for ( let i = 0 ; i < maxn ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( let i = 1 ; i < maxn ; ++ i ) { for ( let k = 0 ; k < 3 ; k ++ ) { let j = arr [ k ] ; if ( i >= j && dp [ i - j ] != - 1 ) { dp [ i ] = Math . max ( dp [ i ] , dp [ i - j ] + 1 ) ; } } } return dp ; } function Maximum_Summands ( dp , n ) { if ( n < maxn ) return dp [ n ] ; else { let t = parseInt ( ( n - maxn ) / 4 , 10 ) + 1 ; return t + dp [ n - 4 * t ] ; } } let n = 12 ; $n = 12 ; let dp = precompute ( ) ; document . write ( Maximum_Summands ( dp , n ) ) ;
Minimum cost to reach end of array array when a maximum jump of K index is allowed | Function for returning the min of two elements ; for calculating the number of elements ; Allocating Memo table and initializing with INT_MAX ; Base case ; For every element relax every reachable element ie relax next k elements ; reaching next k element ; Relaxing the element ; return the last element in the array ; Driver Code
function min ( a , b ) { return ( a > b ) ? b : a ; } function minCostJumpsDP ( A , k ) { let size = A . length ; let x = new Array ( size ) ; for ( let i = 0 ; i < size ; i ++ ) x [ i ] = Number . MAX_VALUE ; x [ 0 ] = 0 ; for ( let i = 0 ; i < size ; i ++ ) { for ( let j = i + 1 ; j < i + k + 1 && j < size ; j ++ ) { x [ j ] = min ( x [ j ] , x [ i ] + Math . abs ( A [ i ] - A [ j ] ) ) ; } } return x [ size - 1 ] ; } let input = [ 83 , 26 , 37 , 35 , 33 , 35 , 56 ] ; document . write ( minCostJumpsDP ( input , 3 ) ) ;
DP on Trees | Set | JavaScript program to find diameter of a tree using DFS . ; Function to find the diameter of the tree using Dynamic Programming ; Store the first maximum and secondmax ; Traverse for all children of node ; Call DFS function again ; Find first max ; Secondmaximum ; Find secondmaximum ; Base case for every node ; if ( firstmax != - 1 ) Add ; Find dp [ 2 ] ; Return maximum of both ; Constructed tree is 1 / \ 2 3 / \ 4 5 ; create undirected edges ; Find diameter by calling function
let diameter = - 1 ; function dfs ( node , parent , dp1 , dp2 , adj ) { let firstmax = - 1 ; let secondmax = - 1 ; for ( let i = 0 ; i < adj [ node ] . length ; ++ i ) { if ( adj [ node ] [ i ] == parent ) continue ; dfs ( adj [ node ] [ i ] , node , dp1 , dp2 , adj ) ; if ( firstmax == - 1 ) { firstmax = dp1 [ adj [ node ] [ i ] ] ; } else if ( dp1 [ adj [ node ] [ i ] ] >= firstmax ) { secondmax = firstmax ; firstmax = dp1 [ adj [ node ] [ i ] ] ; } else if ( dp1 [ adj [ node ] [ i ] ] > secondmax ) { secondmax = dp1 [ adj [ node ] [ i ] ] ; } } dp1 [ node ] = 1 ; dp1 [ node ] += firstmax ; if ( secondmax != - 1 ) dp2 [ node ] = 1 + firstmax + secondmax ; return Math . max ( dp1 [ node ] , dp2 [ node ] ) ; } let n = 5 ; let adj = new Array ( n + 1 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { adj [ i ] = [ ] ; } adj [ 1 ] . push ( 2 ) ; adj [ 2 ] . push ( 1 ) ; adj [ 1 ] . push ( 3 ) ; adj [ 3 ] . push ( 1 ) ; adj [ 2 ] . push ( 4 ) ; adj [ 4 ] . push ( 2 ) ; adj [ 2 ] . push ( 5 ) ; adj [ 5 ] . push ( 2 ) ; let dp1 = new Array ( n + 1 ) ; let dp2 = new Array ( n + 1 ) ; dp1 . fill ( 0 ) ; dp2 . fill ( 0 ) ; document . write ( " " + dfs ( 1 , 1 , dp1 , dp2 , adj ) ) ;
Find sub | Javascript implementation of the approach ; Function to return the sum of the sub - matrix ; Function that returns true if it is possible to find the sub - matrix with required sum ; 2 - D array to store the sum of all the sub - matrices ; Filling of dp [ ] [ ] array ; Checking for each possible sub - matrix of size k X k ; Sub - matrix with the given sum not found ; Driver code ; Function call
var N = 4 function getSum ( r1 , r2 , c1 , c2 , dp ) { return dp [ r2 ] [ c2 ] - dp [ r2 ] [ c1 ] - dp [ r1 ] [ c2 ] + dp [ r1 ] [ c1 ] ; } function sumFound ( K , S , grid ) { var dp = Array . from ( Array ( N + 1 ) , ( ) => Array ( N + 1 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < N ; j ++ ) dp [ i + 1 ] [ j + 1 ] = dp [ i + 1 ] [ j ] + dp [ i ] [ j + 1 ] - dp [ i ] [ j ] + grid [ i ] [ j ] ; for ( var i = 0 ; i < N ; i ++ ) for ( var j = 0 ; j < N ; j ++ ) { var sum = getSum ( i , i + K , j , j + K , dp ) ; if ( sum == S ) return true ; } return false ; } var grid = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; var K = 2 ; var S = 14 ; if ( sumFound ( K , S , grid ) ) document . write ( " " ) ; else document . write ( " " ) ;
Stepping Numbers | Prints all stepping numbers reachable from num and in range [ n , m ] ; Queue will contain all the stepping Numbers ; Get the front element and pop from the queue ; If the Stepping Number is in the range [ n , m ] then display ; If Stepping Number is 0 or greater then m , no need to explore the neighbors ; Get the last digit of the currently visited Stepping Number ; There can be 2 cases either digit to be appended is lastDigit + 1 or lastDigit - 1 ; If lastDigit is 0 then only possible digit after 0 can be 1 for a Stepping Number ; If lastDigit is 9 then only possible digit after 9 can be 8 for a Stepping Number ; Prints all stepping numbers in range [ n , m ] using BFS . ; For every single digit Number ' i ' find all the Stepping Numbers starting with i ; Driver code ; Display Stepping Numbers in the range [ n , m ]
function bfs ( n , m , num ) { let q = [ ] ; q . push ( num ) ; while ( q . length != 0 ) { let stepNum = q . shift ( ) ; if ( stepNum <= m && stepNum >= n ) { document . write ( stepNum + " " ) ; } if ( stepNum == 0 stepNum > m ) continue ; let lastDigit = stepNum % 10 ; let stepNumA = stepNum * 10 + ( lastDigit - 1 ) ; let stepNumB = stepNum * 10 + ( lastDigit + 1 ) ; if ( lastDigit == 0 ) q . push ( stepNumB ) ; else if ( lastDigit == 9 ) q . push ( stepNumA ) ; else { q . push ( stepNumA ) ; q . push ( stepNumB ) ; } } } function displaySteppingNumbers ( n , m ) { for ( let i = 0 ; i <= 9 ; i ++ ) bfs ( n , m , i ) ; } let n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ;