codeparrot/xlcost-text-to-code huggingface dataset:
Length of longest increasing prime subsequence from a given array | Javascript program for the above approach ; Function to find the prime numbers till 10 ^ 5 using Sieve of Eratosthenes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function which computes the length of the LIS of Prime Numbers ; Create an array of size n ; Create boolean array to mark prime numbers ; Initialize all values to true ; Precompute N primes ; Compute optimized LIS having prime numbers in bottom up manner ; Check for LIS and prime ; Return maximum value in lis [ ] ; Given array ; Size of array ; Function Call
let N = 100005 function SieveOfEratosthenes ( prime , p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } function LISPrime ( arr , n ) { let lisp = new Array ( n ) ; let prime = new Array ( N + 1 ) ; prime . fill ( true ) ; SieveOfEratosthenes ( prime , N ) ; lisp [ 0 ] = prime [ arr [ 0 ] ] ? 1 : 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { lisp [ i ] = 0 ; continue ; } lisp [ i ] = 1 ; for ( let j = 0 ; j < i ; j ++ ) { if ( prime [ arr [ j ] ] && arr [ i ] > arr [ j ] && lisp [ i ] < lisp [ j ] + 1 ) { lisp [ i ] = lisp [ j ] + 1 ; } } } return lisp . sort ( ( a , b ) => b - a ) [ 0 ] ; } let arr = [ 1 , 2 , 5 , 3 , 2 , 5 , 1 , 7 ] ; let M = arr . length ; document . write ( LISPrime ( arr , M ) ) ;
Check if a given number can be expressed as pair | Function to check if the number is pair - sum of sum of first X natural numbers ; Check if the given number is sum of pair of special numbers ; X is the sum of first i natural numbers ; t = 2 * Y ; Condition to check if Y is a special number ; Driver Code ; Function call
function checkSumOfNatural ( n ) { var i = 1 ; var flag = false ; while ( i * ( i + 1 ) < n * 2 ) { var X = i * ( i + 1 ) ; var t = n * 2 - X ; var k = parseInt ( Math . sqrt ( t ) ) ; if ( k * ( k + 1 ) == t ) { flag = true ; break ; } i += 1 ; } if ( flag ) document . write ( " " ) ; else document . write ( " " ) ; } var n = 25 ; checkSumOfNatural ( n ) ;
Count distinct regular bracket sequences which are not N periodic | Function that finds the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Return the C ( n , k ) ; Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; Return C ( 2 n , n ) / ( n + 1 ) ; Function to find possible ways to put balanced parenthesis in an expression of length n ; If n is odd , not possible to create any valid parentheses ; Otherwise return n / 2 th Catalan Number ; Difference between counting ways of 2 * N and N is the result ; Given value of N ; Function Call
function binomialCoeff ( n , k ) { let res = 1 ; if ( k > n - k ) k = n - k ; for ( let i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } function catalan ( n ) { let c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } function findWays ( n ) { if ( n & 1 ) return 0 ; return catalan ( n / 2 ) ; } function countNonNPeriodic ( N ) { document . write ( findWays ( 2 * N ) - findWays ( N ) ) ; } let N = 4 ; countNonNPeriodic ( N ) ;
Check if two elements of a matrix are on the same diagonal or not | Function to check if two letegers are on the same diagonal of the matrix ; Storing indexes of x in I , J ; Storing Indexes of y in P , Q ; Condition to check if the both the elements are in same diagonal of a matrix ; Dimensions of Matrix ; Given Matrix ; Elements to be checked ; Function call
function checkSameDiag ( li , x , y , m , n ) { let I = 0 , J = 0 ; let P = 0 , Q = 0 ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( li [ i ] [ j ] == x ) { I = i ; J = j ; } if ( li [ i ] [ j ] == y ) { P = i ; Q = j ; } } } if ( P - Q == I - J P + Q == I + J ) { document . write ( " " ) ; } else document . write ( " " ) ; } let m = 6 ; let n = 5 ; let li = [ [ 32 , 94 , 99 , 26 , 82 ] , [ 51 , 69 , 52 , 63 , 17 ] , [ 90 , 36 , 88 , 55 , 33 ] , [ 93 , 42 , 73 , 39 , 28 ] , [ 81 , 31 , 83 , 53 , 10 ] , [ 12 , 29 , 85 , 80 , 87 ] ] ; let x = 42 ; let y = 80 ; checkSameDiag ( li , x , y , m , n ) ;
Count of decrement operations required to obtain K in N steps | Function to check whether m number of steps of type 1 are valid or not ; If m and n are the count of operations of type 1 and type 2 respectively , then n - m operations are performed ; Find the value of S after step 2 ; If m steps of type 1 is valid ; Function to find the number of operations of type 1 required ; Iterate over the range ; Find the value of mid ; Check if m steps of type 1 are valid or not ; If mid is the valid number of steps ; If no valid number of steps exist ; Given and N , K ; Function Call
function isValid ( n , m , k ) { var step2 = n - m ; var cnt = parseInt ( ( step2 * ( step2 + 1 ) ) / 2 ) ; if ( cnt - m == k ) return 0 ; if ( cnt - m > k ) return 1 ; return - 1 ; } function countOfOperations ( n , k ) { var start = 0 , end = n ; var ok = 1 ; while ( start <= end ) { var mid = parseInt ( ( start + end ) / 2 ) ; var temp = isValid ( n , mid , k ) ; if ( temp == 0 ) { ok = 0 ; document . write ( mid ) ; break ; } else if ( temp == 1 ) { start = mid + 1 ; } else { end = mid - 1 ; } } if ( ok ) document . write ( " " ) ; } var N = 5 , K = 4 ; countOfOperations ( N , K ) ;
Print all Possible Decodings of a given Digit Sequence | Function to check if all the characters are lowercase or not ; Traverse the string ; If any character is not found to be in lowerCase ; Function to print the decodings ; If all characters are not in lowercase ; Function to return the character corresponding to given integer ; Function to return the decodings ; Base case ; Recursive call ; Stores the characters of two digit numbers ; Extract first digit and first two digits ; Check if it lies in the range of alphabets ; Next recursive call ; Combine both the output in a single readonly output array ; Index of readonly output array ; Store the elements of output1 in readonly output array ; Store the elements of output2 in readonly output array ; Result the result ; Driver Code ; Function call ; Print function call
function nonLower ( s ) { for ( let i = 0 ; i < s . length ; i ++ ) { if ( ! ( s [ i ] . charCodeAt ( ) >= 97 && s [ i ] . charCodeAt ( ) <= 122 ) ) { return true ; } } return false ; } function printCodes ( output ) { for ( let i = 0 ; i < output . length ; i ++ ) { if ( nonLower ( output [ i ] ) ) continue ; document . write ( output [ i ] + " " ) ; } } function getChar ( n ) { return String . fromCharCode ( n + 96 ) ; } function getCode ( str ) { if ( str . length == 0 ) { let ans = [ " " ] ; return ans ; } let output1 = getCode ( str . substring ( 1 ) ) ; let output2 = new Array ( 0 ) ; let firstDigit = ( str [ 0 ] - ' ' ) ; let firstTwoDigit = 0 ; if ( str . length >= 2 ) { firstTwoDigit = ( str [ 0 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) * 10 + ( str [ 1 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; if ( firstTwoDigit >= 10 && firstTwoDigit <= 26 ) { output2 = getCode ( str . substring ( 2 ) ) ; } } let output = new Array ( output1 . length + output2 . length ) ; let k = 0 ; for ( let i = 0 ; i < output1 . length ; i ++ ) { let ch = getChar ( firstDigit ) ; output [ i ] = ch + output1 [ i ] ; k ++ ; } for ( let i = 0 ; i < output2 . length ; i ++ ) { let ch = getChar ( firstTwoDigit ) ; output [ k ] = ch + output2 [ i ] ; k ++ ; } return output ; } let input = " " ; let output = getCode ( input ) ; printCodes ( output ) ;
Count prime numbers that can be expressed as sum of consecutive prime numbers | Function to check if a number is prime or not ; Base Case ; Iterate till [ 5 , sqrt ( N ) ] to detect primality of numbers ; If N is divisible by i or i + 2 ; Return 1 if N is prime ; Function to count the prime numbers which can be expressed as sum of consecutive prime numbers ; Initialize count as 0 ; Stores prime numbers ; If i is prime ; Initialize the sum ; Find all required primes upto N ; Add it to the sum ; Return the final count ; Given number N ; Function Call
function isprm ( n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 1 ; if ( n % 2 == 0 n % 3 == 0 ) return 0 ; for ( var i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return 0 ; } return 1 ; } function countprime ( n ) { var count = 0 ; var primevector = [ ] ; for ( var i = 2 ; i <= n ; i ++ ) { if ( isprm ( i ) == 1 ) { primevector . push ( i ) ; } } var sum = primevector [ 0 ] ; for ( var i = 1 ; i < primevector . length ; i ++ ) { sum += primevector [ i ] ; if ( sum > n ) break ; if ( isprm ( sum ) == 1 ) { count ++ ; } } return count ; } var N = 45 ; document . write ( countprime ( N ) ) ;
Find a subarray of size K whose sum is a perfect square | Function to check if a given number is a perfect square or not ; Find square root of n ; Check if the square root is an integer or not ; Function to print the subarray whose sum is a perfect square ; Sum of first K elements ; If the first k elements have a sum as perfect square ; Iterate through the array ; If sum is perfect square ; If subarray not found ; Given array ; Given subarray size K ; Function Call
function isPerfectSquare ( n ) { var sr = Math . sqrt ( n ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } function SubarrayHavingPerfectSquare ( arr , k ) { var ans = [ ] ; var sum = 0 , i ; for ( i = 0 ; i < k ; i ++ ) { sum += arr [ i ] ; } var found = false ; if ( isPerfectSquare ( sum ) ) { ans [ 0 ] = 0 ; ans [ 1 ] = i - 1 ; } else { for ( var j = i ; j < arr . length ; j ++ ) { sum = sum + arr [ j ] - arr [ j - k ] ; if ( isPerfectSquare ( sum ) ) { found = true ; ans [ 0 ] = j - k + 1 ; ans [ 1 ] = j ; } } for ( var k = ans [ 0 ] ; k <= ans [ 1 ] ; k ++ ) { document . write ( arr [ k ] + " " ) ; } } if ( found == false ) { document . write ( " " ) ; } } var arr = [ 20 , 34 , 51 , 10 , 99 , 87 , 23 , 45 ] ; var K = 3 ; SubarrayHavingPerfectSquare ( arr , K ) ;
Check if any permutation of a number without any leading zeros is a power of 2 or not | Javascript program for the above approach ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i to n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagrams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Function to check if any permutation of a number is a power of 2 or not ; Iterate over all possible perfect power of 2 ; Print that number ; Given Number N ; Function Call
var TEN = 10 ; function updateFreq ( n , freq ) { while ( n ) { var digit = n % TEN ; freq [ digit ] ++ ; n = parseInt ( n / TEN ) ; } } function areAnagrams ( a , b ) { var freqA = Array ( TEN ) . fill ( 0 ) ; var freqB = Array ( TEN ) . fill ( 0 ) ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( var i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } function isPowerOf2 ( N ) { for ( var i = 0 ; i < 32 ; i ++ ) { if ( areAnagrams ( 1 << i , N ) ) { document . write ( ( 1 << i ) ) ; return true ; } } return false ; } var N = 46 ; if ( ! isPowerOf2 ( N ) ) { document . write ( " " ) ; }
Count number of triangles possible with length of sides not exceeding N | Function to count total number of right angled triangle ; Initialise count with 0 ; Run three nested loops and check all combinations of sides ; Condition for right angled triangle ; Increment count ; Given N ; Function call
function right_angled ( n ) { var count = 0 ; for ( z = 1 ; z <= n ; z ++ ) { for ( y = 1 ; y <= z ; y ++ ) { for ( x = 1 ; x <= y ; x ++ ) { if ( ( x * x ) + ( y * y ) == ( z * z ) ) { count ++ ; } } } } return count ; } var n = 5 ; document . write ( right_angled ( n ) ) ;
Check if the given two matrices are mirror images of one another | Function to check whether the two matrices are mirror of each other ; Initialising row and column of second matrix ; Iterating over the matrices ; Check row of first matrix with reversed row of second matrix ; If the element is not equal ; Increment column ; Reset column to 0 for new row ; Increment row ; Given 2 matrices ; Function Call
function mirrorMatrix ( mat1 , mat2 , N ) { let row = 0 ; let col = 0 ; let isMirrorImage = true ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = N - 1 ; j >= 0 ; j -- ) { if ( mat2 [ row ] [ col ] != mat1 [ i ] [ j ] ) { isMirrorImage = false ; } col ++ ; } col = 0 ; row ++ ; } if ( isMirrorImage ) document . write ( " " ) ; else document . write ( " " ) ; } let N = 4 ; let mat1 = [ [ 1 , 2 , 3 , 4 ] , [ 0 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; let mat2 = [ [ 4 , 3 , 2 , 1 ] , [ 8 , 7 , 6 , 0 ] , [ 12 , 11 , 10 , 9 ] , [ 16 , 15 , 14 , 13 ] ] ; mirrorMatrix ( mat1 , mat2 , N ) ;
Calculate number of nodes between two vertices in an acyclic Graph by DFS method | Function to return the count of nodes in the path from source to destination ; Mark the node visited ; If dest is reached ; Traverse all adjacent nodes ; If not already visited ; If there is path , then include the current node ; Return 0 if there is no path between src and dest through the current node ; Function to return the count of nodes between two given vertices of the acyclic Graph ; Initialize an adjacency list ; Populate the edges in the list ; Mark all the nodes as not visited ; Count nodes in the path from src to dest ; Return the nodes between src and dest ; Given number of vertices and edges ; Given source and destination vertices ; Given edges
function dfs ( src , dest , vis , adj ) { vis [ src ] = 1 ; if ( src == dest ) { return 1 ; } for ( let u = 0 ; u < adj [ src ] . length ; u ++ ) { if ( vis [ adj [ src ] [ u ] ] == 0 ) { let temp = dfs ( adj [ src ] [ u ] , dest , vis , adj ) ; if ( temp != 0 ) { return temp + 1 ; } } } return 0 ; } function countNodes ( V , E , src , dest , edges ) { let adj = new Array ( V + 1 ) ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; for ( let i = 0 ; i < E ; i ++ ) { adj [ edges [ i ] [ 0 ] ] . push ( edges [ i ] [ 1 ] ) ; adj [ edges [ i ] [ 1 ] ] . push ( edges [ i ] [ 0 ] ) ; } let vis = new Array ( V + 1 ) ; for ( let i = 0 ; i < vis . length ; i ++ ) { vis [ i ] = 0 ; } let count = dfs ( src , dest , vis , adj ) ; return count - 2 ; } let V = 8 , E = 7 ; let src = 5 , dest = 2 ; let edges = [ [ 1 , 4 ] , [ 4 , 5 ] , [ 4 , 2 ] , [ 2 , 6 ] , [ 6 , 3 ] , [ 2 , 7 ] , [ 3 , 8 ] ] ; document . write ( countNodes ( V , E , src , dest , edges ) ) ;
Check if all array elements are pairwise co | Function to calculate GCD ; Function to calculate LCM ; Function to check if all elements in the array are pairwise coprime ; Initialize variables ; Iterate over the array ; Calculate product of array elements ; Calculate LCM of array elements ; If the product of array elements is equal to LCM of the array ; Driver Code ; Function call
function GCD ( a , b ) { if ( a == 0 ) return b ; return GCD ( b % a , a ) ; } function LCM ( a , b ) { return ( a * b ) / GCD ( a , b ) ; } function checkPairwiseCoPrime ( A , n ) { var prod = 1 ; var lcm = 1 ; for ( i = 0 ; i < n ; i ++ ) { prod *= A [ i ] ; lcm = LCM ( A [ i ] , lcm ) ; } if ( prod == lcm ) document . write ( " " ) ; else document . write ( " " ) ; } var A = [ 2 , 3 , 5 ] ; var n = A . length ; checkPairwiseCoPrime ( A , n ) ;
Partition a set into two non | Function to return the maximum difference between the subset sums ; Stores the total sum of the array ; Checks for positive and negative elements ; Stores the minimum element from the given array ; Traverse the array ; Calculate total sum ; Mark positive element present in the set ; Mark negative element present in the set ; Find the minimum element of the set ; If the array contains both positive and negative elements ; Otherwise ; Given the array ; Length of the array ; Function Call
function maxDiffSubsets ( arr ) { var totalSum = 0 ; var pos = false , neg = false ; var min = Number . MAX_VALUE ; for ( i = 0 ; i < arr . length ; i ++ ) { totalSum += Math . abs ( arr [ i ] ) ; if ( arr [ i ] > 0 ) pos = true ; if ( arr [ i ] < 0 ) neg = true ; if ( arr [ i ] < min ) min = arr [ i ] ; } if ( pos && neg ) return totalSum ; else return totalSum - 2 * min ; } var S = [ 1 , 2 , 1 ] ; var N = S . length ; if ( N < 2 ) document . write ( " " ) ; else document . write ( maxDiffSubsets ( S ) ) ;
Split array into two subarrays such that difference of their sum is minimum | Function to return minimum difference between two subarray sums ; To store prefix sums ; Generate prefix sum array ; To store suffix sums ; Generate suffix sum array ; Stores the minimum difference ; Traverse the given array ; Calculate the difference ; Update minDiff ; Return minDiff ; Given array ; Length of the array
function minDiffSubArray ( arr , n ) { let prefix_sum = new Array ( n ) ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; let suffix_sum = new Array ( n ) ; suffix_sum [ n - 1 ] = arr [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) suffix_sum [ i ] = suffix_sum [ i + 1 ] + arr [ i ] ; let minDiff = Number . MAX_VALUE ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let diff = Math . abs ( prefix_sum [ i ] - suffix_sum [ i + 1 ] ) ; if ( diff < minDiff ) minDiff = diff ; } return minDiff ; } let arr = [ 7 , 9 , 5 , 10 ] ; let n = arr . length ; document . write ( minDiffSubArray ( arr , n ) ) ;
Count of days remaining for the next day with higher temperature | Function to determine how many days required to wait for the next warmer temperature ; To store the answer ; Traverse all the temperatures ; Check if current index is the next warmer temperature of any previous indexes ; Pop the element ; Push the current index ; Print waiting days ; Given temperatures ; Function Call
function dailyTemperatures ( T ) { var n = T . length ; var daysOfWait = Array ( n ) . fill ( - 1 ) ; var s = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { while ( s . length != 0 && T [ s [ s . length - 1 ] ] < T [ i ] ) { daysOfWait [ s [ s . length - 1 ] ] = i - s [ s . length - 1 ] ; s . pop ( ) ; } s . push ( i ) ; } for ( var i = 0 ; i < n ; i ++ ) { document . write ( daysOfWait [ i ] + " " ) ; } } var arr = [ 73 , 74 , 75 , 71 , 69 , 72 , 76 , 73 ] ; dailyTemperatures ( arr ) ;
Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U | To store the time ; Function to perform DFS to store times , distance and parent of each node ; Update the distance of node u ; Update parent of node u ; Increment time timeT ; Discovery time of node u ; Traverse the adjacency list of current node and recursively call DFS for each vertex ; If current node Adj [ u , i ] is unvisited ; Update the finishing time ; Function to add edges between nodes u and v ; Function to find the node U such that path from root to U contains nodes in V [ ] ; Initialise vis , dis , parent , preTime , and postTime ; Store Adjacency List ; Create adjacency List ; Perform DFS Traversal ; Stores the distance of deepest vertex ' u ' ; Update the deepest node by traversing the qu [ ] ; Find deepest vertex ; Replace each vertex with it 's  corresponding parent except  the root vertex ; Checks if the ancestor with respect to deepest vertex u ; Update ans ; Print the result ; Total vertices ; Given set of vertices ; Given edges ; Function call
let timeT = 0 ; function dfs ( u , p , dis , vis , distance , parent , preTime , postTime , Adj ) { distance [ u ] = dis ; parent [ u ] = p ; vis [ u ] = 1 ; timeT ++ ; preTime [ u ] = timeT ; for ( let i = 0 ; i < Adj [ u ] . length ; i ++ ) { if ( vis [ Adj [ u ] [ i ] ] == 0 ) { dfs ( Adj [ u ] [ i ] , u , dis + 1 , vis , distance , parent , preTime , postTime , Adj ) ; } } timeT ++ ; postTime [ u ] = timeT ; } function addEdge ( Adj , u , v ) { Adj [ u ] . push ( v ) ; Adj [ v ] . push ( u ) ; } function findNodeU ( N , V , Vertices , Edges ) { let vis = new Array ( N + 1 ) ; vis . fill ( 0 ) ; let distance = new Array ( N + 1 ) ; distance . fill ( 0 ) ; let parent = new Array ( N + 1 ) ; parent . fill ( 0 ) ; let preTime = new Array ( N + 1 ) ; preTime . fill ( 0 ) ; let postTime = new Array ( N + 1 ) ; postTime . fill ( 0 ) ; let Adj = [ ] ; for ( let i = 0 ; i < N + 1 ; i ++ ) Adj . push ( [ ] ) ; let u = 0 , v ; for ( let i = 0 ; i < N - 1 ; i ++ ) { addEdge ( Adj , Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } dfs ( 1 , 0 , 0 , vis , distance , parent , preTime , postTime , Adj ) ; let maximumDistance = 0 ; maximumDistance = 0 ; for ( let k = 0 ; k < V ; k ++ ) { if ( maximumDistance < distance [ Vertices [ k ] ] ) { maximumDistance = distance [ Vertices [ k ] ] ; u = Vertices [ k ] ; } if ( parent [ Vertices [ k ] ] != 0 ) { Vertices [ k ] = parent [ Vertices [ k ] ] ; } } let ans = true ; let flag ; for ( let k = 0 ; k < V ; k ++ ) { if ( preTime [ Vertices [ k ] ] <= preTime [ u ] && postTime [ Vertices [ k ] ] >= postTime [ u ] ) flag = true ; else flag = false ; ans = ans & flag ; } if ( ans ) document . write ( u ) ; else document . write ( " " ) ; } let N = 10 ; let V = 5 ; let Vertices = [ 4 , 3 , 8 , 9 , 10 ] ; let Edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] , [ 2 , 5 ] , [ 2 , 6 ] , [ 3 , 7 ] , [ 7 , 8 ] , [ 7 , 9 ] , [ 9 , 10 ] ] ; findNodeU ( N , V , Vertices , Edges ) ;
Check if an array contains only one distinct element | Function to find if the array contains only one distinct element ; Create a set ; Traversing the array ; Compare and print the result ; Driver code ; Function call
function uniqueElement ( arr , n ) { var set = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { set . add ( arr [ i ] ) ; } if ( set . size == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var arr = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 ] ; var n = arr . length ; uniqueElement ( arr , n ) ;
Maximum cost of splitting given Binary Tree into two halves | To store the results and sum of all nodes in the array ; To create adjacency list ; Function to add edges into the adjacency list ; Recursive function that calculate the value of the cost of splitting the tree recursively ; Fetch the child of node - r ; Neglect if cur node is parent ; Add all values of nodes which are decendents of r ; The two trees formed are rooted at ' r ' with its decendents ; Check and replace if current product t1 * t2 is large ; Function to find the maximum cost after splitting the tree in 2 halves ; Find sum of values in all nodes ; Traverse edges to create adjacency list ; Function Call ; Driver Code ; Values in each node ; Given Edges
let ans = 0 , allsum = 0 ; let edges = new Array ( 100001 ) ; function addedge ( a , b ) { edges [ a ] . push ( b ) ; edges [ b ] . push ( a ) ; } function findCost ( r , p , arr ) { let i , cur ; for ( i = 0 ; i < edges [ r ] . length ; i ++ ) { cur = edges [ r ] [ i ] ; if ( cur == p ) continue ; findCost ( cur , r , arr ) ; arr [ r ] += arr [ cur ] ; } let t1 = arr [ r ] ; let t2 = allsum - t1 ; if ( t1 * t2 > ans ) { ans = t1 * t2 ; } } function maximumCost ( r , p , N , M , arr , Edges ) { for ( let i = 0 ; i < N ; i ++ ) { allsum += arr [ i ] ; } for ( let i = 0 ; i < M ; i ++ ) { addedge ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } findCost ( r , p , arr ) ; } let N = 6 ; let arr = [ 13 , 8 , 7 , 4 , 5 , 9 ] ; let M = 5 ; let Edges = [ [ 0 , 1 ] , [ 1 , 2 ] , [ 1 , 4 ] , [ 3 , 4 ] , [ 4 , 5 ] ] ; for ( let i = 0 ; i < edges . length ; i ++ ) edges [ i ] = [ ] ; maximumCost ( 1 , - 1 , N , M , arr , Edges ) ; document . write ( ans ) ;