codeparrot/xlcost-text-to-code huggingface dataset:
Tcefrep Numbers | Iterative function to reverse digits of num ; Function to calculate sum of all proper divisors num -- > given natural number ; Final result of summation of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Add 1 to the result as 1 is also a divisor ; Given Number N ; Function Call
function reverse ( num ) { let rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = parseInt ( num / 10 ) ; } return rev_num ; } function properDivSum ( num ) { let result = 0 ; for ( i = 2 ; i <= Math . sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += i ; else result += ( i + num / i ) ; } } return ( result + 1 ) ; } function isTcefrep ( n ) { return properDivSum ( n ) == reverse ( n ) ; } let N = 6 ; if ( isTcefrep ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find the N | Javascript program to find n - th number containing only 3 and 5. ; If n is odd , append 3 and move to parent ; If n is even , append 5 and move to parent ; Reverse res and return . ; Driver code
function reverseString ( str ) { return str . split ( " " ) . reverse ( ) . join ( " " ) ; } function findNthNo ( n ) { let res = " " ; while ( n >= 1 ) { if ( ( n & 1 ) == 1 ) { res = res + " " ; n = ( n - 1 ) / 2 ; } else { res = res + " " ; n = ( n - 2 ) / 2 ; } } sb = ( res ) ; sb = reverseString ( sb ) ; return ( sb ) ; } let n = 5 ; document . write ( findNthNo ( n ) ) ;
Nth non | Function to find the nth Non - Square Number ; Conversion from var to var var is necessary in order to preserve decimal places after square root . ; Calculating the result ; Initializing the term number ; Print the result
function findNthNonSquare ( n ) { var x = n ; var ans = x + Math . floor ( 0.5 + Math . sqrt ( x ) ) ; return parseInt ( ans ) ; } var n = 16 ; document . write ( " " + n + " " ) ; document . write ( findNthNonSquare ( n ) ) ;
Sum of series with alternate signed squares of AP | function to calculate series sum ; Driver code
function seiresSum ( n , a ) { return n * ( a [ 0 ] * a [ 0 ] - a [ 2 * n - 1 ] * a [ 2 * n - 1 ] ) / ( 2 * n - 1 ) ; } let n = 2 ; a = [ 1 , 2 , 3 , 4 ] ; document . write ( seiresSum ( n , a ) ) ;
Find nth number that contains the digit k or divisible by k . | Function for checking if digit k is in n or not ; finding remainder ; if digit found ; Function for finding nth number ; since k is the first which satisfy th criteria , so consider it in count making count = 1 and starting from i = k + 1 ; checking that the number contain k digit or divisible by k ; Driver Code
function checkdigit ( n , k ) { while ( n != 0 ) { let rem = n % 10 ; if ( rem == k ) return true ; n = n / 10 ; } return false ; } function findNthNumber ( n , k ) { for ( let i = k + 1 , count = 1 ; count < n ; i ++ ) { if ( checkdigit ( i , k ) || ( i % k == 0 ) ) count ++ ; if ( count == n ) return i ; } return - 1 ; } let n = 10 , k = 2 ; document . write ( findNthNumber ( n , k ) ) ;
Count of subarrays of size K which is a permutation of numbers from 1 to K | Javascript implementation ; Save index of numbers of the array ; Update min and max index with the current index and check if it 's a valid permutation ; Driver Code
function find_permutations ( arr ) { var cnt = 0 ; var max_ind = - 1 , min_ind = 10000000 ; var n = arr . length ; var index_of = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { index_of . set ( arr [ i ] , i + 1 ) ; } for ( var i = 1 ; i <= n ; i ++ ) { max_ind = Math . max ( max_ind , index_of . get ( i ) ) ; min_ind = Math . min ( min_ind , index_of . get ( i ) ) ; if ( max_ind - min_ind + 1 == i ) cnt ++ ; } return cnt ; } var nums = [ ] ; nums . push ( 2 ) ; nums . push ( 3 ) ; nums . push ( 1 ) ; nums . push ( 5 ) ; nums . push ( 4 ) ; document . write ( find_permutations ( nums ) ) ;
Count of integers that divide all the elements of the given array | Javascript implementation of the approach ; Function to return the count of the required integers ; To store the gcd of the array elements ; To store the count of factors of the found gcd ; If g is a perfect square ; Factors appear in pairs ; Driver code
function calgcd ( a , b ) { if ( b == 0 ) return a ; return calgcd ( b , a % b ) ; } function getCount ( a , n ) { let gcd = 0 ; for ( let i = 0 ; i < n ; i ++ ) gcd = calgcd ( gcd , a [ i ] ) ; let cnt = 0 ; for ( let i = 1 ; i * i <= gcd ; i ++ ) { if ( gcd % i == 0 ) { if ( i * i == gcd ) cnt ++ ; else cnt += 2 ; } } return cnt ; } let a = [ 4 , 16 , 1024 , 48 ] ; let n = a . length ; document . write ( getCount ( a , n ) ) ;
Minimize cost of removals required to make all remaining characters of the string unique | Function to find the minimum cost of removing characters to make the string unique ; stores the visited character ; stores the answer ; traverse the string ; if already visited ; Stores the maximum cost of removing a particular character ; Store the total deletion cost of a particular character ; Mark the current character visited ; Traverse the indices of the string [ i , N - 1 ] ; If any duplicate is found ; Update the maximum cost and total cost ; Mark the current character visited ; Keep the character with maximum cost and delete the rest ; return the minimum cost ; input string ; input array ; function call
function delCost ( s , cost ) { var visited = Array ( s . length ) . fill ( false ) ; var ans = 0 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( visited [ i ] ) { continue ; } var maxDel = 0 ; var totalCost = 0 ; visited [ i ] = true ; for ( j = i ; j < s . length ; j ++ ) { if ( s . charAt ( i ) == s . charAt ( j ) ) { maxDel = Math . max ( maxDel , cost [ j ] ) ; totalCost += cost [ j ] ; visited [ j ] = true ; } } ans += totalCost - maxDel ; } return ans ; } var s = " " ; var cost = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; document . write ( delCost ( s , cost ) ) ;
Make all array elements equal by replacing triplets with their Bitwise XOR | Function to find triplets such that replacing them with their XOR make all array elements equal ; If N is even ; Calculate xor of array elements ; Traverse the array ; Update xor ; If xor is not equal to 0 ; Selecting the triplets such that elements of the pairs ( arr [ 0 ] , arr [ 1 ] ) , ( arr [ 2 ] , arr [ 3 ] ) . . . can be made equal ; Selecting the triplets such that all array elements can be made equal to arr [ N - 1 ] ; Selecting the triplets such that elements of the pairs ( arr [ 0 ] , arr [ 1 ] ) , ( arr [ 2 ] , arr [ 3 ] ) . . . can be made equal ; Selecting the triplets such that all array elements can be made equal to arr [ N - 1 ] ; Given array ; Size of array ; Function call
function checkXOR ( arr , N ) { if ( N % 2 == 0 ) { let xro = 0 ; for ( let i = 0 ; i < N ; i ++ ) { xro ^= arr [ i ] ; } if ( xro != 0 ) { document . write ( - 1 + " " ) ; return ; } for ( let i = 0 ; i < N - 3 ; i += 2 ) { document . write ( i + " " + ( i + 1 ) + " " + ( i + 2 ) + " " ) ; } for ( let i = 0 ; i < N - 3 ; i += 2 ) { document . write ( i + " " + ( i + 1 ) + " " + ( N - 1 ) + " " ) ; } } else { for ( let i = 0 ; i < N - 2 ; i += 2 ) { document . write ( i + " " + ( i + 1 ) + " " + ( i + 2 ) + " " ) ; } for ( let i = 0 ; i < N - 2 ; i += 2 ) { document . write ( i + " " + ( i + 1 ) + " " + ( N - 1 ) + " " ) ; } } } let arr = [ 4 , 2 , 1 , 7 , 2 ] ; let N = arr . length ; checkXOR ( arr , N ) ;
Make all array elements even by replacing adjacent pair of array elements with their sum | Function to find minimum count of operations required to make all array elements even ; Stores minimum count of replacements to make all array elements even ; Stores the count of odd continuous numbers ; Traverse the array ; If arr [ i ] is an odd number ; Update odd_cont_seg ; If odd_cont_seg is even ; Update res ; Update res ; Reset odd_cont_seg = 0 ; If odd_cont_seg exceeds 0 ; If odd_cont_seg is even ; Update res ; Update res ; Print the result ; Driver Code
function make_array_element_even ( arr , N ) { let res = 0 ; let odd_cont_seg = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd_cont_seg ++ ; } else { if ( odd_cont_seg > 0 ) { if ( odd_cont_seg % 2 == 0 ) { res += odd_cont_seg / 2 ; } else { res += ( odd_cont_seg / 2 ) + 2 ; } odd_cont_seg = 0 ; } } } if ( odd_cont_seg > 0 ) { if ( odd_cont_seg % 2 == 0 ) { res += odd_cont_seg / 2 ; } else { res += odd_cont_seg / 2 + 2 ; } } return res ; } let arr = [ 2 , 4 , 5 , 11 , 6 ] ; let N = arr . length ; document . write ( make_array_element_even ( arr , N ) ) ;
Find a number K such that exactly K array elements are greater than or equal to K | Function to find K for which there are exactly K array elements greater than or equal to K ; Finding the largest array element ; Possible values of K ; Traverse the array ; If current array element is greater than or equal to i ; If i array elements are greater than or equal to i ; Otherwise ; To find maximum Element ; Driver Code
function zvalue ( nums ) { var m = max_element ( nums ) ; var cnt = 0 ; for ( i = 0 ; i <= m ; i ++ ) { cnt = 0 ; for ( j = 0 ; j < nums . length ; j ++ ) { if ( nums [ j ] >= i ) cnt ++ ; } if ( cnt == i ) return i ; } return - 1 ; } function max_element ( nums ) { var max = nums [ 0 ] ; for ( i = 1 ; i < nums . length ; i ++ ) max = Math . max ( max , nums [ i ] ) ; return max ; } nums = [ 7 , 8 , 9 , 0 , 0 , 1 ] ; document . write ( zvalue ( nums ) ) ;
Lexicographically smallest and largest anagrams of a string containing another string as its substring | Function to find the lexicographically smallest anagram of string which contains another string ; Initializing the map and set ; Iterating over s1 ; Storing the frequency of characters present in s1 ; Storing the distinct characters present in s1 ; Decreasing the frequency of characters from M that are already present in s2 ; Traversing alphabets in sorted order ; If current character of set is not equal to current character of s2 ; If element is equal to current character of s2 ; Checking for second distinct character in s2 ; s2 [ j ] will store second distinct character ; Return the answer ; Function to find the lexicographically largest anagram of string which contains another string ; Getting the lexicographically smallest anagram ; d1 stores the prefix ; d2 stores the suffix ; Return the result ; Given two strings ; Function Calls
function lexico_smallest ( s1 , s2 ) { let M = new Map ( ) ; let S = new Set ( ) ; let pr ; for ( let i = 0 ; i <= s1 . length - 1 ; ++ i ) { if ( M . has ( s1 [ i ] ) ) { M [ s1 [ i ] ] ++ ; } else { M [ s1 [ i ] ] = 1 ; } S . add ( s1 [ i ] ) ; } for ( let i = 0 ; i <= s2 . length - 1 ; ++ i ) { if ( M . has ( s2 [ i ] ) ) { M [ s2 [ i ] ] -- ; } else { M [ s2 [ i ] ] = - 1 ; } } let c = s2 [ 0 ] ; let index = 0 ; let res = " " ; S . forEach ( function ( x ) { if ( x != c ) { for ( let i = 1 ; i <= M [ x ] ; ++ i ) { res += x ; } } else { let j = 0 ; index = res . length ; while ( s2 [ j ] == x ) { j ++ ; } if ( s2 [ j ] < c ) { res += s2 ; for ( let i = 1 ; i <= M [ x ] ; ++ i ) { res += x ; } } else { for ( let i = 1 ; i <= M [ x ] ; ++ i ) { res += x ; } index += M [ x ] ; res += s2 ; } } } ) res = " " ; pr = [ res , index ] ; return pr ; } function lexico_largest ( s1 , s2 ) { let pr = lexico_smallest ( s1 , s2 ) ; let d1 = " " ; for ( let i = pr [ 1 ] - 1 ; i >= 0 ; i -- ) { d1 += pr [ 0 ] [ i ] ; } let d2 = " " ; for ( let i = pr [ 0 ] . length - 1 ; i >= pr [ 1 ] + s2 . length ; -- i ) { d2 += pr [ 0 ] [ i ] ; } let res = d2 + s2 + d1 ; return res ; } let s1 = " " ; let s2 = " " ; document . write ( lexico_smallest ( s1 , s2 ) [ 0 ] + " " ) ; document . write ( lexico_largest ( s1 , s2 ) ) ;
Queries to find the count of shortest paths in a Tree that contains a given edge | Javascript implementation for the above approach ; Adjacency list to represent the tree ; Number of vertices ; Mark visited / unvisited vertices ; Stores the subtree size of the corresponding nodes ; Function to create an edge between two vertices ; Add a to b 's list ; Add b to a 's list ; Function to perform DFS ; Mark the vertex visited ; Include the node in the subtree ; Traverse all its children ; Function to print the required number of paths ; Number of vertices ; Calling modified dfs function ; Count pairs of vertices in the tree
var sz = 100005 ; var tree = Array . from ( Array ( sz ) , ( ) => Array ( ) ) var n ; var vis = Array ( sz ) ; var subtreeSize = Array ( sz ) ; function addEdge ( a , b ) { tree [ a ] . push ( b ) ; tree [ b ] . push ( a ) ; } function dfs ( x ) { vis [ x ] = true ; subtreeSize [ x ] = 1 ; tree [ x ] . forEach ( i => { if ( ! vis [ i ] ) { dfs ( i ) ; subtreeSize [ x ] += subtreeSize [ i ] ; } } ) ; } function countPairs ( a , b ) { var sub = Math . min ( subtreeSize [ a ] , subtreeSize [ b ] ) ; document . write ( sub * ( n - sub ) + " " ) ; } n = 6 ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 3 , 4 ) ; addEdge ( 3 , 5 ) ; dfs ( 0 ) ; countPairs ( 1 , 3 ) ; countPairs ( 0 , 2 ) ;
Count of permutations of an Array having each element as a multiple or a factor of its index | Function to find the count of desired permutations ; Base case ; If i has not been inserted ; Backtrack ; Insert i ; Recur to find valid permutations ; Remove i ; Return the final count ; Driver Code
function findPermutation ( arr , N ) { var pos = arr . size + 1 ; if ( pos > N ) return 1 ; var res = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { if ( ! arr . has ( i ) ) { if ( i % pos == 0 pos % i == 0 ) { arr . add ( i ) ; res += findPermutation ( arr , N ) ; arr . delete ( i ) ; } } } return res ; } var N = 5 ; var arr = new Set ( ) ; document . write ( findPermutation ( arr , N ) ) ;
Check if sum Y can be obtained from the Array by the given operations | Function to check if it is possible to obtain sum Y from a sequence of sum X from the array arr [ ] ; Store the difference ; Iterate over the array ; If diff reduced to 0 ; Driver Code
function solve ( arr , n , X , Y ) { var diff = Y - X ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != 1 ) { diff = diff % ( arr [ i ] - 1 ) ; } } if ( diff == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var arr = [ 1 , 2 , 7 , 9 , 10 ] ; var n = arr . length ; var X = 11 , Y = 13 ; solve ( arr , n , X , Y ) ;
Farthest distance of a Node from each Node of a Tree | JavaScript program to implement the above approach ; Adjacency List to store the graph ; Stores the height of each node ; Stores the maximum distance of a node from its ancestors ; Function to add edge between two vertices ; Insert edge from u to v ; Insert edge from v to u ; Function to calculate height of each Node ; Iterate in the adjacency list of the current node ; Dfs for child node ; Calculate height of nodes ; Increase height ; Function to calculate the maximum distance of a node from its ancestor ; Iterate in the adjacency list of the current node ; Find two children with maximum heights ; Calculate the maximum distance with ancestor for every node ; Calculating for children ; Driver Code ; Calculate height of nodes of the tree ; Calculate the maximum distance with ancestors ; Print the maximum of the two distances from each node
let maxN = 100001 ; let adj = new Array ( maxN ) ; adj . fill ( 0 ) ; let height = new Array ( maxN ) ; height . fill ( 0 ) ; let dist = new Array ( maxN ) ; dist . fill ( 0 ) ; function addEdge ( u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } function dfs1 ( cur , par ) { for ( let u = 0 ; u < adj [ cur ] . length ; u ++ ) { if ( adj [ cur ] [ u ] != par ) { dfs1 ( adj [ cur ] [ u ] , cur ) ; height [ cur ] = Math . max ( height [ cur ] , height [ adj [ cur ] [ u ] ] ) ; } } height [ cur ] += 1 ; } function dfs2 ( cur , par ) { let max1 = 0 ; let max2 = 0 ; for ( let u = 0 ; u < adj [ cur ] . length ; u ++ ) { if ( adj [ cur ] [ u ] != par ) { if ( height [ adj [ cur ] [ u ] ] >= max1 ) { max2 = max1 ; max1 = height [ adj [ cur ] [ u ] ] ; } else if ( height [ adj [ cur ] [ u ] ] > max2 ) { max2 = height [ adj [ cur ] [ u ] ] ; } } } let sum = 0 ; for ( let u = 0 ; u < adj [ cur ] . length ; u ++ ) { if ( adj [ cur ] [ u ] != par ) { sum = ( ( max1 == height [ adj [ cur ] [ u ] ] ) ? max2 : max1 ) ; if ( max1 == height [ adj [ cur ] [ u ] ] ) dist [ adj [ cur ] [ u ] ] = 1 + Math . max ( 1 + max2 , dist [ cur ] ) ; else dist [ adj [ cur ] [ u ] ] = 1 + Math . max ( 1 + max1 , dist [ cur ] ) ; dfs2 ( adj [ cur ] [ u ] , cur ) ; } } } let n = 6 ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; addEdge ( 1 , 2 ) ; addEdge ( 2 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 5 , 6 ) ; dfs1 ( 1 , 0 ) ; dfs2 ( 1 , 0 ) ; for ( let i = 1 ; i <= n ; i ++ ) document . write ( ( Math . max ( dist [ i ] , height [ i ] ) - 1 ) + " " ) ;
Middle of three using minimum comparisons | Javascript program to find middle of three distinct numbers ; Function to find the middle of three number ; Checking for b ; Checking for a ; driver code
function middleOfThree ( a , b , c ) { function middleOfThree ( $a , $b , $c ) { if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ; else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ; else return c ; } let a = 20 , b = 30 , c = 40 ; document . write ( middleOfThree ( a , b , c ) ) ;
Difference between Insertion sort and Selection sort | Function to implement the selection sort ; One by one move boundary of unsorted subarray ; Find the minimum element in unsorted array ; Swap the found minimum element with the first element ; Function to print an array ; Driver Code ; Function Call ; Print the array
function selectionSort ( arr , n ) { let i , j , min_idx ; for ( i = 0 ; i < n - 1 ; i ++ ) { min_idx = i ; for ( j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] < arr [ min_idx ] ) min_idx = j ; let temp = arr [ min_idx ] ; arr [ min_idx ] = arr [ i ] ; arr [ i ] = temp ; } } function printArray ( arr , size ) { let i ; for ( i = 0 ; i < size ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let arr = [ 64 , 25 , 12 , 22 , 11 ] ; let n = arr . length ; selectionSort ( arr , n ) ; document . write ( " " ) ; printArray ( arr , n ) ;
Check if a given string can be converted to another by given possible swaps | Javascript program to implement the above approach ; Stores length of str1 ; Stores length of str2 ; Stores distinct characters of str1 ; Stores distinct characters of str2 ; Stores frequency of each character of str1 ; Traverse the string str1 ; Update frequency of str1 [ i ] ; Traverse the string str1 ; Insert str1 [ i ] into st1 ; Traverse the string str2 ; Insert str1 [ i ] into st1 ; If distinct characters in str1 and str2 are not same ; Stores frequency of each character of str2 ; Traverse the string str2 ; Update frequency of str2 [ i ] ; Sort hash1 [ ] array ; Sort hash2 [ ] array ; Traverse hash1 [ ] and hash2 [ ] ; If hash1 [ i ] not equal to hash2 [ i ] ; Driver Code
function checkStr1CanConStr2 ( str1 , str2 ) { var N = str1 . length ; var M = str2 . length ; var st1 = new Set ( ) ; var st2 = new Set ( ) ; var hash1 = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { hash1 [ str1 [ i ] . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < N ; i ++ ) { st1 . add ( str1 [ i ] ) ; } for ( var i = 0 ; i < M ; i ++ ) { st2 . add ( str2 [ i ] ) ; } if ( st1 . size != st2 . size ) { return false ; } var hash2 = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < M ; i ++ ) { hash2 [ str2 [ i ] . charCodeAt ( 0 ) ] ++ ; } hash1 . sort ( ( a , b ) => a - b ) ; hash2 . sort ( ( a , b ) => a - b ) ; for ( var i = 0 ; i < 256 ; i ++ ) { if ( hash1 [ i ] != hash2 [ i ] ) { return false ; } } return true ; } var str1 = " " ; var str2 = " " ; if ( checkStr1CanConStr2 ( str1 , str2 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Sort the array in a given index range | Function to sort the elements of the array from index a to index b ; Function to sort the elements of the array from index a to index b ; Variables to store start and end of the index range ; Sort the subarray from arr [ l ] to arr [ r ] . sort ( arr , l , r + 1 ) ; ; Prvar the modified array ; Driver code
function swap ( arr , xp , yp ) { var temp = arr [ xp ] ; arr [ xp ] = arr [ yp ] ; arr [ yp ] = temp ; } function partSort ( arr , N , a , b ) { var l = Math . min ( a , b ) ; var r = Math . max ( a , b ) ; var i , j ; for ( i = l ; i < r + 1 + 1 ; i ++ ) { for ( j = l ; j < r - i + 1 ; j ++ ) { if ( arr [ j ] > arr [ j + 1 ] ) { swap ( arr , j , j + 1 ) ; } } } for ( i = 0 ; i < N ; i ++ ) document . write ( arr [ i ] + " " ) ; } var arr = [ 7 , 8 , 4 , 5 , 2 ] ; var a = 1 , b = 4 ; var N = arr . length ; partSort ( arr , N , a , b ) ;
Find the minimum cost to reach destination using a train | A Dynamic Programming based solution to find min cost to reach station N - 1 from station 0. ; A recursive function to find the shortest path from source ' s ' to destination ' d ' . This function returns the smallest possible cost to reach station N - 1 from station 0. ; dist [ i ] stores minimum cost to reach station i from station 0. ; Go through every station and check if using it as an intermediate station gives better path ; Driver program to test above function
let INF = Number . MAX_VALUE , N = 4 ; function minCost ( cost ) { let dist = new Array ( N ) ; dist . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) dist [ i ] = INF ; dist [ 0 ] = 0 ; for ( let i = 0 ; i < N ; i ++ ) for ( let j = i + 1 ; j < N ; j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) dist [ j ] = dist [ i ] + cost [ i ] [ j ] ; return dist [ N - 1 ] ; } let cost = [ [ 0 , 15 , 80 , 90 ] , [ INF , 0 , 40 , 50 ] , [ INF , INF , 0 , 70 ] , [ INF , INF , INF , 0 ] ] ; document . write ( " " + " " + N + " " + minCost ( cost ) ) ;
Number of loops of size k starting from a specific node | Return the Number of ways from a node to make a loop of size K in undirected complete connected graph of N nodes ; Driver code
function numOfways ( n , k ) { let p = 1 ; if ( k % 2 != 0 ) p = - 1 ; return ( Math . pow ( n - 1 , k ) + p * ( n - 1 ) ) / n ; } let n = 4 , k = 2 ; document . write ( numOfways ( n , k ) ) ;
Program to find the largest and smallest ASCII valued characters in a string | function that return the largest alphabet . ; initializing max alphabet to ' a ' ; find largest alphabet ; returning largest element ; function that return the smallest alphabet ; initializing smallest alphabet to ' z ' ; find smallest alphabet ; returning smallest alphabet ; Input String ; Calculating size of the string ; calling functions and print returned value
function largest_alphabet ( a , n ) { let max = ' ' ; for ( let i = 0 ; i < n ; i ++ ) if ( a [ i ] . charCodeAt ( ) > max . charCodeAt ( ) ) max = a [ i ] ; return max ; } function smallest_alphabet ( a , n ) { let min = ' ' ; for ( let i = 0 ; i < n - 1 ; i ++ ) if ( a [ i ] . charCodeAt ( ) < min . charCodeAt ( ) ) min = a [ i ] ; return min ; } let a = " " ; let size = a . length ; document . write ( " " ) ; document . write ( largest_alphabet ( a , size ) + " " ) ; document . write ( smallest_alphabet ( a , size ) ) ;