codeparrot/xlcost-text-to-code huggingface dataset:
Count nodes having smallest value in the path from root to itself in a Binary Tree | Structure of tree node ; Function to create new tree node ; Function to find the total number of required nodes ; If current node is null then return to the parent node ; Check if current node value is less than or equal to minNodeVal ; Update the value of minNodeVal ; Update the count ; Go to the left subtree ; Go to the right subtree ; Binary Tree creation 8 / \ / \ 6 5 / \ / \ / \ / \ 6 7 3 9 ; Function Call ; Print the result
class Node { constructor ( key ) { this . left = null ; this . right = null ; this . key = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return temp ; } let ans ; function countReqNodes ( root , minNodeVal ) { if ( root == null ) return ; if ( root . key <= minNodeVal ) { minNodeVal = root . key ; ans ++ ; } countReqNodes ( root . left , minNodeVal ) ; countReqNodes ( root . right , minNodeVal ) ; } let root = newNode ( 8 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 6 ) ; root . left . right = newNode ( 7 ) ; root . right . left = newNode ( 3 ) ; root . right . right = newNode ( 9 ) ; let minNodeVal = Number . MAX_VALUE ; ans = 0 ; countReqNodes ( root , minNodeVal ) ; document . write ( ans ) ;
Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | Function to check if a string is palindrome or not ; Start and end of the string ; Iterate the string until i > j ; If there is a mismatch ; Increment first pointer and decrement the other ; Given string is a palindrome ; Function two check if the strings can be combined to form a palindrome ; Initialize array of characters ; Stores character of string in the character array ; Find left and right parts of strings a and b ; Substring a [ j ... i - 1 ] ; Substring b [ j ... i - 1 ] ; Substring a [ i ... n ] ; Substring b [ i ... n ] ; Check is left part of a + right part of b or vice versa is a palindrome ; Print the result ; Otherwise ; Driver Code
function isPalindrome ( str ) { var i = 0 , j = str . length - 1 ; while ( i < j ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; j -- ; } return true ; } function formPalindrome ( a , b , n ) { var aa = new Array ( n + 2 ) ; var bb = new Array ( n + 2 ) ; for ( var i = 0 ; i < n + 2 ; i ++ ) { aa [ i ] = ' ' ; bb [ i ] = ' ' ; } for ( var i = 1 ; i <= n ; i ++ ) { aa [ i ] = a [ i - 1 ] ; bb [ i ] = b [ i - 1 ] ; } var ok = Boolean ( false ) ; for ( var i = 0 ; i <= n + 1 ; i ++ ) { var la = " " ; var ra = " " ; var lb = " " ; var rb = " " ; for ( var j = 1 ; j <= i - 1 ; j ++ ) { if ( aa [ j ] == ' ' ) la += " " ; else la += aa [ j ] ; if ( bb [ j ] == ' ' ) lb += " " ; else lb += bb [ j ] ; } for ( var j = i ; j <= n + 1 ; j ++ ) { if ( aa [ j ] == ' ' ) ra += " " ; else ra += aa [ j ] ; if ( bb [ j ] == ' ' ) rb += " " ; else rb += bb [ j ] ; } if ( isPalindrome ( la + rb ) || isPalindrome ( lb + ra ) ) { ok = true ; break ; } } if ( ok ) document . write ( " " ) ; else document . write ( " " ) ; } var A = " " ; var B = " " ; var N = 4 ; formPalindrome ( A , B , N ) ;
Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2 | Function to return minimum element to be added in array B so that array A become subsequence of array B ; Stores indices of the array elements ; Iterate over the array ; Store the indices of the array elements ; Stores the LIS ; Check if element B [ i ] is in array A [ ] ; Perform Binary Search ; Find the value of mid m ; Update l and r ; If found better element ' e ' for pos r + 1 ; Otherwise , extend the current subsequence ; Return the answer ; Given arrays ; Function Call
function minElements ( A , B , N , M ) { var map = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { map . set ( A [ i ] , i ) ; } var subseq = [ ] ; var l = 0 , r = - 1 ; for ( var i = 0 ; i < M ; i ++ ) { if ( map . has ( B [ i ] ) ) { var e = map . get ( B [ i ] ) ; while ( l <= r ) { var m = l + parseInt ( ( r - l ) / 2 ) ; if ( subseq [ m ] < e ) l = m + 1 ; else r = m - 1 ; } if ( r + 1 < subseq . length ) { subseq [ r + 1 ] = e ; } else { subseq . push ( e ) ; } l = 0 ; r = subseq . length - 1 ; } } return N - subseq . length ; } var A = [ 1 , 2 , 3 , 4 , 5 ] ; var B = [ 2 , 5 , 6 , 4 , 9 , 12 ] ; var M = A . length ; var N = B . length ; document . write ( minElements ( A , B , M , N ) ) ;
Detect a negative cycle in a Graph using Shortest Path Faster Algorithm | Javascript program for the above approach ; Stores the adjacency list of the given graph ; Create Adjacency List ; Stores the distance of all reachable vertex from source ; Check if vertex is present in queue or not ; Counts the relaxation for each vertex ; Distance from src to src is 0 ; Create a queue ; Push source in the queue ; Mark source as visited ; Front vertex of Queue ; Relaxing all edges of vertex from the Queue ; Update the dist [ v ] to minimum distance ; If vertex v is in Queue ; Negative cycle ; No cycle found ; Number of vertices ; Given source node src ; Number of Edges ; Given Edges with weight ; If cycle is present
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } function sfpa ( V , src , Edges , M ) { var g = Array . from ( Array ( V ) , ( ) => Array ( ) ) ; for ( var i = 0 ; i < M ; i ++ ) { var u = Edges [ i ] [ 0 ] ; var v = Edges [ i ] [ 1 ] ; var w = Edges [ i ] [ 2 ] ; g [ u ] . push ( new pair ( v , w ) ) ; } var dist = Array ( V ) ; for ( var i = 0 ; i < V ; i ++ ) dist [ i ] = 1000000000 ; var inQueue = Array ( V ) . fill ( false ) ; var cnt = Array ( V ) . fill ( 0 ) ; dist [ src ] = 0 ; var q = [ ] ; q . push ( src ) ; inQueue [ src ] = true ; while ( q . length != 0 ) { var u = q [ 0 ] ; q . shift ( ) ; inQueue [ u ] = false ; for ( var x of g [ u ] ) { var v = x . first ; var cost = x . second ; if ( dist [ v ] > dist [ u ] + cost ) { dist [ v ] = dist [ u ] + cost ; if ( ! inQueue [ v ] ) { q . push ( v ) ; inQueue [ v ] = true ; cnt [ v ] ++ ; if ( cnt [ v ] >= V ) return true ; } } } } return false ; } var N = 4 ; var src = 0 ; var M = 4 ; var Edges = [ [ 0 , 1 , 1 ] , [ 1 , 2 , - 1 ] , [ 2 , 3 , - 1 ] , [ 3 , 0 , - 1 ] ] ; if ( sfpa ( N , src , Edges , M ) == true ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Sum of all pair shortest paths in a Tree | Javascript program for the above approach ; Function that performs the Floyd Warshall to find all shortest paths ; Initialize the distance matrix ; Pick all vertices as source one by one ; Pick all vertices as destination for the above picked source ; If vertex k is on the shortest path from i to j then update dist [ i ] [ j ] ; Sum the upper diagonal of the shortest distance matrix ; Traverse the given dist [ ] [ ] ; Add the distance ; Return the final sum ; Function to generate the tree ; Add edges ; Get source and destination with weight ; Add the edges ; Perform Floyd Warshal Algorithm ; Number of Vertices ; Number of Edges ; Given Edges with weight ; Function Call
let INF = 99999 ; function floyd_warshall ( graph , V ) { let dist = new Array ( V ) ; for ( let i = 0 ; i < V ; i ++ ) { dist [ i ] = new Array ( V ) ; } let i , j , k ; for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { dist [ i ] [ j ] = graph [ i ] [ j ] ; } } for ( k = 0 ; k < V ; k ++ ) { for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) { dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ] ; } } } } let sum = 0 ; for ( i = 0 ; i < V ; i ++ ) { for ( j = i + 1 ; j < V ; j ++ ) { sum += dist [ i ] [ j ] ; } } return sum ; } function sumOfshortestPath ( N , E , edges ) { let g = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { g [ i ] = new Array ( N ) ; for ( let j = 0 ; j < N ; j ++ ) { g [ i ] [ j ] = INF ; } } for ( let i = 0 ; i < E ; i ++ ) { let u = edges [ i ] [ 0 ] ; let v = edges [ i ] [ 1 ] ; let w = edges [ i ] [ 2 ] ; g [ u ] [ v ] = w ; g [ v ] [ u ] = w ; } return floyd_warshall ( g , N ) ; } let N = 4 ; let E = 3 ; let Edges = [ [ 0 , 1 , 1 ] , [ 1 , 2 , 2 ] , [ 2 , 3 , 3 ] ] ; document . write ( sumOfshortestPath ( N , E , Edges ) ) ;
Minimum peak elements from an array by their repeated removal at every iteration of the array | Function to return the list of minimum peak elements ; Length of original list ; Initialize resultant list ; Traverse each element of list ; Length of original list after removing the peak element ; Traverse new list after removal of previous min peak element ; Update min and index , if first element of list > next element ; Update min and index , if last element of list > previous one ; Update min and index , if list has single element ; Update min and index , if current element > adjacent elements ; Remove current min peak element from list ; Insert min peak into resultant list ; Print resultant list ; Given array arr [ ] ; Function call
function minPeaks ( list ) { var n = list . length ; var result = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { var min = 1000000000 ; var index = - 1 ; var size = list . length ; for ( var j = 0 ; j < size ; j ++ ) { if ( j == 0 && j + 1 < size ) { if ( list [ j ] > list [ j + 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } else if ( j == size - 1 && j - 1 >= 0 ) { if ( list [ j ] > list [ j - 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } else if ( size == 1 ) { min = list [ j ] ; index = j ; } else if ( list [ j ] > list [ j - 1 ] && list [ j ] > list [ j + 1 ] && min > list [ j ] ) { min = list [ j ] ; index = j ; } } list . splice ( index , 1 ) ; result . push ( min ) ; } document . write ( " " ) ; for ( var i = 0 ; i < result . length ; i ++ ) { document . write ( result [ i ] + " " ) ; } document . write ( " " ) ; } var arr = [ 1 , 9 , 7 , 8 , 2 , 6 ] ; minPeaks ( arr ) ;
Search an element in a reverse sorted array | Function to search if element X is present in reverse sorted array ; Store the first index of the subarray in which X lies ; Store the last index of the subarray in which X lies ; Store the middle index of the subarray ; Check if value at middle index of the subarray equal to X ; Element is found ; If X is smaller than the value at middle index of the subarray ; Search in right half of subarray ; Search in left half of subarray ; If X not found ; Driver Code
function binarySearch ( arr , N , X ) { let start = 0 ; let end = N ; while ( start <= end ) { let mid = Math . floor ( start + ( end - start ) / 2 ) ; if ( X == arr [ mid ] ) { return mid ; } else if ( X < arr [ mid ] ) { start = mid + 1 ; } else { end = mid - 1 ; } } return - 1 ; } let arr = [ 5 , 4 , 3 , 2 , 1 ] ; let N = arr . length ; let X = 5 ; document . write ( binarySearch ( arr , N , X ) ) ;
Construct an AP series consisting of A and B having minimum possible Nth term | Function to check if both a and b are present in the AP series or not ; Iterate over the array arr [ ] ; If a is present ; If b is present ; If both are present ; Otherwise ; Function to print all the elements of the Arithmetic Progression ; Function to construct AP series consisting of A and B with minimum Nth term ; Stores the resultant series ; Initialise ans [ i ] as let_MAX ; Maintain a smaller than b ; swap ( a and b ) ; Difference between a and b ; Check for all possible combination of start and common difference d ; Initialise arr [ 0 ] as start ; Check if both a and b are present or not and the Nth term is the minimum or not ; Update the answer ; Print the resultant array ; Driver Code ; Function call
function check_both_present ( arr , N , a , b ) { let f1 = false , f2 = false ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == a ) { f1 = true ; } if ( arr [ i ] == b ) { f2 = true ; } } if ( f1 && f2 ) { return true ; } else { return false ; } } function print_array ( ans , N ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } function build_AP ( N , a , b ) { let arr = Array ( N ) . fill ( 0 ) ; let ans = Array ( N ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) ans [ i ] = Number . MAX_VALUE ; let flag = 0 ; if ( a > b ) { a += ( b - ( b = a ) ) ; } let diff = b - a ; for ( let start = 1 ; start <= a ; start ++ ) { for ( let d = 1 ; d <= diff ; d ++ ) { arr [ 0 ] = start ; for ( let i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i - 1 ] + d ; } if ( check_both_present ( arr , N , a , b ) && arr [ N - 1 ] < ans [ N - 1 ] ) { for ( let i = 0 ; i < N ; i ++ ) { ans [ i ] = arr [ i ] ; } } } } print_array ( ans , N ) ; } let N = 5 , A = 20 , B = 50 ; build_AP ( N , A , B ) ;
Find the word from a given sentence having given word as prefix | Function to find the position of the string having word as prefix ; Initialize an ArrayList ; Extract words from the sentence for ( String e : a ) ; Traverse each word ; Traverse the characters of word ; If prefix does not match ; Otherwise ; Return the word ; Return - 1 if not present ; Driver code
function isPrefixOfWord ( sentence , Word ) { var a = sentence . split ( " " ) ; var v = [ ] ; for ( var i = 0 ; i < a . length ; i ++ ) { v . push ( a [ i ] ) ; } for ( var i = 0 ; i < v . length ; i ++ ) { for ( var j = 0 ; j < v [ i ] . length ; j ++ ) { if ( v [ i ] . charAt ( j ) != Word [ j ] ) break ; else if ( j == Word . length - 1 ) return v [ i ] ; } } return " " ; } var s = " " ; var word = " " ; document . write ( isPrefixOfWord ( s , word ) ) ;
Length of longest increasing absolute even subsequence | Function to find the longest increasing absolute even subsequence ; Length of arr ; Stores length of required subsequence ; Traverse the array ; Traverse prefix of current array element ; Check if the subsequence is LIS and have even absolute difference of adjacent pairs ; Update lis [ ] ; Stores maximum length ; Find the length of longest absolute even subsequence ; Return the maximum length of absolute even subsequence ; Given array arr [ ] and brr [ ] ; Function call
function EvenLIS ( arr ) { let n = arr . length ; let lis = new Array ( n ) . fill ( 1 ) ; for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j < i ; j ++ ) { if ( Math . abs ( arr [ i ] ) > Math . abs ( arr [ j ] ) && Math . abs ( arr [ i ] ) % 2 == 0 && Math . abs ( arr [ j ] ) % 2 == 0 && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } } let maxlen = 0 ; for ( let i = 0 ; i < n ; i ++ ) maxlen = Math . max ( maxlen , lis [ i ] ) ; document . write ( maxlen ) ; } let arrr = [ 11 , - 22 , 43 , - 54 , 66 , 5 ] ; let N = arrr . length ; EvenLIS ( arrr ) ;
Count balanced nodes present in a binary tree | JavaScript program for the above approach ; Structure of a Tree Node ; Function to get the sum of left subtree and right subtree ; Base case ; Store the sum of left subtree ; Store the sum of right subtree ; Check if node is balanced or not ; Increase count of balanced nodes ; Return subtree sum ; Insert nodes in tree ; Store the count of balanced nodes
let res = 0 ; class Node { constructor ( val ) { this . left = null ; this . right = null ; this . data = val ; } } function Sum ( root ) { if ( root == null ) { return 0 ; } let leftSubSum = Sum ( root . left ) ; let rightSubSum = Sum ( root . right ) ; if ( root . left != null && root . right != null && leftSubSum == rightSubSum ) res += 1 ; return root . data + leftSubSum + rightSubSum ; } let root = new Node ( 9 ) ; root . left = new Node ( 2 ) ; root . left . left = new Node ( - 1 ) ; root . left . right = new Node ( 3 ) ; root . right = new Node ( 4 ) ; root . right . right = new Node ( 0 ) ; res = 0 ; Sum ( root ) ; document . write ( res ) ;
Print alternate nodes from all levels of a Binary Tree | Structure of a Node ; Print alternate nodes of a binary tree ; Store nodes of each level ; Store count of nodes of current level ; Print alternate nodes of the current level ; If left child exists ; Store left child ; If right child exists ; Store right child ; Driver Code ; Create a tree
class Node { constructor ( val ) { this . left = null ; this . right = null ; this . data = val ; } } function PrintAlternate ( root ) { let Q = [ ] ; Q . push ( root ) ; while ( Q . length > 0 ) { let N = Q . length ; for ( let i = 0 ; i < N ; i ++ ) { let temp = Q [ 0 ] ; Q . shift ( ) ; if ( i % 2 == 0 ) { document . write ( temp . data + " " ) ; } if ( temp . left != null ) { Q . push ( temp . left ) ; } if ( temp . right != null ) { Q . push ( temp . right ) ; } } document . write ( " " ) ; } } let root ; root = new Node ( 71 ) ; root . left = new Node ( 88 ) ; root . right = new Node ( 99 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 11 ) ; root . right . left . right = new Node ( 13 ) ; root . right . right . left = new Node ( 14 ) ; PrintAlternate ( root ) ;
Count subarrays for every array element in which they are the minimum | Function to required count subarrays ; For storing count of subarrays ; For finding next smaller element left to a element if there is no next smaller element left to it than taking - 1. ; For finding next smaller element right to a element if there is no next smaller element right to it than taking n . ; Taking exact boundaries in which arr [ i ] is minimum ; Similarly for right side ; Driver code ; Given array arr [ ] ; Function call
function countingSubarray ( arr , n ) { let a = new Array ( n ) ; let nsml = new Array ( n ) ; nsml . fill ( - 1 ) ; let nsmr = new Array ( n ) ; nsmr . fill ( n ) ; let st = [ ] ; for ( let i = n - 1 ; i >= 0 ; i -- ) { while ( st . length > 0 && arr [ st [ st . length - 1 ] ] >= arr [ i ] ) st . pop ( ) ; nsmr [ i ] = ( st . length > 0 ) ? st [ st . length - 1 ] : n ; st . push ( i ) ; } while ( st . length > 0 ) st . pop ( ) ; for ( let i = 0 ; i < n ; i ++ ) { while ( st . length > 0 && arr [ st [ st . length - 1 ] ] >= arr [ i ] ) st . pop ( ) ; nsml [ i ] = ( st . length > 0 ) ? st [ st . length - 1 ] : - 1 ; st . push ( i ) ; } for ( let i = 0 ; i < n ; i ++ ) { nsml [ i ] ++ ; nsmr [ i ] -- ; let r = nsmr [ i ] - i + 1 ; let l = i - nsml [ i ] + 1 ; a [ i ] = r * l ; } return a ; } let N = 5 ; let arr = [ 3 , 2 , 4 , 1 , 5 ] ; let a = countingSubarray ( arr , N ) ; document . write ( " " ) ; let n = a . length - 1 ; for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ; document . write ( a [ n ] + " " ) ;
Lexicographically smallest permutation of a string that contains all substrings of another string | Function to reorder the String B to contain all the subStrings of A ; Find length of Strings ; Initialize array to count the frequencies of the character ; Counting frequencies of character in B ; Find remaining character in B ; Declare the reordered String ; Loop until freq [ j ] > 0 ; Decrement the value from freq array ; Check if A [ j ] > A [ 0 ] ; Check if A [ j ] < A [ 0 ] ; Append the remaining characters to the end of the result ; Push all the values from frequency array in the answer ; Return the answer ; Driver Code Given Strings A and B ; Function Call
function reorderString ( A , B ) { let size_a = A . length ; let size_b = B . length ; let freq = new Array ( 300 ) ; for ( let i = 0 ; i < 300 ; i ++ ) { freq [ i ] = 0 ; } for ( let i = 0 ; i < size_b ; i ++ ) freq [ B [ i ] . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < size_a ; i ++ ) freq [ A [ i ] . charCodeAt ( 0 ) ] -- ; for ( let j = ' ' . charCodeAt ( 0 ) ; j <= ' ' . charCodeAt ( 0 ) ; j ++ ) { if ( freq [ j ] < 0 ) return " " ; } let answer = " " ; for ( let j = ' ' . charCodeAt ( 0 ) ; j < A [ 0 ] . charCodeAt ( 0 ) ; j ++ ) while ( freq [ j ] > 0 ) { answer += j ; freq [ j ] -- ; } let first = A [ 0 ] ; for ( let j = 0 ; j < size_a ; j ++ ) { if ( A [ j ] > A [ 0 ] ) break ; if ( A [ j ] < A [ 0 ] ) { answer += ( A ) . join ( " " ) ; A = new Array ( A . length ) ; break ; } } while ( freq [ first ] > 0 ) { answer += ( String . fromCharCode ( first ) ) ; -- freq [ first ] ; } answer += ( A ) . join ( " " ) ; for ( let j = ' ' . charCodeAt ( 0 ) ; j <= ' ' . charCodeAt ( 0 ) ; j ++ ) while ( freq [ j ] -- > 0 ) answer += String . fromCharCode ( j ) ; return answer ; } let A = " " ; let B = " " ; document . write ( reorderString ( A . split ( " " ) , B . split ( " " ) ) ) ;
Find the interval which contains maximum number of concurrent meetings | Function to find time slot of maximum concurrent meeting ; Sort array by start time of meeting ; Declare Minheap ; Insert first meeting end time ; Initialize max_len , max_start and max_end ; Traverse over sorted array to find required slot ; Pop all meetings that end before current meeting ; Push current meeting end time ; Update max_len , max_start and max_end if size of queue is greater than max_len ; Print slot of maximum concurrent meeting ; Given array of meetings ; Function Call
function maxConcurrentMeetingSlot ( meetings ) { meetings . sort ( function ( a , b ) { return ( a [ 0 ] != b [ 0 ] ) ? a [ 0 ] - b [ 0 ] : a [ 1 ] - b [ 1 ] } ) ; let pq = [ ] ; pq . push ( meetings [ 0 ] [ 1 ] ) ; pq . sort ( function ( a , b ) { return a - b ; } ) ; let max_len = 0 , max_start = 0 ; let max_end = 0 ; for ( let k = 0 ; k < meetings . length ; k ++ ) { while ( pq . length != 0 && meetings [ k ] [ 0 ] >= pq [ 0 ] ) pq . shift ( ) ; pq . push ( meetings [ k ] [ 1 ] ) ; pq . sort ( function ( a , b ) { return a - b ; } ) ; if ( pq . length > max_len ) { max_len = pq . length ; max_start = meetings [ k ] [ 0 ] ; max_end = pq [ 0 ] ; } } document . write ( max_start + " " + max_end + " " ) ; } let meetings = [ [ 100 , 200 ] , [ 50 , 300 ] , [ 300 , 400 ] ] ; maxConcurrentMeetingSlot ( meetings ) ;
Number from a range [ L , R ] having Kth minimum cost of conversion to 1 by given operations | Function to calculate the cost ; Base case ; Even condition ; Odd condition ; Return cost ; Function to find Kth element ; Array to store the costs ; Sort the array based on cost ; Given range and6 K ; Function call
function func ( n ) { var count = 0 ; if ( n == 2 n == 1 ) return 1 ; if ( n % 2 == 0 ) count = 1 + func ( n / 2 ) ; if ( n % 2 != 0 ) count = 1 + func ( n * 3 + 1 ) ; return count ; } function findKthElement ( l , r , k ) { var arr = [ ] ; for ( var i = l ; i <= r ; i ++ ) arr . push ( i ) ; var result = [ ] ; arr . forEach ( i => { result . push ( [ i , func ( i ) ] ) ; } ) ; result . sort ( ) ; document . write ( result [ k - 1 ] [ 0 ] ) ; } var l = 12 ; var r = 15 ; var k = 2 ; findKthElement ( l , r , k ) ;
Check if a string represents a hexadecimal number or not | Function to check if the string represents a hexadecimal number ; Size of string ; Iterate over string ; Check if the character is invalid ; Print true if all characters are valid ; Given string ; Function Call
function checkHex ( s ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { let ch = s [ i ] ; if ( ( ch < ' ' ch > ' ' ) && ( ch < ' ' ch > ' ' ) ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } let s = " " ; checkHex ( s ) ;
Longest subsequence forming an Arithmetic Progression ( AP ) | Function that finds the longest arithmetic subsequence having the same absolute difference ; Stores the length of sequences having same difference ; Stores the resultant length ; Iterate over the array ; Update length of subsequence ; Return res ; Given array arr [ ] ; Function Call
function lenghtOfLongestAP ( A , n ) { var dp = new Map ( ) ; var res = 2 ; for ( var i = 0 ; i < n ; ++ i ) { for ( var j = i + 1 ; j < n ; ++ j ) { var d = A [ j ] - A [ i ] ; if ( dp . has ( d ) ) { if ( dp . get ( d ) . has ( i ) ) { var tmp = dp . get ( d ) ; tmp . set ( j , dp . get ( d ) . get ( i ) + 1 ) ; } else { var tmp = new Map ( ) ; tmp . set ( j , 2 ) ; dp . set ( d , tmp ) ; } } else { var tmp = new Map ( ) ; tmp . set ( j , 2 ) ; dp . set ( d , tmp ) ; } res = Math . max ( res , dp . get ( d ) . get ( j ) ) ; } } return res ; } var arr = [ 20 , 1 , 15 , 3 , 10 , 5 , 8 ] ; var N = arr . length ; document . write ( lenghtOfLongestAP ( arr , N ) ) ;