codeparrot/xlcost-text-to-code huggingface dataset:
Count of strings in the first array which are smaller than every string in the second array | JavaScript implementation of the approach ; Function to count the number of smaller strings in A [ ] for every String in B [ ] ; Count the frequency of all characters ; Iterate for all possible strings in A [ ] ; Increase the frequency of every character ; Check for the smallest character 's frequency ; Get the smallest character frequency ; Insert it in the vector ; Sort the count of all the frequency of the smallest character in every string ; Iterate for every String in B [ ] ; Hash set every frequency 0 ; Count the frequency of every character ; Find the frequency of the smallest character ; Count the number of strings in A [ ] which has the frequency of the smaller character less than the frequency of the smaller character of the String in B [ ] ; Store the answer ; Function to print the answer ; Get the answer ; Print the number of strings for every answer ; Driver code
const MAX = 26 ; function findCount ( a , b , n , m ) { var freq = new Array ( MAX ) . fill ( 0 ) ; var smallestFreq = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { var s = a [ i ] ; for ( var l = 0 ; l < freq . length ; l ++ ) freq [ l ] = 0 ; for ( var j = 0 ; j < s . length ; j ++ ) { freq [ s [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } for ( var j = 0 ; j < MAX ; j ++ ) { if ( freq [ j ] > 0 ) { smallestFreq . push ( freq [ j ] ) ; break ; } } } smallestFreq . sort ( ) ; var ans = [ ] ; for ( var i = 0 ; i < m ; i ++ ) { var s = b [ i ] ; for ( var l = 0 ; l < freq . length ; l ++ ) freq [ l ] = 0 ; for ( var j = 0 ; j < s . length ; j ++ ) { freq [ s [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } var frequency = 0 ; for ( var j = 0 ; j < MAX ; j ++ ) { if ( freq [ j ] > 0 ) { frequency = freq [ j ] ; break ; } } var array = new Array ( smallestFreq . length ) . fill ( 0 ) ; var k = 0 ; for ( const val of smallestFreq ) { array [ k ] = val ; k ++ ; } var ind = lower_bound ( array , 0 , smallestFreq . length , frequency ) ; ans . push ( ind ) ; } return ans ; } function lower_bound ( a , low , high , element ) { while ( low < high ) { var middle = low + parseInt ( ( high - low ) / 2 ) ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } function printAnswer ( a , b , n , m ) { var ans = findCount ( a , b , n , m ) ; for ( const it of ans ) { document . write ( it + " " ) ; } } var A = [ " " , " " , " " ] ; var B = [ " " , " " ] ; var n = A . length ; var m = B . length ; printAnswer ( A , B , n , m ) ;
Find Leftmost and Rightmost node of BST from its given preorder traversal | Function to return the leftmost and rightmost nodes of the BST whose preorder traversal is given ; Variables for finding minimum and maximum values of the array ; Update the minimum ; Update the maximum ; Print the values ; Driver Code
function LeftRightNode ( preorder , n ) { var min = 1000000000 , max = - 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { if ( min > preorder [ i ] ) min = preorder [ i ] ; if ( max < preorder [ i ] ) max = preorder [ i ] ; } document . write ( " " + min + " " ) ; document . write ( " " + max ) ; } var preorder = [ 3 , 2 , 1 , 5 , 4 ] ; var n = 5 ; LeftRightNode ( preorder , n ) ;
Find the position of the given row in a 2 | JavaScript implementation of the approach ; Function that compares both the arrays and returns - 1 , 0 and 1 accordingly ; Return 1 if mid row is less than arr [ ] ; Return 1 if mid row is greater than arr [ ] ; Both the arrays are equal ; Function to find a row in the given matrix using binary search ; If current row is equal to the given array then return the row number ; If arr [ ] is greater , ignore left half ; If arr [ ] is smaller , ignore right half ; No valid row found ; Driver code
var m = 6 , n = 4 ; function compareRow ( a1 , a2 ) { for ( var i = 0 ; i < n ; i ++ ) { if ( a1 [ i ] < a2 [ i ] ) return 1 ; else if ( a1 [ i ] > a2 [ i ] ) return - 1 ; } return 0 ; } function binaryCheck ( ar , arr ) { var l = 0 , r = m - 1 ; while ( l <= r ) { var mid = parseInt ( ( l + r ) / 2 ) ; var temp = compareRow ( ar [ mid ] , arr ) ; if ( temp == 0 ) return mid + 1 ; else if ( temp == 1 ) l = mid + 1 ; else r = mid - 1 ; } return - 1 ; } var mat = [ [ 0 , 0 , 1 , 0 ] , [ 10 , 9 , 22 , 23 ] , [ 40 , 40 , 40 , 40 ] , [ 43 , 44 , 55 , 68 ] , [ 81 , 73 , 100 , 132 ] , [ 100 , 75 , 125 , 133 ] ] ; var row = [ 10 , 9 , 22 , 23 ] ; document . write ( binaryCheck ( mat , row ) ) ;
Number of subarrays having absolute sum greater than K | Set | Javascript implementation of the above approach ; Function to find required value ; Variable to store final answer ; Loop to find prefix - sum ; Sorting prefix - sum array ; Loop to find upper_bound for each element ; Returning final answer ; Driver code ; Function to find required value
var maxLen = 30 ; function upper_bound ( a , low , high , element ) { while ( low < high ) { var middle = low + parseInt ( ( high - low ) / 2 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } function findCnt ( arr , n , k ) { var ans = 0 ; for ( var i = 1 ; i < n ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; if ( arr [ i ] > k arr [ i ] < - 1 * k ) ans ++ ; } if ( arr [ 0 ] > k arr [ 0 ] < - 1 * k ) ans ++ ; arr . sort ( ( a , b ) => a - b ) for ( var i = 0 ; i < n ; i ++ ) ans += ( n - upper_bound ( arr , 0 , n , arr [ i ] + k ) ) ; return ans ; } var arr = [ - 1 , 4 , - 5 , 6 ] ; var n = arr . length ; var k = 0 ; document . write ( findCnt ( arr , n , k ) ) ;
Number of intersections between two ranges | Javascript implementation of above approach ; Function to return total number of intersections ; Maximum possible number of intersections ; Store all starting points of type1 ranges ; Store all endpoints of type1 ranges ; Starting point of type2 ranges ; Ending point of type2 ranges ; Subtract those ranges which are starting after R ; Subtract those ranges which are ending before L ; Driver Code
function upperBound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( a [ middle ] > element ) { high = middle ; } else { low = middle + 1 ; } } return low ; } function FindIntersection ( type1 , n , type2 , m ) { let ans = n * m ; let start = [ ] ; let end = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { start . push ( type1 [ i ] [ 0 ] ) ; end . push ( type1 [ i ] [ 1 ] ) ; } start . sort ( function ( a , b ) { return a - b ; } ) ; end . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < m ; i ++ ) { let L = type2 [ i ] [ 0 ] ; let R = type2 [ i ] [ 1 ] ; ans -= start . length - upperBound ( start , 0 , start . length , R ) ; ans -= upperBound ( end , 0 , end . length , L - 1 ) ; } return ans ; } let type1 = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 4 , 5 ] , [ 6 , 7 ] ] ; let type2 = [ [ 1 , 5 ] , [ 2 , 3 ] , [ 4 , 7 ] , [ 5 , 7 ] ] ; let n = type1 . length ; let m = type2 . length ; document . write ( FindIntersection ( type1 , n , type2 , m ) ) ;
Find the Nth term divisible by a or b or c | Function to return gcd of a and b ; Function to return the lcm of a and b ; Function to return the count of numbers from 1 to num which are divisible by a , b or c ; Calculate number of terms divisible by a and by b and by c then , remove the terms which is are divisible by both a and b , both b and c , both c and a and then add which are divisible by a and b and c ; Function to find the nth term divisible by a , b or c by using binary search ; Set low to 1 and high to max ( a , b , c ) * n ; If the current term is less than n then we need to increase low to mid + 1 ; If current term is greater than equal to n then high = mid ; Driver code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function lcm ( a , b ) { return parseInt ( ( a * b ) / gcd ( a , b ) ) ; } function divTermCount ( a , b , c , num ) { return ( parseInt ( num / a ) + parseInt ( num / b ) + parseInt ( num / c ) - parseInt ( num / lcm ( a , b ) ) - parseInt ( num / lcm ( b , c ) ) - parseInt ( num / lcm ( a , c ) ) + parseInt ( num / lcm ( a , lcm ( b , c ) ) ) ) ; } function findNthTerm ( a , b , c , n ) { let low = 1 , high = Number . MAX_VALUE , mid ; while ( low < high ) { mid = low + parseInt ( ( high - low ) / 2 ) ; if ( divTermCount ( a , b , c , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } let a = 2 , b = 3 , c = 5 , n = 10 ; document . write ( findNthTerm ( a , b , c , n ) ) ;
Split the given array into K sub | Function to check if mid can be maximum sub - arrays sum ; If individual element is greater maximum possible sum ; Increase sum of current sub - array ; If the sum is greater than mid increase count ; Check condition ; Function to find maximum subarray sum which is minimum ; var start = max ; Max subarray sum , considering subarray of length 1 ; end += array [ i ] ; Max subarray sum , considering subarray of length n ; Answer stores possible maximum sub array sum ; If mid is possible solution Put answer = mid ; ; Driver Code
function check ( mid , array , n , K ) { var count = 0 ; var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( array [ i ] > mid ) return false ; sum += array [ i ] ; if ( sum > mid ) { count ++ ; sum = array [ i ] ; } } count ++ ; if ( count <= K ) return true ; return false ; } function solve ( array , n , K ) { var max = array . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var end = 0 ; for ( var i = 0 ; i < n ; i ++ ) { } var answer = 0 ; while ( start <= end ) { var mid = parseInt ( ( start + end ) / 2 ) ; if ( check ( mid , array , n , K ) ) { answer = mid ; end = mid - 1 ; } else { start = mid + 1 ; } } return answer ; } var array = [ 1 , 2 , 3 , 4 ] ; var n = array . length ; var K = 3 ; document . write ( solve ( array , n , K ) ) ;
Create a Sorted Array Using Binary Search | Function to create a new sorted array using Binary Search ; Auxiliary Array ; if b is empty any element can be at first place ; Perform Binary Search to find the correct position of current element in the new array ; let the element should be at first index ; if a [ j ] is already present in the new array ; add a [ j ] at mid + 1. you can add it at mid ; if a [ j ] is lesser than b [ mid ] go right side ; means pos should be between start and mid - 1 ; else pos should be between mid + 1 and end ; if a [ j ] is the largest push it at last ; here Max ( 0 , pos ) is used because sometimes pos can be negative as smallest duplicates can be present in the array ; Print the new generated sorted array ; Driver Code
function createSorted ( a , n ) { var b = [ ] ; for ( var j = 0 ; j < n ; j ++ ) { if ( b . length == 0 ) b . push ( a [ j ] ) ; else { var start = 0 , end = b . length - 1 ; var pos = 0 ; while ( start <= end ) { var mid = start + parseInt ( ( end - start ) / 2 ) ; if ( b [ mid ] === a [ j ] ) { b . insert ( Math . max ( 0 , mid + 1 ) , a [ j ] ) ; break ; } else if ( b [ mid ] > a [ j ] ) pos = end = mid - 1 ; else pos = start = mid + 1 ; if ( start > end ) { pos = start ; b . insert ( Math . max ( 0 , pos ) , a [ j ] ) ; break ; } } } } for ( var i = 0 ; i < n ; i ++ ) document . write ( b [ i ] + " " ) ; } Array . prototype . insert = function ( index , item ) { this . splice ( index , 0 , item ) ; } ; var a = [ 2 , 5 , 4 , 9 , 8 ] ; var n = a . length ; createSorted ( a , n ) ;
Largest sub | Javascript implementation of the approach ; Function that return true if frequency of all the present characters is at least k ; If the character is present and its frequency is less than k ; Function to set every frequency to zero ; Function to return the length of the longest sub - string such that it contains every character at least k times ; To store the required maximum length ; Starting index of the sub - string ; Ending index of the sub - string ; If the frequency of every character of the current sub - string is at least k ; Update the maximum possible length ; Driver code
var MAX = 26 ; function atLeastK ( freq , k ) { for ( var i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] != 0 && freq [ i ] < k ) return false ; } return true ; } function setZero ( freq ) { for ( var i = 0 ; i < MAX ; i ++ ) freq [ i ] = 0 ; } function findlength ( str , n , k ) { var maxLen = 0 ; var freq = Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { setZero ( freq ) ; for ( var j = i ; j < n ; j ++ ) { freq [ str [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; if ( atLeastK ( freq , k ) ) { maxLen = Math . max ( maxLen , j - i + 1 ) ; } } } return maxLen ; } var str = " " ; var n = str . length ; var k = 2 ; document . write ( findlength ( str , n , k ) ) ;
Minimum increments to convert to an array of consecutive integers | Function that return true if the required array can be generated with m as the last element ; Build the desired array ; Check if the given array can be converted to the desired array with the given operation ; Function to return the minimum number of operations required to convert the given array to an increasing AP series with common difference as 1 ; Apply Binary Search ; If array can be generated with mid as the last element ; Current ans is mid ; Check whether the same can be achieved with even less operations ; Build the desired array ; Calculate the number of operations required ; Return the number of operations required ; Driver code
function check ( m , n , arr ) { var desired = Array ( n ) ; for ( var i = n - 1 ; i >= 0 ; i -- ) { desired [ i ] = m ; m -- ; } for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > desired [ i ] desired [ i ] < 1 ) { return false ; } } return true ; } function minOperations ( arr , n ) { var start = arr [ n - 1 ] ; var end = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) + n ; var max_arr = 0 ; while ( start <= end ) { var mid = parseInt ( ( start + end ) / 2 ) ; if ( check ( mid , n , arr ) ) { max_arr = mid ; end = mid - 1 ; } else { start = mid + 1 ; } } var desired = Array ( n ) ; for ( var i = n - 1 ; i >= 0 ; i -- ) { desired [ i ] = max_arr ; max_arr -- ; } var operations = 0 ; for ( var i = 0 ; i < n ; i ++ ) { operations += ( desired [ i ] - arr [ i ] ) ; } return operations ; } var arr = [ 4 , 4 , 5 , 5 , 7 ] ; var n = arr . length ; document . write ( minOperations ( arr , n ) ) ;
Count duplicates in a given linked list | Representation of node ; Function to insert a node at the beginning ; Function to count the number of duplicate nodes in the linked list ; Create a hash table insert head ; Traverse through remaining nodes ; Return the count of duplicate nodes ; Driver code
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } ; function insert ( head , item ) { var temp = new Node ( ) ; temp . data = item ; temp . next = head ; head = temp ; return head ; } function countNode ( head ) { if ( head == null ) return 0 ; ; var s = new Set ( ) ; s . add ( head . data ) ; var count = 0 ; for ( var curr = head . next ; curr != null ; curr = curr . next ) { if ( s . has ( curr . data ) ) count ++ ; s . add ( curr . data ) ; } return count ; } var head = null ; head = insert ( head , 5 ) ; head = insert ( head , 7 ) ; head = insert ( head , 5 ) ; head = insert ( head , 1 ) ; head = insert ( head , 7 ) ; document . write ( countNode ( head ) ) ;
Minimize the number of steps required to reach the end of the array | Set 2 | Function to return the minimum steps required to reach the end of the given array ; Array to determine whether a cell has been visited before ; Queue for bfs ; Push the source i . e . index 0 ; Variable to store the depth of search ; BFS algorithm ; Current queue size ; Top - most element of queue ; Base case ; If we reach the destination i . e . index ( n - 1 ) ; Marking the cell visited ; Pushing the adjacent nodes i . e . indices reachable from the current index ; Driver code
function minSteps ( arr , n ) { var v = Array ( n ) . fill ( 0 ) ; var q = [ ] ; q . push ( 0 ) ; var depth = 0 ; while ( q . length != 0 ) { var x = q . length ; while ( x -- ) { var i = q [ 0 ] ; q . shift ( ) ; if ( v [ i ] ) continue ; if ( i == n - 1 ) return depth ; v [ i ] = 1 ; if ( i + arr [ i ] < n ) q . push ( i + arr [ i ] ) ; if ( i - arr [ i ] >= 0 ) q . push ( i - arr [ i ] ) ; } depth ++ ; } return - 1 ; } var arr = [ 1 , 1 , 1 , 1 , 1 , 1 ] ; var n = arr . length ; document . write ( minSteps ( arr , n ) ) ;
Find the winner of the game based on greater number of divisors | Function to return the count of divisors of elem ; Function to return the winner of the game ; Convert every element of A [ ] to their divisor count ; Convert every element of B [ ] to their divisor count ; Sort both the arrays ; For every element of A apply Binary Search to find number of pairs where A wins ; B wins if A doesnot win ; Driver code
function divisorcount ( elem ) { let ans = 0 ; for ( let i = 1 ; i <= Math . sqrt ( elem ) ; i ++ ) { if ( elem % i == 0 ) { if ( i * i == elem ) ans ++ ; else ans += 2 ; } } return ans ; } function findwinner ( A , B , N , M ) { for ( let i = 0 ; i < N ; i ++ ) { A [ i ] = divisorcount ( A [ i ] ) ; } for ( let i = 0 ; i < M ; i ++ ) { B [ i ] = divisorcount ( B [ i ] ) ; } A . sort ( function ( a , b ) { return a - b ; } ) ; B . sort ( function ( a , b ) { return a - b ; } ) ; let winA = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let val = A [ i ] ; let start = 0 ; let end = M - 1 ; let index = - 1 ; while ( start <= end ) { let mid = Math . floor ( ( start + end ) / 2 ) ; if ( B [ mid ] <= val ) { index = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } winA += ( index + 1 ) ; } let winB = N * M - winA ; if ( winA > winB ) { return " " ; } else if ( winB > winA ) { return " " ; } return " " ; } let A = [ 4 , 12 , 24 ] ; let N = A . length ; let B = [ 25 , 28 , 13 , 45 ] ; let M = B . length ; document . write ( findwinner ( A , B , N , M ) ) ;
Minimum time required to transport all the boxes from source to the destination under the given constraints | Function that returns true if it is possible to transport all the boxes in the given amount of time ; If all the boxes can be transported in the given time ; If all the boxes can 't be  transported in the given time ; Function to return the minimum time required ; Sort the two arrays ; Stores minimum time in which all the boxes can be transported ; Check for the minimum time in which all the boxes can be transported ; If it is possible to transport all the boxes in mid amount of time ; Driver code
function isPossible ( box , truck , n , m , min_time ) { let temp = 0 ; let count = 0 ; while ( count < m ) { for ( let j = 0 ; j < min_time && temp < n && truck [ count ] >= box [ temp ] ; j += 2 ) temp ++ ; count ++ ; } if ( temp == n ) return true ; return false ; } function minTime ( box , truck , n , m ) { box . sort ( function ( a , b ) { return a - b } ) ; truck . sort ( function ( a , b ) { return a - b } ) ; let l = 0 ; let h = 2 * n ; let min_time = 0 ; while ( l <= h ) { let mid = Math . floor ( ( l + h ) / 2 ) ; if ( isPossible ( box , truck , n , m , mid ) ) { min_time = mid ; h = mid - 1 ; } else l = mid + 1 ; } return min_time ; } let box = [ 10 , 2 , 16 , 19 ] ; let truck = [ 29 , 25 ] ; let n = box . length ; let m = truck . length ; document . write ( minTime ( box , truck , n , m ) ) ;
Maximum points covered after removing an Interval | Function To find the required interval ; Total Count of covered numbers ; Array to store numbers that occur exactly in one interval till ith interval ; Calculate New count by removing all numbers in range [ l , r ] occurring exactly once ; Driver code
function solve ( interval , N , Q ) { var Mark = Array ( Q ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { var l = interval [ i ] [ 0 ] - 1 ; var r = interval [ i ] [ 1 ] - 1 ; for ( var j = l ; j <= r ; j ++ ) Mark [ j ] ++ ; } var count = 0 ; for ( var i = 0 ; i < Q ; i ++ ) { if ( Mark [ i ] ) count ++ ; } var count1 = Array ( Q ) . fill ( 0 ) ; if ( Mark [ 0 ] == 1 ) count1 [ 0 ] = 1 ; for ( var i = 1 ; i < Q ; i ++ ) { if ( Mark [ i ] == 1 ) count1 [ i ] = count1 [ i - 1 ] + 1 ; else count1 [ i ] = count1 [ i - 1 ] ; } var maxindex ; var maxcoverage = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var l = interval [ i ] [ 0 ] - 1 ; var r = interval [ i ] [ 1 ] - 1 ; var elem1 ; if ( l != 0 ) elem1 = count1 [ r ] - count1 [ l - 1 ] ; else elem1 = count1 [ r ] ; if ( count - elem1 >= maxcoverage ) { maxcoverage = count - elem1 ; maxindex = i ; } } document . write ( " " + maxcoverage + " " + maxindex ) ; } var interval = [ [ 1 , 4 ] , [ 4 , 5 ] , [ 5 , 6 ] , [ 6 , 7 ] , [ 3 , 5 ] ] ; var N = interval . length ; var Q = 7 ; solve ( interval , N , Q ) ;
Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps | Function that returns true if it is possible to reach end of the array in exactly k jumps ; Variable to store the number of steps required to reach the end ; If it is possible to reach the end in exactly k jumps ; Returns the minimum maximum distance required to reach the end of the array in exactly k jumps ; Stores the answer ; Binary search to calculate the result ; Driver Code
function isPossible ( arr , n , dist , k ) { let req = 0 ; let curr = 0 ; let prev = 0 ; for ( let i = 0 ; i < n ; i ++ ) { while ( curr != n && arr [ curr ] - arr [ prev ] <= dist ) { curr ++ ; } req ++ ; if ( curr == n ) { break ; } prev = curr - 1 ; } if ( curr != n ) { return false ; } if ( req <= k ) { return true ; } return false ; } function minDistance ( arr , n , k ) { let l = 0 ; let h = arr [ n - 1 ] ; let ans = 0 ; while ( l <= h ) { let m = Math . floor ( ( l + h ) / 2 ) ; if ( isPossible ( arr , n , m , k ) ) { ans = m ; h = m - 1 ; } else { l = m + 1 ; } } return ans ; } let arr = [ 2 , 15 , 36 , 43 ] ; let n = arr . length ; let k = 2 ; document . write ( minDistance ( arr , n , k ) ) ;
Find the kth element in the series generated by the given N ranges | Function to return the kth element of the required series ; To store the number of integers that lie upto the ith index ; Compute the number of integers ; Stores the index , lying from 1 to n , ; Using binary search , find the index in which the kth element will lie ; Find the position of the kth element in the interval in which it lies ; Driver code
function getKthElement ( n , k , L , R ) { let l = 1 ; let h = n ; let total = new Array ( n + 1 ) ; total [ 0 ] = 0 ; for ( let i = 0 ; i < n ; i ++ ) { total [ i + 1 ] = total [ i ] + ( R [ i ] - L [ i ] ) + 1 ; } let index = - 1 ; while ( l <= h ) { let m = Math . floor ( ( l + h ) / 2 ) ; if ( total [ m ] > k ) { index = m ; h = m - 1 ; } else if ( total [ m ] < k ) l = m + 1 ; else { index = m ; break ; } } l = L [ index - 1 ] ; h = R [ index - 1 ] ; let x = k - total [ index - 1 ] ; while ( l <= h ) { let m = Math . floor ( ( l + h ) / 2 ) ; if ( ( m - L [ index - 1 ] ) + 1 == x ) { return m ; } else if ( ( m - L [ index - 1 ] ) + 1 > x ) h = m - 1 ; else l = m + 1 ; } return k ; } let L = [ 1 , 8 , 21 ] ; let R = [ 4 , 10 , 23 ] ; let n = L . length ; let k = 6 ; document . write ( getKthElement ( n , k , L , R ) ) ;
Find minimum positive integer x such that a ( x ^ 2 ) + b ( x ) + c >= k | Function to return the minimum positive integer satisfying the given equation ; Binary search to find the value of x ; Return the answer ; Driver code
function MinimumX ( a , b , c , k ) { let x = Number . MAX_VALUE ; if ( k <= c ) return 0 ; let h = k - c ; let l = 0 ; while ( l <= h ) { let m = Math . floor ( ( l + h ) / 2 ) ; if ( ( a * m * m ) + ( b * m ) > ( k - c ) ) { x = Math . min ( x , m ) ; h = m - 1 ; } else if ( ( a * m * m ) + ( b * m ) < ( k - c ) ) l = m + 1 ; else return m ; } return x ; } let a = 3 , b = 2 , c = 4 , k = 15 ; document . write ( MinimumX ( a , b , c , k ) ) ;