codeparrot/xlcost-text-to-code huggingface dataset:
Minimize the Sum of all the subarrays made up of the products of same | Javascript Program to implement the above approach ; Function to rearrange the second array such that the sum of its product of same indexed elements from both the arrays is minimized ; Stores ( i - 1 ) * ( n - i ) * a [ i ] for every i - th element ; Updating the value of pro according to the function ; Sort the array in reverse order ; Sort the products ; Updating the ans ; Return the ans ; Driver code ; Function call
var mod = 1000000007 ; function findMinValue ( a , b ) { var n = a . length ; var pro = Array ( n ) ; for ( var i = 0 ; i < n ; ++ i ) { pro [ i ] = ( ( i + 1 ) * ( n - i ) ) ; pro [ i ] *= ( 1 * a [ i ] ) ; ; } b . sort ( ( a , b ) => b - a ) pro . sort ( ( a , b ) => a - b ) var ans = 0 ; for ( var i = 0 ; i < n ; ++ i ) { ans += ( pro [ i ] % mod * b [ i ] ) % mod ; ans %= mod ; } return ans ; } var a = [ 1 , 2 , 3 ] ; var b = [ 2 , 3 , 2 ] ; document . write ( findMinValue ( a , b ) ) ;
Maximum number of unique Triplets such that each element is selected only once | Function that finds maximum number of triplets with different elements ; Map M will store the frequency of each element in the array ; Priority queue of pairs { frequency , value } ; ans will store possible triplets ; Extract top 3 elements ; Make a triplet ; Decrease frequency and push back into priority queue if non - zero frequency ; Print the triplets ; Print the triplets ; Given array arr [ ] ; Function Call
function findTriplets ( ar , n ) { var mp = new Map ( ) ; for ( var x = 0 ; x < n ; x ++ ) { if ( mp . has ( ar [ x ] ) ) mp . set ( ar [ x ] , mp . get ( ar [ x ] ) + 1 ) else mp . set ( ar [ x ] , 1 ) } var pq = [ ] ; for ( var pa of mp ) pq . push ( [ pa [ 1 ] , pa [ 0 ] ] ) ; var ans = [ ] ; pq . sort ( ( a , b ) => { if ( a [ 0 ] == b [ 0 ] ) return a [ 1 ] - b [ 1 ] ; return a [ 0 ] - b [ 0 ] ; } ) ; while ( pq . length >= 3 ) { var ar = Array . from ( Array ( 3 ) , ( ) => Array ( 2 ) . fill ( 0 ) ) ; for ( var x = 0 ; x < 3 ; x ++ ) { ar [ x ] = pq [ pq . length - 1 ] ; pq . pop ( ) ; } ans . push ( [ ar [ 0 ] [ 1 ] , ar [ 1 ] [ 1 ] , ar [ 2 ] [ 1 ] ] ) ; for ( var x = 0 ; x < 3 ; x ++ ) { ar [ x ] [ 0 ] -- ; if ( ar [ x ] [ 0 ] ) pq . push ( ar [ x ] ) ; } pq . sort ( ( a , b ) => { if ( a [ 0 ] == b [ 0 ] ) return a [ 1 ] - b [ 1 ] ; return a [ 0 ] - b [ 0 ] ; } ) ; } document . write ( " " + " " ) ; document . write ( ans . length + " " ) ; for ( var pa of ans ) { for ( var v of pa ) document . write ( v + " " ) ; document . write ( " " ) ; } } var arr = [ 2 , 2 , 3 , 3 , 4 , 4 , 4 , 4 , 5 ] ; var n = arr . length ; findTriplets ( arr , n ) ;
Sort Array such that smallest is at 0 th index and next smallest it at last index and so on | Function to perform the rearrangement ; Initialize variables ; Loop until i crosses j ; This check is to find the minimum values in the ascending order ; Condition to alternatively iterate variable i and j ; Perform swap operation ; Increment i ; Assign the value of min ; Perform swap ; Decrement i ; Assign the value of min ; Prvar the array ; Given Array arr ; Function Call
function rearrange ( a ) { var i = 0 , j = a . length - 1 ; var min = 0 , k , x = 0 , temp ; while ( i < j ) { for ( k = i ; k <= j ; k ++ ) { if ( a [ k ] < a [ min ] ) min = k ; } if ( x % 2 == 0 ) { temp = a [ i ] ; a [ i ] = a [ min ] ; a [ min ] = temp ; i ++ ; min = i ; } else { temp = a [ j ] ; a [ j ] = a [ min ] ; a [ min ] = temp ; j -- ; min = j ; } x ++ ; } for ( i = 0 ; i < a . length ; i ++ ) document . write ( a [ i ] + " " ) ; } var arr = [ 1 , 3 , 3 , 4 , 5 ] ; rearrange ( arr ) ;
Maximum number of elements greater than X after equally distributing subset of array | Function to find the maximum number of elements greater than X by equally distributing ; Sorting the array ; Loop to iterate over the elements of the array ; If no more elements can become larger than x ; Driver Code
function redistribute ( arr , n , x ) { arr . sort ( ) ; arr . reverse ( ) ; let i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( ( sum / ( i + 1 ) ) < x ) { document . write ( i ) ; break ; } } if ( i == n ) document . write ( n ) ; } let arr = [ 5 , 1 , 2 , 1 ] ; let x = 3 ; redistribute ( arr , 4 , x ) ;
Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal | Function to print the remaining element ; Priority queue can be used to construct max - heap ; Insert all element of arr [ ] into priority queue ; Perform operation until heap size becomes 0 or 1 ; Remove largest element ; Remove 2 nd largest element ; If extracted element are not equal ; Find X - Y and push it to heap ; If heap size is 1 , then print the remaining element ; Else print " - 1" ; Given array arr [ ] ; Size of array arr [ ] ; Function Call
function final_element ( arr , n ) { var heap = [ ] ; for ( var i = 0 ; i < n ; i ++ ) heap . push ( arr [ i ] ) ; heap . sort ( ( a , b ) => a - b ) ; while ( heap . length > 1 ) { var X = heap [ heap . length - 1 ] ; heap . pop ( ) ; var Y = heap [ heap . length - 1 ] ; heap . pop ( ) ; if ( X != Y ) { var diff = Math . abs ( X - Y ) ; heap . push ( diff ) ; } heap . sort ( ( a , b ) => a - b ) ; } if ( heap . length == 1 ) { document . write ( heap [ heap . length - 1 ] ) ; } else { document . write ( " " ) ; } } var arr = [ 3 , 5 , 2 , 7 ] ; var n = arr . length ; final_element ( arr , n ) ;
Sort a string according to the frequency of characters | Returns count of character in the string ; Check for vowel ; Function to sort the string according to the frequency ; Vector to store the frequency of characters with respective character ; Inserting frequency with respective character in the vector pair ; Sort the vector , this will sort the pair according to the number of characters ; Print the sorted vector content ; Driver Code
function countFrequency ( str , ch ) { var count = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) if ( str [ i ] == ch ) ++ count ; return count ; } function sortArr ( str ) { var n = str . length ; vp = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { vp [ i ] = [ countFrequency ( str , str [ i ] ) , str [ i ] ] ; } vp . sort ( ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( vp [ i ] [ 1 ] ) ; } let str = " " ; sortArr ( str ) ;
Maximum sum after rearranging the array for K queries | function to sort a array ; Auxiliary array to find the count of each selected elements ; Finding count of every element to be selected ; Making it to 0 - indexing ; Prefix sum array concept is used to obtain the count array ; Iterating over the count array to get the final array ; Variable to store the maximum sum ; Sorting both the arrays ; Loop to find the maximum sum ; Driver code
function arrSort ( a , n ) { var i , j , min , temp ; for ( i = 0 ; i < n - 1 ; i ++ ) { min = i ; for ( j = i + 1 ; j < n ; j ++ ) if ( a [ j ] < a [ min ] ) min = j ; temp = a [ i ] ; a [ i ] = a [ min ] ; a [ min ] = temp ; } } function maxSumArrangement ( A , R , N , M ) { var count = new Array ( N ) ; count . fill ( 0 ) ; for ( var i = 0 ; i < M ; i ++ ) { var l = R [ i ] [ 0 ] , r = R [ i ] [ 1 ] + 1 ; l -- ; r -- ; count [ l ] ++ ; if ( r < N ) count [ r ] -- ; } for ( var i = 1 ; i < N ; ++ i ) { count [ i ] += count [ i - 1 ] ; } var ans = 0 ; count . sort ( ) ; arrSort ( A , N ) ; for ( var i = N - 1 ; i >= 0 ; -- i ) { ans += A [ i ] * count [ i ] ; } return ans ; } var A = [ 2 , 6 , 10 , 1 , 5 , 6 ] ; var R = [ [ 1 , 3 ] , [ 4 , 6 ] , [ 3 , 4 ] ] ; var N = A . length ; var M = R . length ; document . write ( maxSumArrangement ( A , R , N , M ) ) ;
Sum of M maximum distinct digit sum from 1 to N that are factors of K | Function to find the factors of K in N ; Initialise a vector ; Find out the factors of K less than N ; Find the digit sum of each factor ; Sum of digits for each element in vector ; Find the largest M distinct digit sum from the digitSum vector ; Find the sum of last M numbers . ; Find the at most M numbers from N natural numbers whose digit sum is distinct and those M numbers are factors of K . ; Find out the factors of K less than N ; Sum of digits for each element in vector ; Sorting the digitSum vector ; Removing the duplicate elements ; " HashSet " is stores only unique elements ; Finding the sum and returning it ; Driver Code ; Function Call
function findFactors ( n , k ) { let factors = [ ] ; for ( let i = 1 ; i <= Math . sqrt ( k ) ; i ++ ) { if ( k % i == 0 ) { if ( k / i == i && i <= n ) factors . push ( i ) ; else { if ( i <= n ) factors . push ( i ) ; if ( k / i <= n ) factors . push ( k / i ) ; } } } return factors ; } function findDigitSum ( a ) { for ( let i = 0 ; i < a . length ; i ++ ) { let c = 0 ; while ( a [ i ] > 0 ) { c += ( a [ i ] % 10 ) ; a [ i ] = Math . floor ( a [ i ] / 10 ) ; } a [ i ] = c ; } return a ; } function findMMaxDistinctDigitSum ( distinctDigitSum , m ) { let sum = 0 ; for ( let i = distinctDigitSum . length - 1 ; i >= 0 && m > 0 ; i -- , m -- ) sum += distinctDigitSum [ i ] ; return sum ; } function findDistinctMnumbers ( n , k , m ) { let factors = findFactors ( n , k ) ; let digitSum = findDigitSum ( factors ) ; digitSum . sort ( function ( a , b ) { return a - b ; } ) ; let hs1 = new Set ( digitSum ) ; let vect2 = Array . from ( hs1 ) ; return findMMaxDistinctDigitSum ( vect2 , m ) ; } let n = 100 , k = 80 , m = 4 ; document . write ( findDistinctMnumbers ( n , k , m ) ) ;
Sorting objects using In | Partition function which will partition the array and into two parts ; Compare hash values of objects ; Classic quicksort algorithm ; Function to sort and print the objects ; Create a hash table ; As the sorting order is blue objects , red objects and then yellow objects ; Quick sort function ; Printing the sorted array ; Let 's represent objects as strings
function partition ( objects , l , r , hash ) { let j = l - 1 ; let last_element = hash . get ( objects [ r ] ) ; for ( let i = l ; i < r ; i ++ ) { if ( hash . get ( objects [ i ] ) <= last_element ) { j ++ ; let temp = objects [ i ] ; objects [ i ] = objects [ j ] ; objects [ j ] = temp ; } } j ++ ; let temp = objects [ r ] ; objects [ r ] = objects [ j ] ; objects [ j ] = temp ; return j ; } function quicksort ( objects , l , r , hash ) { if ( l < r ) { let mid = partition ( objects , l , r , hash ) ; quicksort ( objects , l , mid - 1 , hash ) ; quicksort ( objects , mid + 1 , r , hash ) ; } } function sortObj ( objects ) { let hash = new Map ( ) ; hash . set ( " " , 1 ) ; hash . set ( " " , 2 ) ; hash . set ( " " , 3 ) ; quicksort ( objects , 0 , objects . length - 1 , hash ) ; for ( let i = 0 ; i < objects . length ; i ++ ) document . write ( objects [ i ] + " " ) ; } let objects = [ " " , " " , " " , " " , " " ] ; sortObj ( objects ) ;
Sort the numbers according to their product of digits | Function to return the product of the digits of n ; Function to sort the array according to the product of the digits of elements ; Vector to store the digit product with respective elements ; Inserting digit product with elements in the vector pair ; Sort the vector , this will sort the pair according to the product of the digits ; Print the sorted vector content ; Driver code
function productOfDigit ( n ) { var product = 1 ; while ( n > 0 ) { product *= n % 10 ; n = Math . floor ( n / 10 ) ; } return product ; } function sortArr ( arr , n ) { var vp = new Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { vp [ i ] . push ( productOfDigit ( arr [ i ] ) ) ; vp [ i ] . push ( arr [ i ] ) ; } vp . sort ( ) ; for ( var i = 0 ; i < n ; i ++ ) document . write ( vp [ i ] [ 1 ] + " " ) ; } var arr = [ 12 , 10 , 102 , 31 , 15 ] ; var n = arr . length ; sortArr ( arr , n ) ;
Minimum steps required to reduce all the elements of the array to zero | Function to return the minimum steps required to reduce all the elements to 0 ; Maximum element from the array ; Driver code
function minSteps ( arr , n ) { let maxVal = Math . max ( ... arr ) ; return maxVal ; } let arr = [ 1 , 2 , 4 ] ; let n = arr . length ; document . write ( minSteps ( arr , n ) ) ;
Maximum water that can be stored between two buildings | Return the maximum water that can be stored ; Check all possible pairs of buildings ; Maximum so far ; Driver program
function maxWater ( height , n ) { let maximum = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let current = ( Math . min ( height [ i ] , height [ j ] ) * ( j - i - 1 ) ) ; maximum = Math . max ( maximum , current ) ; } } return maximum ; } let height = [ 2 , 1 , 3 , 4 , 6 , 5 ] ; let n = height . length ; document . write ( maxWater ( height , n ) ) ;
Check if the given array contains all the divisors of some integer | Function that returns true if arr [ ] contains all the divisors of some integer ; Maximum element from the array ; Vector to store divisors of the maximum element i . e . X ; Store all the divisors of X ; If the lengths of a [ ] and b are different return false ; Sort a [ ] and b ; If divisors are not equal return false ; Driver code
function checkDivisors ( a , n ) { let X = Math . max ( ... a ) ; let b = [ ] ; for ( let i = 1 ; i * i <= X ; i ++ ) { if ( X % i == 0 ) { b . push ( i ) ; if ( parseInt ( X / i ) != i ) b . push ( parseInt ( X / i ) ) ; } } if ( b . length != n ) return false ; a . sort ( ( x , y ) => x - y ) ; b . sort ( ( x , y ) => x - y ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( b [ i ] != a [ i ] ) return false ; } return true ; } let arr = [ 8 , 1 , 2 , 12 , 48 , 6 , 4 , 24 , 16 , 3 ] ; let N = arr . length ; if ( checkDivisors ( arr , N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Kth largest node among all directly connected nodes to the given node in an undirected graph | Function to print Kth node for each node ; Vector to store nodes directly connected to ith node along with their values ; Add edges to the vector along with the values of the node ; Sort neighbors of every node and find the Kth node ; Get the kth node ; If total nodes are < k ; Driver code
function findKthNode ( u , v , n , val , V , k ) { var g = Array . from ( Array ( V ) , ( ) => Array ( ) ) ; for ( var i = 0 ; i < n ; i ++ ) { g [ u [ i ] ] . push ( [ val [ v [ i ] ] , v [ i ] ] ) ; g [ v [ i ] ] . push ( [ val [ u [ i ] ] , u [ i ] ] ) ; } for ( var i = 0 ; i < V ; i ++ ) { if ( g [ i ] . length > 0 ) g [ i ] . sort ( ) ; if ( k <= g [ i ] . length ) document . write ( g [ i ] [ g [ i ] . length - k ] [ 1 ] + " " ) ; else document . write ( " " ) ; } return ; } var V = 3 ; var val = [ 2 , 4 , 3 ] ; var u = [ 0 , 0 , 1 ] ; var v = [ 2 , 1 , 2 ] ; var n = u . length ; var k = 2 ; findKthNode ( u , v , n , val , V , k ) ;
Minimum length of square to contain at least half of the given Coordinates | Function to Calculate Absolute Value ; Function to Calculate the Minimum value of M ; To store the minimum M for each point in array ; Sort the array ; Index at which atleast required point are inside square of length 2 * M ; Driver Code
function mod ( x ) { if ( x >= 0 ) return x ; return - x ; } function findSquare ( n ) { let points = [ [ 1 , 2 ] , [ - 3 , 4 ] , [ 1 , 78 ] , [ - 3 , - 7 ] ] ; let a = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { let x , y ; x = points [ i ] [ 0 ] ; y = points [ i ] [ 1 ] ; a [ i ] = Math . max ( mod ( x ) , mod ( y ) ) ; } a . sort ( function ( a , b ) { return a - b } ) ; let index = ( Math . floor ( n / 2 ) - 1 ) ; document . write ( " " + a [ index ] ) ; } let N ; N = 4 ; findSquare ( N ) ;
Array element with minimum sum of absolute differences | Function to return the minimized sum ; Sort the array ; Median of the array ; Calculate the minimized sum ; Return the required sum ; Driver code
function minSum ( arr , n ) { arr . sort ( ) ; let x = arr [ Math . floor ( n / 2 ) ] ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += Math . abs ( arr [ i ] - x ) ; return sum ; } let arr = [ 1 , 3 , 9 , 3 , 6 ] ; let n = arr . length ; document . write ( minSum ( arr , n ) ) ;
Sort even and odd placed elements in increasing order | Javascript implementation of above approach function to print the odd and even indexed digits ; lists to store the odd and even positioned digits ; traverse through all the indexes in the integer ; if the digit is in odd_index position append it to odd_position list ; else append it to the even_position list ; print the elements in the list in sorted order ; Driver code
function odd_even ( arr , n ) { var odd_indexes = [ ] ; var even_indexes = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) odd_indexes . push ( arr [ i ] ) ; else even_indexes . push ( arr [ i ] ) ; } odd_indexes . sort ( ) ; even_indexes . sort ( ) ; for ( var i = 0 ; i < odd_indexes . length ; i ++ ) document . write ( odd_indexes [ i ] + " " ) ; for ( var i = 0 ; i < even_indexes . length ; i ++ ) document . write ( even_indexes [ i ] + " " ) ; } var arr = [ 3 , 2 , 7 , 6 , 8 ] ; var n = arr . length ; odd_even ( arr , n ) ;
Number of pairs whose sum is a power of 2 | Function to return the count of valid pairs ; Storing occurrences of each element ; Start taking largest element each time ; If element has already been paired ; Find the number which is greater than a [ i ] and power of two ; If there is a number which adds up with a [ i ] to form a power of two ; Edge case when a [ i ] and crr - a [ i ] is same and we have only one occurrence of a [ i ] then it cannot be paired ; Remove already paired elements ; Return the count ; Driver Code
function countPairs ( a , n ) { let map = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { map . set ( a [ i ] , 1 ) ; } let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( map . get ( a [ i ] ) < 1 ) continue ; let cur = 1 ; while ( cur <= a [ i ] ) cur <<= 1 ; if ( map . has ( cur - a [ i ] ) ) { if ( cur - a [ i ] == a [ i ] && map . get ( a [ i ] ) == 1 ) continue ; count ++ ; map . set ( cur - a [ i ] , map . get ( cur - a [ i ] ) - 1 ) ; map . set ( a [ i ] , map . get ( a [ i ] ) - 1 ) ; } } return count ; } let a = [ 3 , 11 , 14 , 5 , 13 ] ; let n = a . length ; document . write ( countPairs ( a , n ) ) ;
Maximizing the elements with a [ i + 1 ] > a [ i ] | returns the number of positions where A ( i + 1 ) is greater than A ( i ) after rearrangement of the array ; Creating a HashMap containing char as a key and occurrences as a value ; Find the maximum frequency ; Driver code
function countMaxPos ( arr ) { let n = arr . length ; let map = new Map ( ) ; for ( let x of arr ) { if ( map . has ( x ) ) map . set ( x , map . get ( x ) + 1 ) ; else map . set ( x , 1 ) ; } let max_freq = 0 ; for ( let entry of map ) max_freq = Math . max ( max_freq , entry [ 1 ] ) ; console . log ( max_freq , n ) return n - max_freq ; } let arr = [ 20 , 30 , 10 , 50 , 40 ] ; document . write ( countMaxPos ( arr ) ) ;
Rearrange all elements of array which are multiples of x in increasing order | Function to sort all the multiples of x from the array in ascending order ; Insert all multiples of 5 to a vector ; Sort the vector ; update the array elements ; Driver code ; Print the result
function sortMultiples ( arr , n , x ) { var v = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % x == 0 ) { v . push ( arr [ i ] ) ; } } v . sort ( ( a , b ) => a - b ) ; var j = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % x == 0 ) arr [ i ] = v [ j ++ ] ; } } var arr = [ 125 , 3 , 15 , 6 , 100 , 5 ] ; var x = 5 ; var n = arr . length ; sortMultiples ( arr , n , x ) ; for ( var i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; }
Minimum increment in the sides required to get non | Javascript Program to find Minimum increment in the sides required to get non - negative area of a triangle ; push the three sides to a array ; sort the array ; check if sum is greater than third side ; Driver Code
function minimumIncrease ( a , b , c ) { var arr = [ a , b , c ] ; arr . sort ( ( a , b ) => a - b ) ; if ( arr [ 0 ] + arr [ 1 ] >= arr [ 2 ] ) return 0 ; else return arr [ 2 ] - ( arr [ 0 ] + arr [ 1 ] ) ; } var a = 3 , b = 5 , c = 10 ; document . write ( minimumIncrease ( a , b , c ) ) ;
Minimum sum of differences with an element in an array | Function to find min sum after operation ; Sort the array ; Pick middle value ; Sum of absolute differences . ; Driver Code
function absSumDidd ( a ) { a . sort ( ( a , b ) => a - b ) ; var midValue = a [ a . length / 2 ] ; var sum = 0 ; for ( var i = 0 ; i < a . length ; i ++ ) { sum = sum + Math . abs ( a [ i ] - midValue ) ; } return sum ; } var arr = [ 5 , 11 , 14 , 10 , 17 , 15 ] ; document . write ( absSumDidd ( arr ) ) ;
Printing frequency of each character just after its consecutive occurrences | Javascript program to print run length encoding of a string ; Counting occurrences of s [ i ] ; Driver code
function printRLE ( s ) { for ( var i = 0 ; i < s . length ; i ++ ) { var count = 1 ; while ( i + 1 < s . length && s . charAt ( i ) == s . charAt ( i + 1 ) ) { i ++ ; count ++ ; } document . write ( s . charAt ( i ) + " " + count + " " ) ; } document . write ( " " ) ; } printRLE ( " " ) ; printRLE ( " " ) ;
Word Ladder ( Length of shortest chain to reach a target word ) | Returns length of shortest chain to reach ' target ' from ' start ' using minimum number of adjacent moves . D is dictionary ; If the target String is not present in the dictionary ; To store the current chain length and the length of the words ; Push the starting word into the queue ; While the queue is non - empty ; Increment the chain length ; Current size of the queue ; Since the queue is being updated while it is being traversed so only the elements which were already present in the queue before the start of this loop will be traversed for now ; Remove the first word from the queue ; For every character of the word ; Retain the original character at the current position ; Replace the current character with every possible lowercase alphabet ; If the new word is equal to the target word ; Remove the word from the set if it is found in it ; And push the newly generated word which will be a part of the chain ; Restore the original character at the current position ; make dictionary
function shortestChainLen ( start , target , D ) { if ( start == target ) return 0 ; if ( ! D . has ( target ) ) return 0 ; let level = 0 , wordlength = start . length ; let Q = [ ] ; Q . push ( start ) ; while ( Q . length != 0 ) { ++ level ; let sizeofQ = Q . length ; for ( let i = 0 ; i < sizeofQ ; ++ i ) { let word = Q [ 0 ] . split ( " " ) ; Q . shift ( ) ; for ( let pos = 0 ; pos < wordlength ; ++ pos ) { let orig_char = word [ pos ] ; for ( let c = ' ' . charCodeAt ( 0 ) ; c <= ' ' . charCodeAt ( 0 ) ; ++ c ) { word [ pos ] = String . fromCharCode ( c ) ; if ( word . join ( " " ) == target ) return level + 1 ; if ( ! D . has ( word . join ( " " ) ) ) continue ; D . delete ( word . join ( " " ) ) ; Q . push ( word . join ( " " ) ) ; } word [ pos ] = orig_char ; } } } return 0 ; } let D = new Set ( ) ; D . add ( " " ) ; D . add ( " " ) ; D . add ( " " ) ; D . add ( " " ) ; D . add ( " " ) ; D . add ( " " ) ; D . add ( " " ) ; let start = " " ; let target = " " ; document . write ( " " + shortestChainLen ( start , target , D ) ) ;
Prim 's algorithm using priority_queue in STL | If v is not in MST and weight of ( u , v ) is smaller than current key of v ; Updating key of v
if ( inMST [ v ] == false && key [ v ] > weight ) { key [ v ] = weight ; value = [ key [ v ] , v ] ; pq . push ( value ) ; parent [ v ] = u ; }