codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs from a given range having even sum | Function to find the count of ordered pairs having even sum ; Stores count of even numbers in the range [ L , R ] ; If L is even ; Update count_even ; Update count_odd ; Stores count of odd numbers in the range [ L , R ] ; Update count_odd ; Update count_odd ; Stores count of pairs whose sum is even and both elements of the pairs are also even ; Stores count of pairs whose sum is even and both elements of the pairs are odd ; Print total ordered pairs whose sum is even ; Given L & R ; Function Call
function countPairs ( L , R ) { let count_even ; if ( L % 2 == 0 ) { count_even = ( R / 2 ) - ( L / 2 ) + 1 ; } else { count_even = ( R / 2 ) - ( L / 2 ) ; } let count_odd ; if ( L % 2 == 0 ) { count_odd = ( ( R + 1 ) / 2 ) - ( ( L + 1 ) / 2 ) ; } else { count_odd = ( ( R + 1 ) / 2 ) - ( ( L + 1 ) / 2 ) + 1 ; } count_even *= count_even ; count_odd *= count_odd ; document . write ( count_even + count_odd ) ; } let L = 1 , R = 3 ; countPairs ( L , R ) ;
Count of array elements that can be found using Randomized Binary Search on every array element | Function to find minimum count of array elements found by repeatedly applying Randomized Binary Search ; Stores count of array elements ; smallestRight [ i ] : Stores the smallest array element on the right side of i ; Update smallestRight [ 0 ] ; Traverse the array from right to left ; Update smallestRight [ i ] ; Stores the largest element upto i - th index ; Stores the minimum count of elements found by repeatedly applying Randomized Binary Search ; If largest element on left side is less than smallest element on right side ; Update ans ; Update mn ; Given array ; Function Call
function getDefiniteFinds ( arr ) { let n = arr . length ; let smallestRight = new Array ( n + 1 ) . fill ( 0 ) ; smallestRight [ n ] = Number . MAX_VALUE ; for ( let i = n - 1 ; i >= 0 ; i -- ) { smallestRight [ i ] = Math . min ( smallestRight [ i + 1 ] , arr [ i ] ) ; } let mn = Number . MIN_VALUE ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( mn < arr [ i ] && arr [ i ] < smallestRight [ i + 1 ] ) { ans ++ ; } mn = Math . max ( arr [ i ] , mn ) ; } return ans ; } let arr = [ 5 , 4 , 9 ] ; document . write ( getDefiniteFinds ( arr ) ) ;
Count numbers from a given range having same first and last digits in their binary representation | Function to count numbers in range [ L , R ] having same first and last digit in the binary representation ; Given range [ L , R ] ; Function Call
function Count_numbers ( L , R ) { var count = ( R - L ) / 2 ; if ( R % 2 != 0 L % 2 != 0 ) count += 1 ; document . write ( count ) ; } var L = 6 , R = 30 ; Count_numbers ( L , R ) ;
Find the index having sum of elements on its left equal to reverse of the sum of elements on its right | Function to check if a number is equal to the reverse of digits of other number ; Stores reverse of digits of rightSum ; Stores rightSum ; Calculate reverse of digits of temp ; Update rev ; Update temp ; If reverse of digits of rightSum equal to leftSum ; Function to find the index that satisfies the condition ; Stores sum of array elements on right side of each index ; Stores sum of array elements on left side of each index ; Traverse the array ; Update rightSum ; Traverse the array ; Update rightSum ; If leftSum equal to reverse of digits of rightSum ; Update leftSum ; Driver code
function checkReverse ( leftSum , rightSum ) { var rev = 0 ; var temp = rightSum ; while ( temp != 0 ) { rev = ( rev * 10 ) + ( temp % 10 ) ; temp = parseInt ( temp / 10 ) ; } if ( rev == leftSum ) { return true ; } return false ; } function findIndex ( arr , N ) { var rightSum = 0 ; var leftSum = 0 ; for ( i = 0 ; i < N ; i ++ ) { rightSum += arr [ i ] ; } for ( i = 0 ; i < N ; i ++ ) { rightSum -= arr [ i ] ; if ( checkReverse ( leftSum , rightSum ) ) { return i ; } leftSum += arr [ i ] ; } return - 1 ; } var arr = [ 5 , 7 , 3 , 6 , 4 , 9 , 2 ] ; var N = arr . length ; document . write ( findIndex ( arr , N ) ) ;
Longest Common Subsequence of two arrays out of which one array consists of distinct elements only | Function to find the longest Common Subsequence between the two arrays ; Maps elements of firstArr [ ] to their respective indices ; Traverse the array firstArr [ ] ; Stores the indices of common elements between firstArr [ ] and secondArr [ ] ; Traverse the array secondArr [ ] ; If current element exists in the Map ; Stores lIS from tempArr [ ] ; Driver Code
function LCS ( firstArr , secondArr ) { let mp = new Map ( ) for ( let i = 0 ; i < firstArr . length ; i ++ ) { mp . set ( firstArr [ i ] , i + 1 ) ; } let tempArr = [ ] ; for ( let i = 0 ; i < secondArr . length ; i ++ ) { if ( mp . has ( secondArr [ i ] ) ) { tempArr . push ( mp . get ( secondArr [ i ] ) ) ; } } let tail = [ ] ; tail . push ( tempArr [ 0 ] ) ; for ( let i = 1 ; i < tempArr . length ; i ++ ) { if ( tempArr [ i ] > tail [ tail . length - 1 ] ) tail . push ( tempArr [ i ] ) ; else if ( tempArr [ i ] < tail [ 0 ] ) tail . unshift ( 0 , tempArr [ i ] ) ; } return tail . length ; } let firstArr = [ 3 , 5 , 1 , 8 ] ; let secondArr = [ 3 , 3 , 5 , 3 , 8 ] ; document . write ( LCS ( firstArr , secondArr ) ) ;
Count ways to partition a Binary String such that each substring contains exactly two 0 s | Function to find count of ways to partition the string such that each partition contains exactly two 0 s . ; Stores indices of 0 s in the given string . ; Store the count of ways to partition the string such that each partition contains exactly two 0 s . ; Iterate over each characters of the given string ; If current character is '0' ; Insert index ; Stores total count of 0 s in str ; Traverse the array , IdxOf0s [ ] ; Update cntWays ; Driver Code
function totalWays ( n , str ) { var IdxOf0s = [ ] ; var cntWays = 1 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { IdxOf0s . push ( i ) ; } } var M = IdxOf0s . length ; if ( M == 0 M % 2 ) { return 0 ; } for ( var i = 2 ; i < M ; i += 2 ) { cntWays = cntWays * ( IdxOf0s [ i ] - IdxOf0s [ i - 1 ] ) ; } return cntWays ; } var str = " " ; var n = str . length ; document . write ( totalWays ( n , str ) ) ;
Count non | Function to find the total count of triplets ( i , j , k ) such that i < j < k and ( j - i ) != ( k - j ) ; Stores indices of 0 s ; Stores indices of 1 s ; Stores indices of 2 s ; Traverse the array ; If current array element is 0 ; If current array element is 1 ; If current array element is 2 ; Total count of triplets ; Traverse the array zero_i [ ] ; Traverse the array one_i [ ] ; Stores index of 0 s ; Stores index of 1 s ; Stores third element of triplets that does not satisfy the condition ; If r present in the map ; Update r ; If r present in the map ; Update r ; If r present in the map and equidistant ; Print the obtained count ; Driver Code
function countTriplets ( arr , N ) { var zero_i = [ ] ; var one_i = [ ] ; var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zero_i . push ( i + 1 ) ; else if ( arr [ i ] == 1 ) one_i . push ( i + 1 ) ; else mp . set ( i + 1 , 1 ) ; } var total = zero_i . length * one_i . length * mp . size ; for ( var i = 0 ; i < zero_i . length ; i ++ ) { for ( var j = 0 ; j < one_i . length ; j ++ ) { var p = zero_i [ i ] ; var q = one_i [ j ] ; var r = 2 * p - q ; if ( mp . has ( r ) ) total -- ; r = 2 * q - p ; if ( mp . has ( r ) ) total -- ; r = ( p + q ) / 2 ; if ( mp . has ( r ) && Math . abs ( r - p ) == Math . abs ( r - q ) ) total -- ; } } document . write ( total ) ; } var arr = [ 0 , 1 , 2 , 1 ] ; var N = arr . length ; countTriplets ( arr , N ) ;
Print prime factors of a given integer in decreasing order using Stack | Function to print prime factors of N in decreasing order ; Stores prime factors of N in decreasing order ; Insert i into stack ; Update N ; Update i ; Print value of stack st ; Driver Code ; Function Call
function PrimeFactors ( N ) { let st = [ ] ; let i = 2 ; while ( N != 1 ) { if ( N % i == 0 ) { st . push ( i ) ; while ( N % i == 0 ) { N = Math . floor ( N / i ) ; } } i ++ ; } while ( st . length != 0 ) { document . write ( st . pop ( ) ) ; } } let N = 8 ; PrimeFactors ( N ) ;
Count subarrays consisting of first K natural numbers in descending order | Function to count subarray having the decreasing sequence K to 1 ; Traverse the array ; Check if required sequence is present or not ; Reset temp to k ; Return the count ; Driver Code ; Function Call
function CountSubarray ( arr , n , k ) { var temp = k , count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == temp ) { if ( temp == 1 ) { count ++ ; temp = k ; } else temp -- ; } else { temp = k ; if ( arr [ i ] == k ) i -- ; } } return count ; } var arr = [ 1 , 2 , 3 , 7 , 9 , 3 , 2 , 1 , 8 , 3 , 2 , 1 ] ; var N = arr . length ; var K = 3 ; document . write ( CountSubarray ( arr , N , K ) ) ;
Smallest number not less than N which is divisible by all digits of N | Function to calculate the LCM ; Function to find the smallest number satisfying given constraints ; LCM value is 1 initially ; Finding the LCM of all non zero digits ; Update the value lcm ; Stores ceil value ; Print the answer ; Driver Code ; Function Call
function LCM ( A , B ) { return ( A * B / __gcd ( A , B ) ) ; } function findSmallestNumber ( X ) { let lcm = 1 ; let temp = X ; while ( temp > 0 ) { let last = temp % 10 ; temp = Math . floor ( temp / 10 ) ; if ( last == 0 ) continue ; lcm = LCM ( lcm , last ) ; } let answer = Math . floor ( ( X + lcm - 1 ) / lcm ) * lcm ; document . write ( answer ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } let X = 280 ; findSmallestNumber ( X ) ;
Minimum sum of two integers whose product is strictly greater than N | Function to find the minimum sum of two integers such that their product is strictly greater than N ; Initialise low as 0 and high as 1e9 ; Iterate to find the first number ; Find the middle value ; If mid ^ 2 is greater than equal to A , then update high to mid ; Otherwise update low ; Store the first number ; Again , set low as 0 and high as 1e9 ; Iterate to find the second number ; Find the middle value ; If first number * mid is greater than N then update high to mid ; Else , update low to mid ; Store the second number ; Print the result ; Driver Code ; Function Call
function minSum ( N ) { let low = 0 , high = 1000000000 ; while ( low + 1 < high ) { let mid = low + parseInt ( ( high - low ) / 2 ) ; if ( mid * mid >= N ) { high = mid ; } else { low = mid ; } } let first = high ; low = 0 ; high = 1000000000 ; while ( low + 1 < high ) { let mid = low + parseInt ( ( high - low ) / 2 ) ; if ( first * mid > N ) { high = mid ; } else { low = mid ; } } let second = high ; document . write ( first + second ) ; } let N = 10 ; minSum ( N ) ;
Minimum sum of two integers whose product is strictly greater than N | Function to find the minimum sum of two integers such that their product is strictly greater than N ; Store the answer using the AP - GP inequality ; Print the answer ; Driver Code ; Function Call
function minSum ( N ) { let ans = Math . ceil ( 2 * Math . sqrt ( N + 1 ) ) ; document . write ( ans ) ; } let N = 10 ; minSum ( N ) ;
Find the array element having maximum frequency of the digit K | Function to find the count of digits , k in the given number n ; Stores count of occurrence of digit K in N ; Iterate over digits of N ; If current digit is k ; Update count ; Update N ; Utility function to find an array element having maximum frequency of digit k ; Stores frequency of digit K in arr [ i ] ; Stores maximum frequency of digit K in the array ; Stores an array element having maximum frequency of digit k ; Initialize max ; Traverse the array ; Count the frequency of digit k in arr [ i ] ; Update max with maximum frequency found so far ; Update ele ; If there is no array element having digit k in it ; Function to find an array element having maximum frequency of digit k ; Stores an array element having maximum frequency of digit k ; If there is no element found having digit k in it ; Prlet the element having max frequency of digit k ; The digit whose max occurrence has to be found ; Given array ; Size of array ; Function Call
function countFreq ( N , K ) { let count = 0 ; while ( N > 0 ) { if ( N % 10 == K ) { count ++ ; } N = Math . floor ( N / 10 ) ; } return count ; } function findElementUtil ( arr , N , K ) { let c ; let max ; let ele = 0 ; max = 0 ; for ( let i = 0 ; i < N ; i ++ ) { c = countFreq ( arr [ i ] , K ) ; if ( c > max ) { max = c ; ele = arr [ i ] ; } } if ( max == 0 ) return - 1 ; else return ele ; } function findElement ( arr , N , K ) { let ele = findElementUtil ( arr , N , K ) ; if ( ele == - 1 ) document . write ( " " ) ; else document . write ( ele ) ; } let K = 3 ; let arr = [ 3 , 77 , 343 , 456 ] ; let N = arr . length ; findElement ( arr , K , N ) ;
Minimum removals required to make any interval equal to the union of the given Set | Function to count minimum number of removals required to make an interval equal to the union of the given Set ; Stores the minimum number of removals ; Traverse the Set ; Left Boundary ; Right Boundary ; Stores count of intervals lying within current interval ; Traverse over all remaining intervals ; Check if interval lies within the current interval ; Increase count ; Update minimum removals required ; Driver Code ; Returns the minimum removals
function findMinDeletions ( v , n ) { let minDel = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { let L = v [ i ] [ 0 ] ; let R = v [ i ] [ 1 ] ; let Count = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( v [ j ] [ 0 ] >= L && v [ j ] [ 1 ] <= R ) { Count += 1 ; } } minDel = Math . min ( minDel , n - Count ) ; } return minDel ; } let v = [ [ 1 , 3 ] , [ 4 , 12 ] , [ 5 , 8 ] , [ 13 , 20 ] ] ; let N = v . length ; document . write ( findMinDeletions ( v , N ) ) ;
Maximum even numbers present in any subarray of size K | Function to find the maximum count of even numbers from all the subarrays of size K ; Stores the count of even numbers in a subarray of size K ; Calculate the count of even numbers in the current subarray ; If current element is an even number ; Stores the maximum count of even numbers from all the subarrays of size K ; Traverse remaining subarrays of size K using sliding window technique ; If the first element of the subarray is even ; Update curr ; If i - th element is even increment the count ; Update the answer ; Return answer ; Driver Code ; Size of the input array ; Function call
function maxEvenLetegers ( arr , N , M ) { let curr = 0 ; for ( let i = 0 ; i < M ; i ++ ) { if ( arr [ i ] % 2 == 0 ) curr ++ ; } let ans = curr ; for ( let i = M ; i < N ; i ++ ) { if ( arr [ i - M ] % 2 == 0 ) { curr -- ; } if ( arr [ i ] % 2 == 0 ) curr ++ ; ans = Math . max ( ans , curr ) ; } return ans ; } let arr = [ 2 , 3 , 5 , 4 , 7 , 6 ] ; let M = 3 ; let N = arr . length ; document . write ( maxEvenLetegers ( arr , N , M ) + " " ) ;
Count numbers up to N having digit D in its octal representation | Function to count the numbers in given range whose octal representation contains atleast digit , d ; Store count of numbers up to n whose octal representation contains digit d ; Iterate over the range [ 1 , n ] ; Calculate digit of i in octal representation ; Check if octal representation of x contains digit d ; Update total ; Update x ; Prlet the answer ; Given N and D ; Counts and prlets numbers up to N having D as a digit in its octal representation
function countNumbers ( n , d ) { let total = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let x = i ; while ( x > 0 ) { if ( x % 8 == d ) { total ++ ; break ; } x = x / 8 ; } } document . write ( total ) ; } let n = 20 , d = 7 ; countNumbers ( n , d ) ;
Count number of triplets ( a , b , c ) from first N natural numbers such that a * b + c = N | Function to find the count of triplets ( a , b , c ) with a * b + c = N ; Stores count of triplets of 1 st N natural numbers which are of the form a * b + c = N ; Iterate over the range [ 1 , N ] ; If N is divisible by i ; Update cntTriplet ; Update cntTriplet ; Driver Code
function findCntTriplet ( N ) { let cntTriplet = 0 ; for ( let i = 1 ; i < N ; i ++ ) { if ( N % i != 0 ) { cntTriplet += Math . floor ( N / i ) ; } else { cntTriplet += Math . floor ( N / i ) - 1 ; } } return cntTriplet ; } let N = 3 ; document . write ( findCntTriplet ( N ) ) ;
Count divisors which generates same Quotient and Remainder | Function to calculate the count of numbers such that it gives same quotient and remainder ; Stores divisor of number N . ; Iterate through numbers from 2 to sqrt ( N ) and store divisors of N ; As N is also divisor of itself ; Iterate through divisors ; Checking whether x satisfies the required condition ; Print the count ; Given N ; Function Call
function countDivisors ( n ) { var count = 0 ; var j = 0 ; var divisor = Array ( n ) . fill ( 0 ) ; for ( var i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( parseInt ( n / i ) == i ) { divisor [ j ] = i ; j += 1 ; } else { divisor [ j ] = i ; divisor [ j + 1 ] = parseInt ( n / i ) ; j += 2 ; } } } divisor [ j ] = n ; for ( var i = 0 ; i <= j ; i ++ ) { var x = divisor [ i ] ; x -= 1 ; if ( parseInt ( n / x ) == parseInt ( n % x ) ) count ++ ; } document . write ( count ) ; } var N = 10000000 ; countDivisors ( N ) ;
Smallest indexed array element required to be flipped to make sum of array equal to 0 | Function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; Stores the required index ; Traverse the given array ; Flip the sign of current element ; Stores the sum of array elements ; Find the sum of the array ; Update sum ; If sum is equal to 0 ; Update pos ; Reset the current element to its original value ; Reset the value of arr [ i ] ; Driver Code
function smallestIndexArrayElementsFlip ( arr , N ) { var pos = - 1 ; var i , j ; for ( i = 0 ; i < N ; i ++ ) { arr [ i ] *= - 1 ; var sum = 0 ; for ( j = 0 ; j < N ; j ++ ) { sum += arr [ j ] ; } if ( sum == 0 ) { pos = i ; break ; } else { arr [ i ] *= - 1 ; } } return pos ; } var arr = [ 1 , 3 , - 5 , 3 , 4 ] ; var N = arr . length ; document . write ( smallestIndexArrayElementsFlip ( arr , N ) ) ;
Smallest indexed array element required to be flipped to make sum of array equal to 0 | Function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; Stores the required index ; Stores the sum of the array ; Traverse the given array ; Update ArrSum ; Traverse the given array ; If sum of array elements double the value of the current element ; Update pos ; Driver Code
function smallestIndexArrayElementsFlip ( arr , N ) { let pos = - 1 ; let ArrSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { ArrSum += arr [ i ] ; } for ( let i = 0 ; i < N ; i ++ ) { if ( 2 * arr [ i ] == ArrSum ) { pos = i ; break ; } } return pos ; } let arr = [ 1 , 3 , - 5 , 3 , 4 ] ; let N = arr . length ; document . write ( smallestIndexArrayElementsFlip ( arr , N ) ) ;
Check if a string can be converted to another given string by removal of a substring | Function to check if S can be converted to T by removing at most one subString from S ; Check if S can be converted to T by removing at most one subString from S ; Stores length of String T ; Stores length of String S ; Iterate over the range [ 0 , M - 1 ] ; Stores Length of the subString { S [ 0 ] , ... , S [ i ] } ; Stores Length of the subString { S [ 0 ] , ... , S [ i ] } ; Stores prefix subString ; Stores suffix subString ; Checking if prefix + suffix == T ; Given String S and T ; Function call
function make_String_S_to_T ( S , T ) { var possible = false ; var M = T . length ; var N = S . length ; for ( i = 0 ; i <= M ; i ++ ) { var prefix_length = i ; var suffix_length = M - i ; var prefix = S . substring ( 0 , prefix_length ) ; var suffix = S . substring ( N - suffix_length , N ) ; if ( ( prefix + suffix ) == ( T ) ) { possible = true ; break ; } } if ( possible ) return " " ; else return " " ; } var S = " " ; var T = " " ; document . write ( make_String_S_to_T ( S , T ) ) ;
Sum of maximum of all subarrays by adding even frequent maximum twice | Function to calculate sum of maximum of all subarrays ; Stores the sum of maximums ; Traverse the array ; Store the frequency of the maximum element in subarray ; Finding maximum ; Increment frequency by 1 ; If new maximum is obtained ; If frequency of maximum is even , then add 2 * maxNumber . Otherwise , add maxNumber ; Print the sum obtained ; Driver Code ; Function Call
function findSum ( a ) { var ans = 0 ; for ( var low = 0 ; low < a . length ; low ++ ) { for ( var high = low ; high < a . length ; high ++ ) { var count = 0 ; var maxNumber = 0 ; for ( var i = low ; i <= high ; i ++ ) { if ( a [ i ] === maxNumber ) count ++ ; else if ( a [ i ] > maxNumber ) { maxNumber = a [ i ] ; count = 1 ; } } ans += maxNumber * ( count % 2 === 0 ? 2 : 1 ) ; } } document . write ( ans ) ; } var arr = [ 2 , 1 , 4 , 4 , 2 ] ; findSum ( arr ) ;
Count pairs of indices having equal prefix and suffix sums | Function to find the count of index pairs having equal prefix and suffix sums ; Maps indices with prefix sums ; Traverse the array ; Update prefix sum ; Update frequency in Map ; Traverse the array in reverse ; Update suffix sum ; Check if any prefix sum of equal value exists or not ; Print the obtained count ; Given array ; Given size ; Function Call
function countPairs ( arr , n ) { let mp1 = new Map ( ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( mp1 . has ( sum ) ) { mp1 . set ( sum , mp1 . get ( sum ) + 1 ) ; } else { mp1 . set ( sum , 1 ) } } sum = 0 ; let ans = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; if ( mp1 . has ( sum ) ) { ans += mp1 . get ( sum ) ; } } document . write ( ans ) ; } let arr = [ 1 , 2 , 1 , 1 ] ; let n = arr . length countPairs ( arr , n ) ;
Queries to count Composite Magic Numbers from a given range [ L , R ] | Check if a number is magic number or not ; Check number is composite number or not ; Corner cases ; Check if the number is a multiple of 2 or 3 ; Check for multiples of remaining primes ; Function to find Composite Magic Numbers in given ranges ; dp [ i ] : Stores the count of composite Magic numbers up to i ; Traverse in the range [ 1 , 1e5 ) ; Check if number is Composite number as well as a Magic number or not ; Prlet results for each query ; Driver code
function isMagic ( num ) { return ( num % 9 == 1 ) ; } function isComposite ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } function find ( L , R , q ) { let dp = [ ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; for ( let i = 1 ; i < 1000005 ; i ++ ) { if ( isComposite ( i ) && isMagic ( i ) == true ) { dp [ i ] = dp [ i - 1 ] + 1 ; } else dp [ i ] = dp [ i - 1 ] ; } for ( let i = 0 ; i < q ; i ++ ) document . write ( dp [ R [ i ] ] - dp [ L [ i ] - 1 ] + " " ) ; } let L = [ 10 , 3 ] ; let R = [ 100 , 2279 ] ; let Q = 2 ; find ( L , R , Q ) ;
Smallest positive integer that divides all array elements to generate quotients with sum not exceeding K | Function to find the smallest positive integer that divides array elements to get the sum <= K ; Stores minimum possible of the smallest positive integer satisfying the condition ; Stores maximum possible of the smallest positive integer satisfying the condition ; Apply binary search over the range [ left , right ] ; Stores middle element of left and right ; Stores sum of array elements ; Traverse the array ; Update sum ; If sum is greater than K ; Update left ; Update right ; Driver Code
function findSmallestInteger ( arr , N , K ) { var left = 1 ; var right = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; while ( left < right ) { var mid = ( left + right ) / 2 ; var sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { sum += parseInt ( ( arr [ i ] + mid - 1 ) / mid ) ; } if ( sum > K ) { left = mid + 1 ; } else { right = mid ; } } return left ; } var arr = [ 1 , 2 , 5 , 9 ] ; var N = arr . length ; var K = 6 ; document . write ( findSmallestInteger ( arr , N , K ) ) ;