codeparrot/xlcost-text-to-code huggingface dataset:
Check if given intervals can be made non | Function to check if two intervals overlap with each other ; Condition to check if the intervals overlap ; Function to check if there is a existing overlapping intervals ; Path compression ; Union of two intervals Returns True if there is a overlapping with the same another interval ; Both have same another overlapping interval ; Function to check if the intervals can be added by X to form non - overlapping intervals ; If the intervals overlaps we will union them ; There is no cycle ; Driver Code
function checkOverlapping ( a , b ) { if ( a [ 0 ] < b [ 0 ] ) { var temp = a [ 0 ] ; a [ 0 ] = b [ 0 ] ; b [ 0 ] = temp ; temp = a [ 1 ] ; a [ 1 ] = b [ 1 ] ; b [ 1 ] = temp ; } if ( b [ 0 ] <= a [ 0 ] && a [ 0 ] <= b [ 1 ] ) return true ; return false ; } function find ( a , i ) { if ( a [ i ] == i ) { return i ; } a [ i ] = find ( a , a [ i ] ) ; return a [ i ] ; } function union ( a , x , y ) { var xs = find ( a , x ) ; var ys = find ( a , y ) ; if ( xs == ys ) { return true ; } a [ ys ] = xs ; return false ; } function checkNonOverlapping ( arr , n ) { var dsu = [ ] ; for ( var i = 0 ; i < n + 1 ; i ++ ) { dsu . push ( i ) ; } for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { if ( checkOverlapping ( arr [ i ] , arr [ j ] ) ) { if ( union ( dsu , i , j ) ) { return false ; } } } } return true ; } var arr = Array ( ) ; arr . push ( [ 1 , 4 ] ) ; arr . push ( [ 2 , 2 ] ) ; arr . push ( [ 2 , 3 ] ) ; var n = arr . length ; if ( checkNonOverlapping ( arr , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Number formed after K times repeated addition of smallest divisor of N | JavaScript program to find the Kth number formed after repeated addition of smallest divisor of N ; If N is even ; If N is odd ; Add smallest divisor to N ; Updated N is even ; Driver code
function FindValue ( N , K ) { if ( N % 2 == 0 ) { N = N + 2 * K ; } else { let i ; for ( i = 2 ; i < Math . sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) break ; } N = N + i ; N = N + 2 * ( K - 1 ) ; } document . write ( N + " " ) ; } let N = 9 ; let K = 4 ; FindValue ( N , K ) ;
Find last digit in factorial | JavaScript program to find last digit in factorial n . ; Explicitly handle all numbers less than or equal to 4 ; For all numbers greater than 4 the last digit is 0 ; Driver code
function lastDigitFactorial ( n ) { if ( n == 0 ) return 1 ; else if ( n <= 2 ) return n ; else if ( n == 3 ) return 6 ; else if ( n == 4 ) return 4 ; else return 0 ; } document . write ( lastDigitFactorial ( 6 ) ) ;
Find the sum of the first Nth Heptadecagonal Number | Function to find the N - th heptadecagonal number ; Formula to calculate nth heptadecagonal number ; Function to find the sum of the first N heptadecagonal numbers ; Variable to store the sum ; Iterating from 1 to N ; Finding the sum ; Driver code
function heptadecagonal_num ( n ) { return ( ( 15 * n * n ) - 13 * n ) / 2 ; } function sum_heptadecagonal_num ( n ) { let summ = 0 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { summ += heptadecagonal_num ( i ) ; } return summ ; } let n = 5 ; document . write ( sum_heptadecagonal_num ( n ) ) ;
Program to check if N is a Centered Pentagonal Number or not | Function to check if number N is a centered pentagonal number ; Condition to check if N is a centered pentagonal number ; Given number ; Function call
function isCenteredpentagonal ( N ) { var n = ( ( 5 + Math . sqrt ( 40 * N - 15 ) ) / 10 ) ; return ( n - parseInt ( n ) == 0 ) ; } var N = 6 ; if ( isCenteredpentagonal ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Program to check if N is a Dodecagonal Number | Function to check if number N is a dodecagonal number or not ; Condition to check if the N is a dodecagonal number ; Given number ; Function call
function isdodecagonal ( N ) { let n = ( 4 + Math . sqrt ( 20 * N + 16 ) ) / 10 ; return ( n - parseInt ( n ) ) == 0 ; } let N = 12 ; if ( isdodecagonal ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Logarithm tricks for Competitive Programming | Function to return the highest power of k less than or equal to n ; Function to return the smallest power of k greater than or equal to n ; Driver code
function prevPowerofK ( n , k ) { let p = parseInt ( Math . log ( n ) / Math . log ( k ) , 10 ) ; return Math . pow ( k , p ) ; } function nextPowerOfK ( n , k ) { return prevPowerofK ( n , k ) * k ; } let N = 7 ; let K = 2 ; document . write ( prevPowerofK ( N , K ) + " " ) ; document . write ( nextPowerOfK ( N , K ) ) ;
Sum of all composite numbers lying in the range [ L , R ] for Q queries | Prefix array to precompute the sum of all composite numbers ; Function that return number num if num is composite else return 0 ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to precompute the sum of all Composite numbers upto 10 ^ 5 ; isComposite ( ) return the number i if i is Composite else return 0 ; Function to print the sum for each query ; Function to print sum of all Composite numbers between [ L , R ] ; Function that pre computes the sum of all Composite numbers ; Iterate over all Queries to print the sum ; Queries ; Function that print the the sum of all composite number in Range [ L , R ]
var pref = Array ( 100001 ) . fill ( 0 ) ; function isComposite ( n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 0 ; if ( n % 2 == 0 n % 3 == 0 ) return n ; for ( var i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return n ; return 0 ; } function preCompute ( ) { for ( var i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isComposite ( i ) ; } } function printSum ( L , R ) { document . write ( pref [ R ] - pref [ L - 1 ] + " " ) ; } function printSumComposite ( arr , Q ) { preCompute ( ) ; for ( var i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } var Q = 2 ; var arr = [ [ 10 , 13 ] , [ 12 , 21 ] ] ; printSumComposite ( arr , Q ) ;
Probability of getting a perfect square when a random number is chosen in a given range | Function to return the probability of getting a perfect square number in a range ; Count of perfect squares ; Total numbers in range l to r ; Calculating probability ; Driver code ; Function Call
function findProb ( l , r ) { var countOfPS = ( Math . floor ( Math . sqrt ( r ) ) - Math . ceil ( Math . sqrt ( l ) ) + 1 ) ; var total = r - l + 1 ; var prob = countOfPS / total ; return prob ; } var L = 16 , R = 25 ; document . write ( findProb ( L , R ) ) ;
Find the length of factorial of a number in any given base | Returns the number of digits present in n ! in base b Since the result can be large long long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; calling findDigits ( Number , Base )
function findDigits ( n , b ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; var M_PI = 3.141592 ; var M_E = 2.7182 ; var x = ( ( n * Math . log10 ( n / M_E ) + Math . log10 ( 2 * M_PI * n ) / 2.0 ) ) / ( Math . log10 ( b ) ) ; return Math . floor ( x ) + 1 ; } document . write ( findDigits ( 4 , 16 ) + " " ) ; document . write ( findDigits ( 5 , 8 ) + " " ) ; document . write ( findDigits ( 12 , 16 ) + " " ) ; document . write ( findDigits ( 19 , 13 ) + " " ) ;
Sum of all Non | Array to precompute the sum of non - fibonacci numbers ; Function to find if a number is a perfect square ; Function that returns N if N is non - fibonacci number ; N is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both are perferct square ; Function to precompute sum of non - fibonacci Numbers ; Function to find the sum of all non - fibonacci numbers in a range ; Pre - computation ; Loop to find the sum for each query
var pref = Array ( 100010 ) . fill ( 0 ) ; function isPerfectSquare ( x ) { var s = parseInt ( Math . sqrt ( x ) ) ; return ( s * s == x ) ; } function isNonFibonacci ( n ) { if ( isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ) return 0 ; else return n ; } function compute ( ) { for ( var i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isNonFibonacci ( i ) ; } } function printSum ( L , R ) { var sum = pref [ R ] - pref [ L - 1 ] ; document . write ( sum + " " ) ; } compute ( ) ; var Q = 2 ; var arr = [ [ 1 , 5 ] , [ 6 , 10 ] ] ; for ( var i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; }
Count twin prime pairs in an Array | A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i and i + 2 then it is not prime ; A utility function that check if n1 and n2 are Twin Primes or not ; Function to find Twin Prime pairs from the given array ; Iterate through all pairs ; Increment count if twin prime pair ; Driver code ; Function call to find Twin Primes pair
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( var i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } function twinPrime ( n1 , n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . abs ( n1 - n2 ) == 2 ) ; } function countTwinPairs ( arr , n ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { if ( twinPrime ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } var arr = [ 2 , 3 , 5 , 11 ] ; var n = arr . length ; document . write ( countTwinPairs ( arr , n ) ) ;
Fill the missing numbers in the array of N natural numbers such that arr [ i ] not equal to i | Function to fill the position with arr [ i ] = 0 ; Inserting all elements in missing [ ] set from 1 to N ; Inserting unfilled positions ; Removing allocated_elements ; Loop for filling the positions with arr [ i ] != i ; Checking for any arr [ i ] = i ; Finding the suitable position in the array to swap with found i for which arr [ i ] = i ; Checking if the position is present in unfilled_position ; Swapping arr [ i ] & arr [ pos ] ( arr [ pos ] = pos ) ; Function to Print the array ; Driver Code
function solve ( arr , n ) { let unfilled_indices = new Set ( ) ; let missing = new Set ( ) ; for ( let i = 1 ; i < n ; i ++ ) { missing . add ( i ) ; } for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { unfilled_indices . add ( i ) ; } else { missing . delete ( arr [ i ] ) ; } } let mi = new Array ( missing . size ) ; let l = 0 ; for ( let x of missing . values ( ) ) { mi [ l ++ ] = x ; } let m = missing . size ; for ( let it of unfilled_indices . values ( ) ) { arr [ it ] = mi [ m - 1 ] ; m -- ; } let pos = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == i ) { pos = i ; } } let x ; if ( pos != 0 ) { for ( let i = 1 ; i < n ; i ++ ) { if ( pos != i ) { if ( unfilled_indices . has ( i ) ) { x = arr [ i ] ; arr [ i ] = pos ; arr [ pos ] = x ; break ; } } } } printArray ( arr , n ) ; } function printArray ( arr , n ) { for ( let i = 1 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let arr = [ 0 , 7 , 4 , 0 , 3 , 0 , 5 , 1 ] ; let n = arr . length ; solve ( arr , n ) ;
Find Kth number from sorted array formed by multiplying any two numbers in the array | Function to find number of pairs ; Negative and Negative ; Add Possible Pairs ; Positive and Positive ; Add Possible pairs ; Negative and Positive ; Add Possible pairs ; Function to find the kth element in the list ; Separate Positive and Negative elements ; Sort the Elements ; Binary search ; Return the required answer ; Driver code ; Function call
function check ( x , pos , neg , k ) { let pairs = 0 ; let p = neg . length - 1 ; let nn = neg . length - 1 ; let pp = pos . length - 1 ; for ( let i = 0 ; i < neg . length ; i ++ ) { while ( ( p >= 0 ) && ( neg [ i ] * neg [ p ] <= x ) ) p -- ; pairs += Math . min ( nn - p , nn - i ) ; } p = 0 ; for ( let i = pos . length - 1 ; i >= 0 ; i -- ) { while ( ( p < pos . length ) && ( pos [ i ] * pos [ p ] <= x ) ) p ++ ; pairs += Math . min ( p , i ) ; } p = pos . length - 1 ; for ( let i = neg . length - 1 ; i >= 0 ; i -- ) { while ( ( p >= 0 ) && ( neg [ i ] * pos [ p ] <= x ) ) p -- ; pairs += pp - p ; } return ( pairs >= k ) ; } function kth_element ( a , n , k ) { let pos = [ ] ; let neg = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) pos . push ( a [ i ] ) ; else neg . push ( a [ i ] ) ; } sort ( pos . begin ( ) , pos . end ( ) ) ; sort ( neg . begin ( ) , neg . end ( ) ) ; pos . sort ( function ( a , b ) { return a - b } ) ; neg . sort ( function ( a , b ) { return a - b } ) ; let l = - 1e8 , ans = 0 , r = 1e8 ; while ( l <= r ) { let mid = ( l + r ) >> 1 ; if ( check ( mid , pos , neg , k ) ) { ans = mid ; r = mid - 1 ; } else l = mid + 1 ; } return ans ; } let a = [ - 4 , - 2 , 3 , 3 ] ; let k = 3 ; let n = a . length ; document . write ( kth_element ( a , n , k ) ) ;
Find root of a number using Newton 's method | Function to return the square root of a number using Newtons method ; Assuming the sqrt of n as n only ; The closed guess will be stored in the root ; To count the number of iterations ; Calculate more closed x ; Check for closeness ; Update root ; Driver code
function squareRoot ( n , l ) { let x = n ; let root ; let count = 0 ; while ( true ) { count ++ ; root = 0.5 * ( x + ( n / x ) ) ; if ( Math . abs ( root - x ) < l ) break ; x = root ; } return root . toFixed ( 4 ) ; } let n = 327 ; let l = 0.00001 ; document . write ( squareRoot ( n , l ) ) ;
Program to find the time remaining for the day to complete | Function to find the remaining time ; Formula for total remaining minutes = 1440 - 60 h - m ; Remaining hours ; Remaining minutes ; Current time ; Get the remaining time
function remainingTime ( h , m ) { var totalMin , hoursRemaining , minRemaining ; totalMin = 1440 - 60 * h - m ; hoursRemaining = totalMin / 60 ; minRemaining = totalMin % 60 ; document . write ( Math . trunc ( hoursRemaining ) + " " + minRemaining ) ; } var h = 0 , m = 1 ; remainingTime ( h , m ) ;
Goldbach 's Weak Conjecture for Odd numbers | Function to check if a number can be represent as as a sum of 3 prime ; Driver code
function check ( n ) { if ( n % 2 == 1 && n > 5 ) { document . write ( " " ) ; } else { document . write ( " " + " " ) ; } } var a = 3 ; var b = 7 ; check ( a ) ; check ( b ) ;
Number of ways to reach ( X , Y ) in a matrix starting from the origin | To store the factorial and factorial mod inverse of the numbers ; Function to find ( a ^ m1 ) % mod ; Function to find the factorial of all the numbers ; Function to find the factorial modinverse of all the numbers ; Function to return nCr ; Function to return the number of ways to reach ( X , Y ) in a matrix with the given moves starting from the origin ; Driver code
let factorial = new Array ( 1000005 ) ; let modinverse = new Array ( 1000005 ) ; factorial . fill ( 0 ) ; modinverse . fill ( 0 ) ; let mod = 1000000007 ; let N = 1000005 ; function power ( a , m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( a * a ) % mod ; else if ( ( m1 & 1 ) != 0 ) return ( a * power ( power ( a , parseInt ( m1 / 2 , 10 ) ) , 2 ) ) % mod ; else return power ( power ( a , parseInt ( m1 / 2 , 10 ) ) , 2 ) % mod ; } function factorialfun ( ) { factorial [ 0 ] = 1 ; for ( let i = 1 ; i < N ; i ++ ) factorial [ i ] = ( factorial [ i - 1 ] * i ) % mod ; } function modinversefun ( ) { modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; for ( let i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } function binomial ( n , r ) { if ( r > n ) return 0 ; let a = ( factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( a * modinverse [ r ] ) % mod ; return a * 0 + 2 ; } function ways ( x , y ) { factorialfun ( ) ; modinversefun ( ) ; if ( ( 2 * x - y ) % 3 == 0 && ( 2 * y - x ) % 3 == 0 ) { let m = parseInt ( ( 2 * x - y ) / 3 , 10 ) ; let n = parseInt ( ( 2 * y - x ) / 3 , 10 ) ; System . out . println ( n + m + " " + n ) ; return binomial ( ( n + m ) , n ) ; } return 0 ; } let x = 3 , y = 3 ; document . write ( ways ( x , y ) ) ;
Find Nth even length palindromic number formed using digits X and Y | Utility function to compute n 'th palindrome number ; Calculate the length from above formula as discussed above ; Calculate rank for length L ; Mask to check if i 't bit  is set or not ; If bit is set append '5' else append '4' ; Driver Code
function solve ( n , x , y ) { var length = Math . ceil ( Math . log2 ( n + 2 ) ) - 1 ; var rank = n - ( 1 << length ) + 1 ; var left = " " , right = " " ; for ( var i = length - 1 ; i >= 0 ; i -- ) { var mask = 1 << i ; var bit = mask & rank ; if ( bit ) { left += y ; right += y ; } else { left += x ; right += x ; } } right = right . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; return left + right ; } var n = 23 ; var x = ' ' , y = ' ' ; var ans = solve ( n , x , y ) ; document . write ( ans + " " ) ;
Maximum of all the integers in the given level of Pascal triangle | Function for the binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the maximum value in the nth level of the Pascal 's triangle ; Driver code
function binomialCoeff ( n , k ) { var C = new Array ( n + 1 ) ; for ( var i = 0 ; i < C . length ; i ++ ) { C [ i ] = new Array ( k + 1 ) ; } var i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } function findMax ( n ) { return binomialCoeff ( n , Math . floor ( n / 2 ) ) ; } var n = 5 ; document . write ( binomialCoeff ( n , Math . floor ( n / 2 ) ) ) ;
Length of the smallest sub | Javascript program to find the length of the smallest substring consisting of maximum distinct characters ; Find maximum distinct characters in any string ; Initialize all character 's  count with 0 ; Increase the count in array if a character is found ; Size of given string ; Find maximum distinct characters in any string ; Result ; Brute force approach to find all substrings ; We have to check here both conditions together 1. substring ' s ▁ distinct ▁ characters ▁ is ▁ equal ▁ ▁ to ▁ maximum ▁ distinct ▁ characters ▁ ▁ 2 . ▁ substring ' s length should be minimum ; Input String
let NO_OF_CHARS = 256 ; function max_distinct_char ( str , n ) { let count = new Array ( NO_OF_CHARS ) ; for ( let i = 0 ; i < count . length ; i ++ ) { count [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { count [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } let max_distinct = 0 ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] != 0 ) { max_distinct ++ ; } } return max_distinct ; } function smallesteSubstr_maxDistictChar ( str ) { let n = str . length ; let max_distinct = max_distinct_char ( str , n ) ; let minl = n ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { let subs = null ; if ( i < j ) subs = str . substring ( i , j ) ; else subs = str . substring ( j , i ) ; let subs_lenght = subs . length ; let sub_distinct_char = max_distinct_char ( subs , subs_lenght ) ; if ( subs_lenght < minl && max_distinct == sub_distinct_char ) { minl = subs_lenght ; } } } return minl ; } let str = " " ; let len = smallesteSubstr_maxDistictChar ( str ) ; document . write ( " " + " " + " " + len ) ;
Find two co |  ; Function to find the required numbers ; GCD of the given numbers ; Printing the required numbers ; Driver Code
function findGCD ( a , b ) { if ( b == 0 ) return a ; else return findGCD ( b , a % b ) ; } function findNumbers ( a , b ) { var gcd = findGCD ( a , b ) ; document . write ( ( a / gcd ) + " " + ( b / gcd ) ) ; } var a = 12 , b = 16 ; findNumbers ( a , b ) ;
Find the remaining balance after the transaction | Function to find the balance ; Check if the transaction can be successful or not ; Transaction is successful ; Transaction is unsuccessful ; Driver Code
function findBalance ( x , bal ) { if ( x % 10 == 0 && ( x + 1.50 ) <= bal ) { document . write ( ( bal - x - 1.50 ) . toFixed ( 2 ) ) ; } else { document . write ( ( bal ) . toFixed ( 2 ) ) ; } } var x = 50 ; var bal = 100.50 ; findBalance ( x , bal ) ;
Count number of pairs in array having sum divisible by K | SET 2 | Program to count pairs whose sum divisible by ' K ' ; Create a frequency array to count occurrences of all remainders when divided by K ; To store count of pairs . ; Traverse the array , compute the remainder and add k - remainder value hash count to ans ; Count number of ( A [ i ] , ( K - rem ) % K ) pairs ; Increment count of remainder in hash map ; Driver code
function countKdivPairs ( A , n , K ) { var freq = Array ( K ) . fill ( 0 ) ; var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var rem = A [ i ] % K ; ans += freq [ ( K - rem ) % K ] ; freq [ rem ] ++ ; } return ans ; } var A = [ 2 , 2 , 1 , 7 , 5 , 3 ] ; var n = A . length ; var K = 4 ; document . write ( countKdivPairs ( A , n , K ) ) ;
Minimum number of Bottles visible when a bottle can be enclosed inside another Bottle | Javascript code for the above approach ; Driver code ; Find the solution
function min_visible_bottles ( arr , n ) { let m = new Map ( ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( m . has ( arr [ i ] ) ) { m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) } else { m . set ( arr [ i ] , 1 ) } ans = Math . max ( ans , m . get ( arr [ i ] ) ) ; } document . write ( " " + " " + ans + " " ) ; } let n = 8 ; let arr = [ 1 , 1 , 2 , 3 , 4 , 5 , 5 , 4 ] ; min_visible_bottles ( arr , n ) ;
Remove Nth node from end of the Linked List | javascript program to delete nth node from last Structure of node ; Function to insert node in a linked list ; Function to remove nth node from last ; To store length of the linked list ; B > length , then we can 't remove node ; We need to remove head node ; Return head . next ; Remove len - B th node from starting ; This function prints contents of linked list starting from the given node ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function create ( head , x ) { var temp , ptr = head ; temp = new Node ( ) ; temp . data = x ; temp . next = null ; if ( head == null ) head = temp ; else { while ( ptr . next != null ) { ptr = ptr . next ; } ptr . next = temp ; } return head ; } function removeNthFromEnd ( head , B ) { var len = 0 ; var tmp = head ; while ( tmp != null ) { len ++ ; tmp = tmp . next ; } if ( B > len ) { document . write ( " " + len ) ; document . write ( " " + B + " " ) ; document . write ( " " ) ; return head ; } else if ( B == len ) { return head . next ; } else { var diff = len - B ; var prev = null ; var curr = head ; for ( i = 0 ; i < diff ; i ++ ) { prev = curr ; curr = curr . next ; } prev . next = curr . next ; return head ; } } function display ( head ) { var temp = head ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( " " ) ; } var head = null ; head = create ( head , 1 ) ; head = create ( head , 2 ) ; head = create ( head , 3 ) ; head = create ( head , 4 ) ; head = create ( head , 5 ) ; var n = 2 ; document . write ( " " ) ; display ( head ) ; head = removeNthFromEnd ( head , 2 ) ; document . write ( " " ) ; display ( head ) ;