codeparrot/xlcost-text-to-code huggingface dataset:
Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not | Javascript Program for the above approach ; Function to check if a number is composite or not ; Corner cases ; Check if the number is divisible by 2 or 3 or not ; Check if n is a multiple of any other prime number ; Function to check if a number is a Perfect Square or not ; Function to check if a number is a Fibonacci number or not ; If 5 * n ^ 2 + 4 or 5 * n ^ 2 - 4 or both are perfect square ; Function to check if GCD of composite numbers from the array a [ ] which are divisible by k is a Fibonacci number or not ; Traverse the array ; If array element is composite and divisible by k ; Calculate GCD of all elements in compositeset ; If GCD is Fibonacci ; Driver Code
function __gcd ( a , b ) { if ( ! b ) { return a ; } return __gcd ( b , a % b ) ; } function isComposite ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; var i ; for ( i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } function isPerfectSquare ( x ) { var s = Math . sqrt ( x ) ; return ( s * s == x ) ; } function isFibonacci ( n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } function ifgcdFibonacci ( a , n , k ) { var compositeset = [ ] ; var i ; for ( i = 0 ; i < n ; i ++ ) { if ( isComposite ( a [ i ] ) && a [ i ] % k == 0 ) { compositeset . push ( a [ i ] ) ; } } var gcd = compositeset [ 0 ] ; for ( i = 1 ; i < compositeset . length ; i ++ ) { gcd = __gcd ( gcd , compositeset [ i ] ) ; if ( gcd == 1 ) { break ; } } if ( isFibonacci ( gcd ) ) { document . write ( " " ) ; return ; } document . write ( " " ) ; return ; } var arr = [ 34 , 2 , 4 , 8 , 5 , 7 , 11 ] ; var n = arr . length ; var k = 2 ; ifgcdFibonacci ( arr , n , k ) ;
Longest subarray in which all elements are a factor of K | Function to find the length of the longest subarray in which all elements are a factor of K ; Stores length of the longest subarray in which all elements are a factor of K ; Stores length of the current subarray ; Traverse the array arr [ ] ; If A [ i ] is a factor of K ; Update Len ; Update MaxLen ; If A [ i ] is not a factor of K ; Update Len ; Driver code
function find_longest_subarray ( A , N , K ) { let MaxLen = 0 ; let Len = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( K % A [ i ] == 0 ) { Len ++ ; MaxLen = Math . max ( MaxLen , Len ) ; } else { Len = 0 ; } } return MaxLen ; } let A = [ 2 , 8 , 3 , 10 , 6 , 7 , 4 , 9 ] ; let N = A . length ; let K = 60 ; document . write ( find_longest_subarray ( A , N , K ) ) ;
Length of smallest meeting that can be attended | Javascript program to implement the above approach ; Function to find the minimum time to attend exactly one meeting ; Stores minimum time to attend exactly one meeting ; Sort entrance [ ] array ; Sort exit [ ] time ; Traverse meeting [ ] [ ] ; Stores start time of current meeting ; Stores end time of current meeting ; Find just greater value of u in entrance [ ] ; Find just greater or equal value of u in entrance [ ] ; System . out . println ( exit . size ( ) ) ; Stores enter time to attend the current meeting ; Stores exist time after attending the meeting ; Update start lies in range [ 0 , m - 1 ] and end lies in the range [ 0 , p - 1 ] ; Update ans ; Return answer ; vec . clear ( ) ; ; Stores interval of meeting ; Stores entrance timings ; Stores exit timings ; Stores total count of meetings ; Stores total entrance timings ; Stores total exit timings ; Minimum time
let exit = [ ] ; function minTime ( meeting , n , entrance , m , p ) { let ans = Number . MAX_VALUE ; ( entrance ) . sort ( function ( a , b ) { return a - b ; } ) ; ( exit ) . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) { let u = meeting [ i ] [ 0 ] ; let v = meeting [ i ] [ 1 ] ; let it1 = upper_bound ( entrance , 0 , entrance . length , u ) ; let it2 = lowerBound ( exit , 0 , exit . length , v ) ; let start = it1 - 1 ; let end = it2 ; if ( start >= 0 && start < m && end >= 0 && end < p ) ans = Math . min ( ans , exit [ end ] - entrance [ start ] ) ; } return ans >= Number . MAX_VALUE ? - 1 : ans ; } function upper_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } function lowerBound ( vec , low , high , element ) { let array = new Array ( vec . length ) ; let k = 0 ; for ( let val = 0 ; val < vec . length ; val ++ ) { array [ k ] = vec [ val ] ; k ++ ; } while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( element > array [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low ; } let meeting = [ [ 15 , 19 ] , [ 5 , 10 ] , [ 7 , 25 ] ] ; let entrance = [ 4 , 13 , 25 , 2 ] ; exit . push ( 10 ) ; exit . push ( 25 ) ; let n = meeting . length ; let m = entrance . length ; let p = exit . length ; document . write ( minTime ( meeting , n , entrance , m , p ) + " " ) ;
Divide array into two arrays which does not contain any pair with sum K | Function to split the given array into two separate arrays satisfying given condition ; Stores resultant arrays ; Traverse the array ; If a [ i ] is smaller than or equal to k / 2 ; Print first array ; Print second array ; Given K ; Given array ; Given size
function splitArray ( a , n , k ) { let first = [ ] ; let second = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= parseInt ( k / 2 ) ) first . push ( a [ i ] ) ; else second . push ( a [ i ] ) ; } for ( let i = 0 ; i < first . length ; i ++ ) { document . write ( first [ i ] + " " ) ; } document . write ( " " ) ; for ( let i = 0 ; i < second . length ; i ++ ) { document . write ( second [ i ] + " " ) ; } } let k = 5 ; let a = [ 0 , 1 , 3 , 2 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; let n = a . length ; splitArray ( a , n , k ) ;
Count numbers in a given range whose count of prime factors is a Prime Number | Javascript program to implement the above approach ; Function to find the smallest prime factor of all the numbers in range [ 0 , MAX ] ; Stores smallest prime factor of all the numbers in the range [ 0 , MAX ] ; No smallest prime factor of 0 and 1 exists ; Traverse all the numbers in the range [ 1 , MAX ] ; Update spf [ i ] ; Update all the numbers whose smallest prime factor is 2 ; Traverse all the numbers in the range [ 1 , sqrt ( MAX ) ] ; Check if i is a prime number ; Update all the numbers whose smallest prime factor is i ; Check if j is a prime number ; Function to find count of prime factor of num ; Stores count of prime factor of num ; Calculate count of prime factor ; Update count ; Update num ; Function to precalculate the count of numbers in the range [ 0 , i ] whose count of prime factors is a prime number ; Stores the sum of all the numbers in the range [ 0 , i ] count of prime factor is a prime number ; Update sum [ 0 ] ; Traverse all the numbers in the range [ 1 , MAX ] ; Stores count of prime factor of i ; If count of prime factor is a prime number ; Update sum [ i ] ; Update sum [ i ] ; Stores smallest prime factor of all the numbers in the range [ 0 , MAX ] ; Stores the sum of all the numbers in the range [ 0 , i ] count of prime factor is a prime number ; let N = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ;
let MAX = 1001 function sieve ( ) { let spf = new Array ( MAX ) ; spf [ 0 ] = spf [ 1 ] = - 1 ; for ( let i = 2 ; i < MAX ; i ++ ) { spf [ i ] = i ; } for ( let i = 4 ; i < MAX ; i = i + 2 ) { spf [ i ] = 2 ; } for ( let i = 3 ; i * i < MAX ; i ++ ) { if ( spf [ i ] == i ) { for ( let j = i * i ; j < MAX ; j = j + i ) { if ( spf [ j ] == j ) { spf [ j ] = i ; } } } } return spf ; } function countFactors ( spf , num ) { let count = 0 ; while ( num > 1 ) { count ++ ; num = num / spf [ num ] ; } return count ; } function precalculateSum ( spf ) { let sum = new Array ( MAX ) ; sum [ 0 ] = 0 ; for ( let i = 1 ; i < MAX ; i ++ ) { let prime_factor = countFactors ( spf , i ) ; if ( spf [ prime_factor ] == prime_factor ) { sum [ i ] = sum [ i - 1 ] + 1 ; } else { sum [ i ] = sum [ i - 1 ] ; } } return sum ; } let spf = sieve ( ) ; let sum = precalculateSum ( spf ) ; let Q = [ [ 4 , 8 ] , [ 30 , 32 ] ] ; for ( let i = 0 ; i < 2 ; i ++ ) { document . write ( ( sum [ Q [ i ] [ 1 ] ] - sum [ Q [ i ] [ 0 ] - 1 ] ) + " " ) ; }
Maximum number of Perfect Numbers present in a subarray of size K | Function to check a number is Perfect Number or not ; Stores sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to N ; Function to return maximum sum of a subarray of size K ; If k is greater than N ; Compute sum of first window of size K ; Compute sums of remaining windows by removing first element of previous window and adding last element of current window ; Return the answer ; Function to find all the perfect numbers in the array ; The given array is converted into binary array ; Driver Code
function isPerfect ( N ) { let sum = 1 ; for ( let i = 2 ; i < Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { sum += i ; } else { sum += i + N / i ; } } } if ( sum == N && N != 1 ) return 1 ; return 0 ; } function maxSum ( arr , N , K ) { if ( N < K ) { document . write ( " " ) ; return - 1 ; } let res = 0 ; for ( let i = 0 ; i < K ; i ++ ) { res += arr [ i ] ; } let curr_sum = res ; for ( let i = K ; i < N ; i ++ ) { curr_sum += arr [ i ] - arr [ i - K ] ; res = Math . max ( res , curr_sum ) ; } return res ; } function max_PerfectNumbers ( arr , N , K ) { for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = isPerfect ( arr [ i ] ) == 1 ? 1 : 0 ; } return maxSum ( arr , N , K ) ; } let arr = [ 28 , 2 , 3 , 6 , 496 , 99 , 8128 , 24 ] ; let K = 4 ; let N = arr . length ; document . write ( max_PerfectNumbers ( arr , N , K ) ) ;
Count greater elements on the left side of every array element | Function to print the count of greater elements on left of each array element ; Function to get the count of greater elements on left of each array element ; Store distinct array elements in sorted order ; Stores the count of greater elements on the left side ; Traverse the array ; Insert array elements into the set ; Find previous greater element ; Find the distance between the previous greater element of arr [ i ] and last element of the set ; Driver Code
function display ( countLeftGreater , N ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( countLeftGreater [ i ] , " " ) ; } } function countGreater ( arr , N ) { let St = new Set ( ) ; let countLeftGreater = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { St . add ( arr [ i ] ) ; let it = 0 ; for ( let st of St ) { if ( arr [ i ] < st ) it += 1 ; } countLeftGreater [ i ] = Math . abs ( it ) ; } display ( countLeftGreater , N ) ; } let arr = [ 12 , 1 , 2 , 3 , 0 , 11 , 4 ] ; let N = arr . length ; countGreater ( arr , N ) ;
Count characters to be shifted from the start or end of a string to obtain another string | Function to find the minimum cost to convert string A to string B ; Length of string ; Initialize maxlen as 0 ; Traverse the string A ; Stores the length of substrings of string A ; Traversing string B for each character of A ; Shift i pointer towards right and increment length , if A [ i ] equals B [ j ] ; If traverse till end ; Update maxlen ; Return minimum cost ; Given two strings A and B ; Function call
function minCost ( A , B ) { var n = A . length ; var i = 0 ; var maxlen = 0 ; while ( i < n ) { var length = 0 ; for ( var j = 0 ; j < n ; ++ j ) { if ( A [ i ] == B [ j ] ) { ++ i ; ++ length ; if ( i == n ) break ; } } maxlen = Math . max ( maxlen , length ) ; } return n - maxlen ; } var A = " " ; var B = " " ; document . write ( minCost ( A , B ) ) ;
Lexicographic rank of a string among all its substrings | Function to find lexicographic rank of string among all its substring ; Length of string ; Traverse the given string and store the indices of each character ; Extract the index ; Store it in the vector ; Traverse the alphaIndex array lesser than the index of first character of given string ; If alphaIndex [ i ] size exceeds 0 ; Traverse over the indices ; Add count of substring equal to n - alphaIndex [ i ] [ j ] ; Store all substrings in a vector str starting with the first character of the given string ; Insert the current character to substring ; Store the substring formed ; Sort the substring in the lexicographical order ; Find the rank of given string ; increase the rank until the given string is same ; If substring is same as the given string ; Add 1 to rank of the given string ; Given string ; Function Call
function lexicographicRank ( s ) { var n = s . length ; var alphaIndex = Array . from ( Array ( 26 ) , ( ) => new Array ( ) ) ; for ( var i = 0 ; i < s . length ; i ++ ) { var x = s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; alphaIndex [ x ] . push ( i ) ; } var rank = 0 ; for ( var i = 0 ; i < 26 && ' ' . charCodeAt ( 0 ) + i < s [ 0 ] . charCodeAt ( 0 ) ; i ++ ) { if ( alphaIndex [ i ] . length > 0 ) { for ( var j = 0 ; j < alphaIndex [ i ] . length ; j ++ ) { rank = rank + ( n - alphaIndex [ i ] [ j ] ) ; } } } var str = [ ] ; for ( var i = 0 ; i < alphaIndex [ s [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] . length ; i ++ ) { var substring = " " ; var j = alphaIndex [ s [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ i ] ; for ( ; j < n ; j ++ ) { substring += ( s [ j ] ) ; str . push ( substring ) ; } } str . sort ( ) ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] != s ) { rank ++ ; } else { break ; } } return rank + 1 ; } var str = " " ; document . write ( lexicographicRank ( str ) ) ;
Find H | Function to find the H - index ; Set the range for binary search ; Check if current citations is possible ; Check to the right of mid ; Update h - index ; Since current value is not possible , check to the left of mid ; Prlet the h - index ; citations
function hIndex ( citations , n ) { let hindex = 0 ; let low = 0 , high = n - 1 ; while ( low <= high ) { let mid = ( low + high ) / 2 ; if ( citations [ mid ] >= ( mid + 1 ) ) { low = mid + 1 ; hindex = mid + 1 ; } else { high = mid - 1 ; } } document . write ( hindex ) ; return hindex ; } let n = 5 ; let citations = [ 5 , 3 , 3 , 2 , 2 ] ; hIndex ( citations , n )
Check if all strings of an array can be made same by interchanging characters | Function to check if all strings are equal after swap operations ; Stores the frequency of characters ; Stores the length of string ; Traverse the array ; Traverse each string ; Check if frequency of character is divisible by N ; Driver Code
function checkEqual ( arr , N ) { var hash = Array ( 256 ) . fill ( 0 ) ; var M = arr [ 0 ] . length ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) { hash [ arr [ i ] [ j ] ] ++ ; } } for ( var i = 0 ; i < 256 ; i ++ ) { if ( hash [ i ] % N != 0 ) { return false ; } } return true ; } var arr = [ " " , " " ] ; var N = arr . length ; if ( checkEqual ( arr , N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Maximize sum of remaining elements after every removal of the array half with greater sum | js program to implement the above approach ; Function to find the maximum sum ; Base Case ; Check if ( mapped key is found in the dictionary ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right values ; Store the value in dp array ; Return the final answer ; Function to print maximum sum ; Stores prefix sum ; Store results of subproblems ; Traversing the array ; Add prefix sum of array ; Print the answer ; Driver Code ; Function call
let dp = [ ] ; for ( let i = 0 ; i < 100 ; i ++ ) { dp [ i ] = [ ] ; for ( let j = 0 ; j < 100 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } function maxweight ( s , e , pre ) { if ( s == e ) return 0 ; if ( dp [ s ] [ e ] != - 1 ) return dp [ s ] [ e ] ; let ans = 0 ; for ( let i = s ; i < e ; i ++ ) { let left = pre [ i ] - pre [ s - 1 ] ; let right = pre [ e ] - pre [ i ] ; if ( left < right ) ans = Math . max ( ans , Number ( left + maxweight ( s , i , pre ) ) ) ; if ( left == right ) ans = Math . max ( ans , Math . max ( left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) ) ) ; if ( left > right ) ans = Math . max ( ans , right + maxweight ( i + 1 , e , pre ) ) ; dp [ s ] [ e ] = ans ; } return dp [ s ] [ e ] ; } function maxSum ( arr , n ) { let pre = new Map ( ) ; pre [ - 1 ] = 0 ; pre [ 0 ] = arr [ 0 ] ; for ( let i = 0 ; i < 100 ; i ++ ) { for ( let j = 0 ; j < 100 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } for ( let i = 0 ; i < n ; i ++ ) pre [ i ] = pre [ i - 1 ] + arr [ i ] ; document . write ( maxweight ( 0 , n - 1 , pre ) ) ; } let arr = [ 6 , 2 , 3 , 4 , 5 , 5 ] ; maxSum ( arr , 6 ) ;
Find K smallest leaf nodes from a given Binary Tree | Structure of binary tree node ; Function to create new node ; Utility function which calculates smallest three nodes of all leaf nodes ; Check if current root is a leaf node ; Traverse the left and right subtree ; Function to find the K smallest nodes of the Binary Tree ; Sorting the Leaf nodes array ; Loop to print the K smallest Leaf nodes of the array ; Construct binary tree ; Function call
class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function newNode ( data ) { let temp = new Node ( data ) ; return temp ; } function storeLeaf ( root , arr ) { if ( root == null ) return arr ; if ( root . left == null && root . right == null ) { arr . push ( root . data ) ; return arr ; } arr = storeLeaf ( root . left , arr ) ; arr = storeLeaf ( root . right , arr ) ; return arr ; } function KSmallest ( root , k ) { let arr = [ ] ; arr = storeLeaf ( root , arr ) ; arr . sort ( function ( a , b ) { return a - b } ) ; for ( let i = 0 ; i < k ; i ++ ) { if ( i < arr . length ) { document . write ( arr [ i ] + " " ) ; } else { break ; } } } let root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 4 ) ; root . left . left . left = newNode ( 21 ) ; root . left . right = newNode ( 5 ) ; root . left . right . right = newNode ( 8 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . right . right = newNode ( 19 ) ; KSmallest ( root , 3 ) ;
Check if all the pairs of an array are coprime with each other | JavaScript implementation of the above approach ; Function to check if all the pairs of the array are coprime with each other or not ; Check if GCD of the pair is not equal to 1 ; All pairs are non - coprime Return false ; Driver Program
function gcd ( a , b ) { if ( ! b ) { return a ; } return gcd ( b , a % b ) ; } function allCoprime ( A , n ) { var all_coprime = true ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { if ( gcd ( A [ i ] , A [ j ] ) != 1 ) { all_coprime = false ; break ; } } } return all_coprime ; } var A = [ 3 , 5 , 11 , 7 , 19 ] ; var arr_size = A . length ; if ( allCoprime ( A , arr_size ) ) { console . log ( " " ) ; } else { console . log ( " " ) ; }
Smallest power of 2 consisting of N digits | Function to return smallest power of 2 consisting of N digits ; calculate log2 N indirectly using log ( ) method ; Driver code
function smallestNum ( n ) { let power = log2 ( 10 ) ; return Math . ceil ( ( n - 1 ) * power ) ; } function log2 ( N ) { return ( Math . log ( N ) / Math . log ( 2 ) ) ; } let n = 4 ; document . write ( smallestNum ( n ) ) ;
Find a valid parenthesis sequence of length K from a given valid parenthesis sequence | Function to find the subsequence of length K forming valid sequence ; Stores the resultant String ; Check whether character at index i is visited or not ; Traverse the String ; Push index of open bracket ; Pop and mark visited ; Increment count by 2 ; Append the characters and create the resultant String ; Return the resultant String ; Driver Code ; Function call
function findString ( s , k ) { var n = s . length ; var ans = " " ; var st = [ ] ; var vis = new Array ( n ) ; List < bool > vis ( n , false ) ; var count = 0 ; for ( var i = 0 ; i < n ; ++ i ) { if ( s [ i ] === " " ) { st . push ( i ) ; } if ( count < k && s [ i ] === " " ) { vis [ st [ st . length - 1 ] ] = true ; st . pop ( ) ; vis [ i ] = true ; count += 2 ; } } for ( var i = 0 ; i < n ; ++ i ) { if ( vis [ i ] === true ) { ans += s [ i ] ; } } return ans ; } var s = " " ; var K = 2 ; document . write ( findString ( s , K ) ) ;
Node having maximum number of nodes less than its value in its subtree | Stores the nodes to be deleted ; Structure of a Tree node ; Function to create a new node ; Function to compare the current node key with keys received from it left & right tree by Post Order traversal ; Base Case ; Find nodes lesser than the current root in the left subtree ; Find nodes lesser than the current root in the right subtree ; Stores all the nodes less than the current node 's ; Add the nodes which are less than current node in left [ ] ; Add the nodes which are less than current node in right [ ] ; Create a combined vector for pass to it 's parent ; Stores key that has maximum nodes ; Return the vector of nodes ; Given Tree ; Function Call ; Print the node value
let mp = new Map ( ) ; let max_v , rootIndex ; class Node { constructor ( key ) { this . left = null ; this . right = null ; this . key = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return ( temp ) ; } function findNodes ( root ) { if ( root == null ) { return [ ] ; } let left = findNodes ( root . left ) ; let right = findNodes ( root . right ) ; let combined = [ ] ; let count = 0 ; for ( let i = 0 ; i < left . length ; i ++ ) { if ( left [ i ] < root . key ) { count += 1 ; } combined . push ( left [ i ] ) ; } for ( let i = 0 ; i < right . length ; i ++ ) { if ( right [ i ] < root . key ) { count += 1 ; } combined . push ( right [ i ] ) ; } combined . push ( root . key ) ; if ( count > max_v ) { rootIndex = root . key ; max_v = count ; } return combined ; } let root = newNode ( 3 ) ; root . left = newNode ( 4 ) ; root . right = newNode ( 6 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; root . left . left = newNode ( 10 ) ; root . left . right = newNode ( 2 ) ; max_v = 0 ; rootIndex = - 1 ; findNodes ( root ) ; document . write ( rootIndex ) ;
Maximum sum path in a matrix from top | Function to find the maximum sum path in the grid ; Dimensions of grid [ ] [ ] ; Stores maximum sum at each cell sum [ i ] [ j ] from cell sum [ 0 ] [ 0 ] ; Iterate to compute the maximum sum path in the grid ; Update the maximum path sum ; Return the maximum sum ; Driver Code
function MaximumPath ( grid ) { let N = grid . length ; let M = grid [ 0 ] . length ; let sum = new Array ( N + 1 ) ; for ( var i = 0 ; i < sum . length ; i ++ ) { sum [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < sum . length ; i ++ ) { for ( var j = 0 ; j < sum . length ; j ++ ) { sum [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= M ; j ++ ) { sum [ i ] [ j ] = Math . max ( sum [ i - 1 ] [ j ] , sum [ i ] [ j - 1 ] ) + grid [ i - 1 ] [ j - 1 ] ; } } return sum [ N ] [ M ] ; } let grid = [ [ 1 , 2 ] , [ 3 , 5 ] ] ; document . write ( MaximumPath ( grid ) ) ;
Maximize cost of deletions to obtain string having no pair of similar adjacent characters | Function to find maximum cost to remove consecutive characters ; Maximum cost ; Traverse from 0 to len ( s ) - 2 ; If characters are identical ; Add cost [ i ] if its maximum ; Add cost [ i + 1 ] if its maximum ; Increment i ; Return the final max count ; Given string s ; Given cost of removal ; Function call
function Maxcost ( s , cost ) { let maxcost = 0 ; let i = 0 ; while ( i < s . length - 1 ) { if ( s [ i ] == s [ i + 1 ] ) { if ( cost [ i ] > cost [ i + 1 ] ) maxcost += cost [ i ] ; else { maxcost += cost [ i + 1 ] ; cost [ i + 1 ] = cost [ i ] ; } } i ++ ; } return maxcost ; } let s = " " ; let cost = [ 1 , 2 , 3 , 4 , 5 ] ; document . write ( Maxcost ( s , cost ) ) ;