codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Digits of the Good Strings | Javascript implementation of the approach ; To store the states of the dp ; Function to fill the dp table ; Sum of digits of the string of length 1 is i as i is only number in that string and count of good strings of length 1 that end with i is also 1 ; Adjacent digits are different ; Increment the count as digit at ( i - 1 ) 'th index is k and count  of good strings is equal to this  because at the end of the strings of  length (i - 1) we are just  putting digit j as the last digit ; Driver code
var DIGITS = 10 var MAX = 10000 var MOD = 1000000007 var dp = Array . from ( Array ( MAX ) , ( ) => Array ( DIGITS ) . fill ( 0 ) ) ; var cnt = Array . from ( Array ( MAX ) , ( ) => Array ( DIGITS ) . fill ( 0 ) ) ; function precompute ( ) { for ( var i = 0 ; i < DIGITS ; i ++ ) dp [ 1 ] [ i ] = i , cnt [ 1 ] [ i ] = 1 ; for ( var i = 2 ; i < MAX ; i ++ ) { for ( var j = 0 ; j < DIGITS ; j ++ ) { for ( var k = 0 ; k < DIGITS ; k ++ ) { if ( j != k ) { dp [ i ] [ j ] = dp [ i ] [ j ] + ( dp [ i - 1 ] [ k ] + ( cnt [ i - 1 ] [ k ] * j ) % MOD ) % MOD ; dp [ i ] [ j ] %= MOD ; cnt [ i ] [ j ] += cnt [ i - 1 ] [ k ] ; cnt [ i ] [ j ] %= MOD ; } } } } } var x = 6 , y = 4 ; precompute ( ) ; document . write ( dp [ x ] [ y ] ) ;
Count non | Javascript implementation of the approach ; Function to pre - compute the sequence ; For N = 1 the answer will be 2 ; Starting two terms of the sequence ; Compute the rest of the sequence with the relation F [ i ] = F [ i - 1 ] + F [ i - 2 ] ; Driver code ; Pre - compute the sequence
var N = 10000 ; var MOD = 1000000007 ; var F = Array ( N ) ; function precompute ( ) { F [ 1 ] = 2 ; F [ 2 ] = 3 ; F [ 3 ] = 4 ; for ( var i = 4 ; i < N ; i ++ ) F [ i ] = ( F [ i - 1 ] + F [ i - 2 ] ) % MOD ; } var n = 8 ; precompute ( ) ; document . write ( F [ n ] ) ;
Minimize sum by dividing all elements of a subarray by K | Function to return the maximum subarray sum ; Function to return the minimized sum of the array elements after performing the given operation ; Find maximum subarray sum ; Find total sum of the array ; Maximum subarray sum is already negative ; Choose the subarray whose sum is maximum and divide all elements by K ; Driver code
function maxSubArraySum ( a , size ) { var max_so_far = - 1000000000 , max_ending_here = 0 ; for ( var i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } function minimizedSum ( a , n , K ) { var sum = maxSubArraySum ( a , n ) ; var totalSum = 0 ; for ( var i = 0 ; i < n ; i ++ ) totalSum += a [ i ] ; if ( sum < 0 ) return totalSum ; totalSum = totalSum - sum + sum / K ; return totalSum ; } var a = [ 1 , - 2 , 3 ] ; var n = a . length ; var K = 2 ; document . write ( minimizedSum ( a , n , K ) ) ;
Minimum numbers with one 's place as 9 to be added to get N | Function to find minimum count of numbers ( with one 's digit 9) that sum up to N ; Fetch one 's digit ; Apply Cases mentioned in approach ; If no possible answer exists ; Driver code
function findMin ( N ) { let digit = N % 10 ; switch ( digit ) { case 0 : if ( N >= 90 ) return 10 ; break ; case 1 : if ( N >= 81 ) return 9 ; break ; case 2 : if ( N >= 72 ) return 8 ; break ; case 3 : if ( N >= 63 ) return 7 ; break ; case 4 : if ( N >= 54 ) return 6 ; break ; case 5 : if ( N >= 45 ) return 5 ; break ; case 6 : if ( N >= 36 ) return 4 ; break ; case 7 : if ( N >= 27 ) return 3 ; break ; case 8 : if ( N >= 18 ) return 2 ; break ; case 9 : if ( N >= 9 ) return 1 ; break ; } return - 1 ; } let N = 27 ; document . write ( findMin ( N ) ) ;
Random list of M non | Utility function to print the elements of an array ; Function to generate a list of m random non - negative integers whose sum is n ; Create an array of size m where every element is initialized to 0 ; To make the sum of the final list as n ; Increment any random element from the array by 1 ; Print the generated list ; Driver code
function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function randomList ( m , n ) { let arr = new Array ( m ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { arr [ ( Math . floor ( ( Math . random ( ) * n ) ) % m ) ] ++ ; } printArr ( arr , m ) ; } let m = 4 , n = 8 ; randomList ( m , n ) ;
Maximum String Partition | Return the count of string ; P will store the answer ; Current will store current string Previous will store the previous string that has been taken already ; Add a character to current string ; Here we will create a partition and update the previous string with current string ; Now we will clear the current string ; Increment the count of partition . ; Driver code
function maxPartition ( s ) { var n = s . length , P = 0 ; var current = " " , previous = " " ; for ( var i = 0 ; i < n ; i ++ ) { current += s [ i ] ; if ( current != previous ) { previous = current ; current = " " ; P ++ ; } } return P ; } var s = " " ; var ans = maxPartition ( s ) ; document . write ( ans ) ;
How to learn Pattern printing easily ? |
var N = 4 , i , j , min ; document . write ( " " + N + " " ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; document . write ( N - min + 1 ) ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; document . write ( N - min + 1 ) ; } document . write ( " " ) ; }
Find the smallest positive number missing from an unsorted array | Set 3 | Function to find the smallest positive missing number ; Default smallest Positive integer ; Store values in set which are greater than variable m ; Store value when m is less than current index of given array ; Increment m when it is equal to current element ; Increment m when it is one of the element of the set ; Return the required answer ; Driver code ; Function call
function findMissingPositive ( arr , n ) { let m = 1 ; let x = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( m < arr [ i ] ) { x . add ( arr [ i ] ) ; } else if ( m == arr [ i ] ) { m = m + 1 ; while ( x . has ( m ) ) { x . delete ( m ) ; m = m + 1 ; } } } return m ; } let arr = [ 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 ] ; let n = arr . length ; document . write ( findMissingPositive ( arr , n ) ) ;
Sum of all second largest divisors after splitting a number into one or more parts | Function to find a number is prime or not ; If there is any divisor ; Function to find the sum of all second largest divisor after splitting a number into one or more parts ; If number is prime ; If n is even ; If the number is odd ; If N - 2 is prime ; There exists 3 primes x1 , x2 , x3 such that x1 + x2 + x3 = n ; Driver code ; Function call
function prime ( n ) { if ( n == 1 ) return false ; for ( let i = 2 ; i * i <= n ; ++ i ) if ( n % i == 0 ) return false ; return true ; } function Min_Sum ( n ) { if ( prime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; else { if ( prime ( n - 2 ) ) return 2 ; else return 3 ; } } let n = 27 ; document . write ( Min_Sum ( n ) ) ;
Find maximum value of the last element after reducing the array with given operations | Function to return the maximized value ; Overall minimum absolute value of some element from the array ; Add all absolute values ; Count positive and negative elements ; Both positive and negative values are present ; Only positive or negative values are present ; Driver code
function find_maximum_value ( a , n ) { var sum = 0 ; var minimum = Number . MAX_VALUE ; var pos = 0 , neg = 0 ; for ( i = 0 ; i < n ; i ++ ) { minimum = Math . min ( minimum , Math . abs ( a [ i ] ) ) ; sum += Math . abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) pos += 1 ; else neg += 1 ; } if ( pos > 0 && neg > 0 ) return sum ; return ( sum - 2 * minimum ) ; } var a = [ 5 , 4 , 6 , 2 ] ; var n = a . length ; document . write ( find_maximum_value ( a , n ) ) ;
Find Nth smallest number that is divisible by 100 exactly K times | Function to find the Nth smallest number ; If N is divisible by 100 then we multiply N + 1 otherwise , it will be divisible by 100 more than K times ; convert integer to string ; if N is not divisible by 100 ; convert integer to string ; add 2 * K 0 's at the end to be divisible  by 100 exactly K times ; Driver Code
function find_number ( N , K ) { var r ; if ( N % 100 == 0 ) { N += 1 ; r = N . toString ( ) ; } else { r = N . toString ( ) ; } for ( var i = 1 ; i <= K ; i ++ ) r += " " ; return r ; } var N = 1000 , K = 2 ; var ans = find_number ( N , K ) ; document . write ( ans ) ;
Count of adjacent Vowel Consonant Pairs | Function to count the adjacent pairs of consonant and vowels in the String ; Using a set to store the vowels so that checking each character becomes easier ; Variable to store number of consonant - vowel pairs ; If the ith character is not found in the set , means it is a consonant And if the ( i + 1 ) th character is found in the set , means it is a vowel We increment the count of such pairs ; Driver Code
function countPairs ( s ) { let st = new Set ( ) ; st . add ( ' ' ) ; st . add ( ' ' ) ; st . add ( ' ' ) ; st . add ( ' ' ) ; st . add ( ' ' ) ; let count = 0 ; let n = s . length ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( st . has ( s [ i ] ) && ! st . has ( s [ i + 1 ] ) ) count ++ ; } return count ; } let s = " " ; document . write ( countPairs ( s ) ) ;
Find the original coordinates whose Manhattan distances are given | Function to find the original coordinated ; Maximum of the given distances ; Sum of the given distances ; Conditions when the solution doesn 't exist ; First coordinate ; Second coordinate ; Third coordinate ; Driver code
function solve ( d1 , d2 , d3 ) { let maxx = Math . max ( d1 , Math . max ( d2 , d3 ) ) ; let sum = ( d1 + d2 + d3 ) ; if ( 2 * maxx > sum sum % 2 == 1 ) { document . write ( " " ) ; return ; } let x1 = 0 , y1 = 0 ; let x2 = d1 , y2 = 0 ; let x3 = parseInt ( ( d1 + d2 - d3 ) / 2 ) ; let y3 = parseInt ( ( d2 + d3 - d1 ) / 2 ) ; document . write ( " " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + x3 + " " + y3 + " " ) ; } let d1 = 3 , d2 = 4 , d3 = 5 ; solve ( d1 , d2 , d3 ) ;
Find the largest interval that contains exactly one of the given N integers . | Function to return the maximum size of the required interval ; Insert the borders for array ; Sort the elements in ascending order ; To store the maximum size ; To store the range [ L , R ] such that only v [ i ] lies within the range ; Total integers in the range ; Driver code
function maxSize ( v , n ) { v . push ( 0 ) ; v . push ( 100001 ) ; n += 2 ; v . sort ( ( a , b ) => a - b ) ; let mx = 0 ; for ( let i = 1 ; i < n - 1 ; i ++ ) { let L = v [ i - 1 ] + 1 ; let R = v [ i + 1 ] - 1 ; let cnt = R - L + 1 ; mx = Math . max ( mx , cnt ) ; } return mx ; } let v = [ 200 , 10 , 5 ] ; let n = v . length ; document . write ( maxSize ( v , n ) ) ;
Maximum length of subarray such that sum of the subarray is even | Function to find length of the longest subarray such that sum of the subarray is even ; Check if sum of complete array is even ; if ( sum % 2 == 0 ) total sum is already even ; Find an index i such the a [ i ] is odd and compare length of both halfs excluding a [ i ] to find max length subarray ; Driver Code
function maxLength ( a , n ) { let sum = 0 , len = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return n ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) len = Math . max ( len , Math . max ( n - i - 1 , i ) ) ; } return len ; } let a = [ 1 , 2 , 3 , 2 ] ; let n = a . length ; document . write ( maxLength ( a , n ) + " " ) ;
Find a Symmetric matrix of order N that contain integers from 0 to N | Function to generate the required matrix ; Form cyclic array of elements 1 to n - 1 ; Store initial array into final array ; Fill the last row and column with 0 's ; Swap 0 and the number present at the current indexed row ; Also make changes in the last row with the number we swapped ; Print the final array ; Driver Code
function solve ( n ) { let initial_array = new Array ( n - 1 ) ; for ( var i = 0 ; i < initial_array . length ; i ++ ) { initial_array [ i ] = new Array ( 2 ) ; } let final_array = new Array ( n ) ; for ( var i = 0 ; i < final_array . length ; i ++ ) { final_array [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < n - 1 ; ++ i ) initial_array [ 0 ] [ i ] = i + 1 ; for ( let i = 1 ; i < n - 1 ; ++ i ) for ( let j = 0 ; j < n - 1 ; ++ j ) initial_array [ i ] [ j ] = initial_array [ i - 1 ] [ ( j + 1 ) % ( n - 1 ) ] ; for ( let i = 0 ; i < n - 1 ; ++ i ) for ( let j = 0 ; j < n - 1 ; ++ j ) final_array [ i ] [ j ] = initial_array [ i ] [ j ] ; for ( let i = 0 ; i < n ; ++ i ) final_array [ i ] [ n - 1 ] = final_array [ n - 1 ] [ i ] = 0 ; for ( let i = 0 ; i < n ; ++ i ) { let t0 = final_array [ i ] [ i ] ; let t1 = final_array [ i ] [ n - 1 ] ; let s = final_array [ i ] [ i ] ; final_array [ i ] [ i ] = final_array [ i ] [ n - 1 ] ; final_array [ i ] [ n - 1 ] = s ; final_array [ n - 1 ] [ i ] = t0 ; } for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 0 ; j < n ; ++ j ) document . write ( final_array [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let n = 5 ; solve ( n ) ;
Generate original array from difference between every two consecutive elements | Function to print the required permutation ; Take x = 0 for simplicity ; Calculate aint the differences and store it in a vector ; Preserve the original array ; Check if aint the consecutive elements have difference = 1 ; If consecutive elements don 't have  difference 1 at any position then  the answer is impossible ; Else store the indices and values at those indices in a map and finainty print them ; Driver code
function findPerm ( n , differences ) { let ans = [ ] ; ans . push ( 0 ) ; let x = 0 ; for ( let i = 0 ; i <= n - 2 ; ++ i ) { let diff = differences [ i ] ; x = x + diff ; ans . push ( x ) ; } let anss = [ ] ; for ( let obj = 0 ; obj < ans . length ; obj ++ ) anss . push ( ans [ obj ] ) ; ans . sort ( function ( a , b ) { return a - b ; } ) ; let flag = - 1 ; for ( let i = 1 ; i <= n - 1 ; ++ i ) { let res = ans [ i ] - ans [ i - 1 ] ; if ( res != 1 ) { flag = 0 ; } } if ( flag == 0 ) { document . write ( - 1 ) ; return ; } else { let mpp = new Map ( ) ; let j = 1 ; let value_at_index = [ ] ; for ( let x1 = 0 ; x1 < ans . length ; x1 ++ ) { mpp . set ( ans [ x1 ] , j ) ; ++ j ; } for ( let x2 = 0 ; x2 < anss . length ; x2 ++ ) { value_at_index . push ( mpp . get ( anss [ x2 ] ) ) ; } for ( let x3 = 0 ; x3 < value_at_index . length ; x3 ++ ) { document . write ( value_at_index [ x3 ] + " " ) ; } document . write ( " " ) ; } } let differences = [ ] ; differences . push ( 2 ) ; differences . push ( - 3 ) ; differences . push ( 2 ) ; let n = differences . length + 1 ; findPerm ( n , differences ) ;
Find smallest number K such that K % p = 0 and q % K = 0 | Function to return the minimum value K such that K % p = 0 and q % k = 0 ; If K is possible ; No such K is possible ; driver program
function getMinVal ( p , q ) { if ( q % p == 0 ) return p ; return - 1 ; } let p = 24 , q = 48 ; document . write ( getMinVal ( p , q ) ) ;
Count all Prime Length Palindromic Substrings | Function that returns true if sub - string starting at i and ending at j in str is a palindrome ; Function to count all palindromic substring whose lwngth is a prime number ; 0 and 1 are non - primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; To store the required number of sub - strings ; Starting from the smallest prime till the largest length of the sub - string possible ; If j is prime ; Check all the sub - strings of length j ; If current sub - string is a palindrome ; Driver Code
function isPalindrome ( str , i , j ) { while ( i < j ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; j -- ; } return true ; } function countPrimePalindrome ( str , len ) { var prime = Array ( len + 1 ) . fill ( true ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( var p = 2 ; p * p <= len ; p ++ ) { if ( prime [ p ] ) { for ( var i = p * p ; i <= len ; i += p ) prime [ i ] = false ; } } var count = 0 ; for ( var j = 2 ; j <= len ; j ++ ) { if ( prime [ j ] ) { for ( var i = 0 ; i + j - 1 < len ; i ++ ) { if ( isPalindrome ( str , i , i + j - 1 ) ) count ++ ; } } } return count ; } var s = " " ; var len = s . length ; document . write ( countPrimePalindrome ( s , len ) ) ;
Minimum operations of the given type required to make a complete graph | Function to return the minimum number of steps required ; Driver Code
function minOperations ( N ) { var x = Math . log ( N ) / Math . log ( 2 ) ; var ans = parseInt ( ( Math . ceil ( x ) ) ) ; return ans ; } var N = 10 ; document . write ( minOperations ( N ) ) ;
Greatest divisor which divides all natural number in range [ L , R ] | Function to return the greatest divisor that divides all the natural numbers in the range [ l , r ] ; Driver Code
function find_greatest_divisor ( l , r ) { if ( l == r ) return l ; return 1 ; } let l = 2 ; let r = 12 ; document . write ( find_greatest_divisor ( l , r ) ) ;
Probability of getting two consecutive heads after choosing a random coin among two different types of coins | Function to return the probability of getting two consecutive heads ; Formula derived from Bayes 's theorem ; given the probability of getting a head for both the coins
function getProbability ( p , q ) { p /= 100 ; q /= 100 ; let probability = ( p * p + q * q ) / ( p + q ) ; return probability ; } let p = 80.0 ; let q = 40.0 ; document . write ( getProbability ( p , q ) . toPrecision ( 15 ) ) ;
Check whether bitwise OR of N numbers is Even or Odd | Function to check if bitwise OR of n numbers is even or odd ; if at least one odd number is found , then the bitwise OR of all numbers will be odd ; Bitwise OR is an odd number ; Driver Code
function check ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) return true ; } return false ; } let arr = [ 3 , 9 , 12 , 13 , 15 ] ; let n = arr . length ; if ( check ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Iterated Logarithm log * ( n ) | Javascript program to find value of Iterated Logarithm ; Driver code
function _log ( x , base ) { return ( Math . log ( x ) / Math . log ( base ) ) ; } function recursiveLogStar ( n , b ) { if ( n > 1.0 ) return 1.0 + recursiveLogStar ( _log ( n , b ) , b ) ; else return 0 ; } let n = 100 , base = 5 ; document . write ( " " + n + " " + recursiveLogStar ( n , base ) ) ;
Auxiliary Space with Recursive Functions |
function sum ( sum ) { if ( n <= 0 ) return 0 ; return n + sum ( n - 1 ) ; }
Find the maximum among the count of positive or negative integers in the array | Function to find the maximum of the count of positive or negative elements ; Initialize the pointers ; Find the value of mid ; If element is negative then ignore the left half ; If element is positive then ignore the right half ; Return maximum among the count of positive & negative element ; Driver Code
function findMaximum ( arr , size ) { let i = 0 , j = size - 1 , mid ; while ( i <= j ) { mid = i + Math . floor ( ( j - i ) / 2 ) ; if ( arr [ mid ] < 0 ) i = mid + 1 ; else if ( arr [ mid ] > 0 ) j = mid - 1 ; } return Math . max ( i , size - i ) ; } let arr = [ - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 ] ; let N = arr . length ; document . write ( findMaximum ( arr , N ) ) ;
Perform given queries on Queue according to the given rules | Function to perform all the queries operations on the given queue ; Stores the count query of type 1 ; Event E1 : increase countE1 ; Event E2 : add the X in set ; Event E3 : Find position of X ; Initial position is ( position - countE1 ) ; If X is already removed or popped out ; Finding the position of X in queue ; Traverse set to decrease position of X for all the number removed in front ; Print the position of X ; Driver Code
function solve ( n , m , queries ) { let countE1 = 0 ; let removed = new Set ( ) ; for ( let i = 0 ; i < m ; i ++ ) { if ( queries [ i ] [ 0 ] == 1 ) ++ countE1 ; else if ( queries [ i ] [ 0 ] == 2 ) removed . add ( queries [ i ] [ 1 ] ) ; else { let position = queries [ i ] [ 1 ] - countE1 ; if ( removed . has ( queries [ i ] [ 1 ] ) != false position <= 0 ) document . write ( " " + " " ) ; else { for ( let it of removed ) { if ( it > queries [ i ] [ 1 ] ) break ; position -- ; } document . write ( position + " " ) ; } } } } let N = 5 , Q = 3 ; let queries = new Array ( Q ) ; for ( let i = 0 ; i < Q ; i ++ ) { queries [ i ] = new Array ( 2 ) ; } queries [ 0 ] [ 0 ] = 1 ; queries [ 0 ] [ 1 ] = 0 ; queries [ 1 ] [ 0 ] = 3 ; queries [ 1 ] [ 1 ] = 3 ; queries [ 2 ] [ 0 ] = 2 ; queries [ 2 ] [ 1 ] = 2 ; solve ( N , Q , queries ) ;
Maximum index a pointer can reach in N steps by avoiding a given index B | JavaScript Program to implement the above approach ; Function to find the maximum index reachable ; Store the answer ; Store the maximum index possible i . e , N * ( N - 1 ) ; Add bthe previous elements ; Run a loop ; Binary Search ; Driver Code
function lowerBound ( Ans , target ) { const targetRange = [ - 1 , - 1 ] const leftIdx = extremeInsertionIndex ( Ans , target , true ) if ( leftIdx === Ans . length Ans [ leftIdx ] != target ) return targetRange targetRange [ 0 ] = leftIdx targetRange [ 1 ] = extremeInsertionIndex ( Ans , target , false ) - 1 return targetRange function extremeInsertionIndex ( Ans , target , left ) { let lo = 0 , hi = Ans . length while ( lo < hi ) { const mid = lo + Math . floor ( ( hi - lo ) / 2 ) if ( Ans [ mid ] > target || ( left && target === Ans [ mid ] ) ) hi = mid else lo = mid + 1 } return lo } } function maxIndex ( N , B ) { let maximumIndexReached = 0 ; let Ans = [ ] ; for ( let i = 1 ; i <= N ; i ++ ) { maximumIndexReached += i ; Ans . push ( i ) ; } Ans . reverse ( ) ; for ( let i = 1 ; i < Ans . length ; i ++ ) { Ans [ i ] += Ans [ i - 1 ] ; } while ( maximumIndexReached ) { let it = lowerBound ( Ans , maximumIndexReached - B ) ; if ( it == - 1 ) { break ; } if ( it == maximumIndexReached - B ) { maximumIndexReached -- ; } else { break ; } } return maximumIndexReached ; } let N = 3 , B = 2 ; document . write ( maxIndex ( N , B ) ) ;
Maximize Kth largest element after splitting the given Array at most C times | Function to find the K - th maximum element after upto C operations ; Check for the base case ; Stores the count iterations of BS ; Create the left and right bounds of binary search ; Perform binary search ; Find the value of mid ; Traverse the array ; Update the ranges ; Return the maximum value obtained ; Driver Code
function maxKth ( arr , N , C , K ) { if ( N + C < K ) { return - 1 ; } let iter = 300 ; let l = 0 , r = 1000000000.0 ; while ( iter -- ) { let mid = ( l + r ) * 0.5 ; let a = 0 ; let b = 0 ; for ( let i = 0 ; i < N ; i ++ ) { a += Math . floor ( arr [ i ] / mid ) ; if ( arr [ i ] >= mid ) { b ++ ; } } if ( a >= K && b + C >= K ) { l = mid ; } else { r = mid ; } } return l ; } let arr = [ 5 , 8 ] ; let K = 1 , C = 1 ; let N = arr . length ; document . write ( maxKth ( arr , N , C , K ) ) ;