codeparrot/xlcost-text-to-code huggingface dataset:
Minimum given operations required to convert a given binary string to all 1 's | Function to return the number of operations required ; ctr will store the number of consecutive ones at the end of the given binary string ; Loop to find number of 1 s at the end of the string ; If the current character is 1 ; If we encounter the first 0 from the LSB position then we 'll break the loop ; Number of operations required is ( l - ctr ) ; Function to remove leading zeroes from the string ; Loop until s [ i ] becomes not equal to 1 ; If we reach the end of the string , it means that string contains only 0 's ; Return the string without leading zeros ; Driver code ; Removing the leading zeroes
function changeToOnes ( str ) { var i , l , ctr = 0 ; l = str . length ; for ( i = l - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) ctr ++ ; else break ; } return l - ctr ; } function removeZeroesFromFront ( str ) { var s ; var i = 0 ; while ( i < str . length && str [ i ] == ' ' ) i ++ ; if ( i == str . length ) s = " " ; else s = str . substring ( i , str . length - i ) ; return s ; } var str = " " ; str = removeZeroesFromFront ( str ) ; document . write ( changeToOnes ( str ) ) ;
Minimum deletions required such that any number X will occur exactly X times | Function to return the minimum deletions required ; To store the frequency of the array elements ; Store frequency of each element ; To store the minimum deletions required ; Value ; It 's frequency ; If number less than or equal to it 's frequency ; Delete extra occurrences ; Delete every occurrence of x ; Driver code
function MinDeletion ( a , n ) { let map = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( map [ a [ i ] ] ) map [ a [ i ] ] ++ ; else map [ a [ i ] ] = 1 } let ans = 0 ; for ( var m in map ) { let x = m ; let frequency = map [ m ] ; if ( x <= frequency ) { ans += ( frequency - x ) ; } else ans += frequency ; } ; return ans ; } let a = [ 2 , 3 , 2 , 3 , 4 , 4 , 4 , 4 , 5 ] ; let n = a . length ; document . write ( MinDeletion ( a , n ) ) ;
Concatenate strings in any order to get Maximum Number of " AB " | Function to find maximum number of ABs ; variable A , B , AB for count strings that end with ' A ' but not end with ' B ' , ' B ' but does not end with ' A ' and ' B ' and ends with ' A ' respectively . ; ' AB ' is already present in string before concatenate them ; count of strings that begins with ' B ' and ends with 'A ; count of strings that begins with ' B ' but does not end with ' A ' ; count of strings that ends with ' A ' but not end with ' B ' ; updating the value of ans and add extra count of ' AB ' ; Driver Code
function maxCountAB ( s , n ) { var A = 0 , B = 0 , BA = 0 , ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var S = s [ i ] ; var L = S . length ; for ( var j = 0 ; j < L - 1 ; j ++ ) { if ( S [ j ] == ' ' && S [ j + 1 ] == ' ' ) { ans ++ ; } } if ( S [ 0 ] == ' ' && S [ L - 1 ] == ' ' ) BA ++ ; else if ( S [ 0 ] == ' ' ) B ++ ; else if ( S [ L - 1 ] == ' ' ) A ++ ; } if ( BA == 0 ) ans += Math . min ( B , A ) ; else if ( A + B == 0 ) ans += BA - 1 ; else ans += BA + Math . min ( B , A ) ; return ans ; } var s = [ " " , " " , " " ] ; var n = s . length ; document . write ( maxCountAB ( s , n ) ) ;
Minimum operations to make sum of neighbouring elements <= X | Function to return the minimum number of operations required ; To store total operations required ; First make all elements equal to x which are currenctly greater ; Left scan the array ; Update the current element such that neighbouring sum is < x ; Driver code
function MinOperations ( n , x , arr ) { let total = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( arr [ i ] > x ) { let difference = arr [ i ] - x ; total = total + difference ; arr [ i ] = x ; } } for ( let i = 1 ; i < n ; ++ i ) { let LeftNeigbouringSum = arr [ i ] + arr [ i - 1 ] ; if ( LeftNeigbouringSum > x ) { let current_diff = LeftNeigbouringSum - x ; arr [ i ] = Math . max ( 0 , arr [ i ] - current_diff ) ; total = total + current_diff ; } } return total ; } let X = 1 ; let arr = [ 1 , 6 , 1 , 2 , 0 , 4 ] ; let N = arr . length ; document . write ( MinOperations ( N , X , arr ) + " " ) ;
Find the repeating and the missing number using two equations | Function to print the required numbers ; Sum of first n natural numbers ; Sum of squares of first n natural numbers ; To store the sum and sum of squares of the array elements ; Driver code
function findNumbers ( arr , n ) { sumN = ( n * ( n + 1 ) ) / 2 ; sumSqN = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; let sum = 0 ; let sumSq = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; sumSq += Math . pow ( arr [ i ] , 2 ) ; } B = ( ( ( sumSq - sumSqN ) / ( sum - sumN ) ) + sumN - sum ) / 2 ; A = sum - sumN + B ; document . write ( " " + A , " " , B ) ; } let arr = [ 1 , 2 , 2 , 3 , 4 ] ; n = arr . length ; findNumbers ( arr , n ) ;
Lexicographically smallest string with given string as prefix | Function to find the whether the string temp starts with str or not ; Base Case ; Check for the corresponding characters in temp & str ; Function to find lexicographic smallest string consisting of the string str as prefix ; Sort the given array string arr [ ] ; If the i - th string contains given string as a prefix , then print the result ; If no string exists then return " - 1" ; Driver Code
function is_prefix ( temp , str ) { if ( temp . length < str . length ) return 0 ; else { for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] != temp [ i ] ) return 0 ; } return 1 ; } } function lexicographicallyString ( input , n , str ) { input = Array . from ( input ) . sort ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let temp = input [ i ] ; if ( is_prefix ( temp , str ) ) { return temp ; } } return " " ; } let arr = [ " " , " " , " " , " " , " " ] ; let S = " " ; let N = 5 ; document . write ( lexicographicallyString ( arr , N , S ) ) ;
Rearrange Array to find K using Binary Search algorithm without sorting | Function to rearrange the array ; Stores the rearranged array ; Stores whether the arrangement is possible or not ; Update K with the position of K ; Stores all elements lesser than and greater than in vector smaller and greater respectively ; Traverse the array arr [ ] ; If arr [ i ] is less than arr [ K ] ; Else ; Iterate unil low is less than or equal to high ; Stores mid point ; If mid is equal to K ; If mid is less than K ; If mid is greater than K ; If f is - 1 ; Iterate in the range [ 1 , N ] ; If ans [ i ] is equal to - 1 ; Print the rearranged array ; Input ; Function Call
function Rearrange ( arr , K , N ) { let ans = new Array ( N + 1 ) ; let f = - 1 ; for ( let i = 0 ; i < N ; i ++ ) { ans [ i ] = - 1 ; } for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == K ) { K = i ; break ; } } let smaller = [ ] ; let greater = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < arr [ K ] ) smaller . push ( arr [ i ] ) ; else if ( arr [ i ] > arr [ K ] ) greater . push ( arr [ i ] ) ; } let low = 0 , high = N - 1 ; while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( mid == K ) { ans [ mid ] = arr [ K ] ; f = 1 ; break ; } else if ( mid < K ) { if ( smaller . length == 0 ) { break ; } ans [ mid ] = smaller [ smaller . length - 1 ] ; smaller . pop ( ) ; low = mid + 1 ; } else { if ( greater . length == 0 ) { break ; } ans [ mid ] = greater [ greater . length - 1 ] ; greater . pop ( ) ; high = mid - 1 ; } } if ( f == - 1 ) { document . write ( - 1 ) ; return ; } for ( let i = 0 ; i < N ; i ++ ) { if ( ans [ i ] == - 1 ) { if ( smaller . length != 0 ) { ans [ i ] = smaller [ smaller . length - 1 ] ; smaller . pop ( ) ; } else if ( greater . length != 0 ) { ans [ i ] = greater [ greater . length - 1 ] ; greater . pop ; } } } for ( let i = 0 ; i < N ; i ++ ) document . write ( ans [ i ] + " " ) ; document . write ( " " ) } let arr = [ 10 , 7 , 2 , 5 , 3 , 8 ] ; let K = 7 ; let N = arr . length ; Rearrange ( arr , K , N ) ;
Minimize K to let Person A consume at least ceil ( N / ( M + 1 ) ) candies based on given rules | Function to find minimum value of K such that the first person gets at least ( N / ( M + 1 ) ) candies ; Find the minimum required value of candies for the first person ; Iterate K from [ 1 , n ] ; Total number of candies ; Candies taken by Person 1 ; Candies taken by 1 st person is minimum of K and candies left ; Traverse the array arr [ ] ; Amount consumed by the person j ; Update the number of candies ; Good share of candies achieved ; Driver Code
function minimumK ( arr , M , N ) { let good = Math . floor ( ( N * 1.0 ) / ( ( M + 1 ) * 1.0 ) ) + 1 ; for ( let i = 1 ; i <= N ; i ++ ) { let K = i ; let candies = N ; let taken = 0 ; while ( candies > 0 ) { taken += Math . min ( K , candies ) ; candies -= Math . min ( K , candies ) ; for ( let j = 0 ; j < M ; j ++ ) { let consume = ( arr [ j ] * candies ) / 100 ; candies -= consume ; } } if ( taken >= good ) { document . write ( i ) ; return ; } } } let N = 13 , M = 1 ; let arr = new Array ( ) ; arr . push ( 50 ) ; minimumK ( arr , M , N ) ;
Total time required to travel a path denoted by a given string | Function to calculate time taken to travel the path ; Stores total time ; Initial position ; Stores visited segments ; Check whether segment is present in the set ; Increment the value of time by 2 ; Insert segment into the set ; Print the value of time ; Driver Code
function calcTotalTime ( path ) { var time = 0 ; var x = 0 , y = 0 ; var s = new Set ( ) ; for ( var i = 0 ; i < path . length ; i ++ ) { var p = x ; var q = y ; if ( path [ i ] == ' ' ) y ++ ; else if ( path [ i ] == ' ' ) y -- ; else if ( path [ i ] == ' ' ) x ++ ; else if ( path [ i ] == ' ' ) x -- ; if ( ! s . has ( [ p + x , q + y ] . toString ( ) ) ) { time += 2 ; s . add ( [ p + x , q + y ] . toString ( ) ) ; } else time += 1 ; } document . write ( time ) } var path = " " ; calcTotalTime ( path ) ;
Cost required to make all array elements equal to 1 | Function to calculate the cost required to make all array elements equal to 1 ; Stores the total cost ; Traverse the array arr [ ] ; If current element is 0 ; Convert 0 to 1 ; Add the cost ; Return the total cost ; Driver Code
function findCost ( A , N ) { var totalCost = 0 ; var i ; for ( i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 0 ) { A [ i ] = 1 ; totalCost += i ; } } return totalCost ; } var arr = [ 1 , 0 , 1 , 0 , 1 , 0 ] var N = arr . length document . write ( findCost ( arr , N ) ) ;
Find the peak index of a given array | Function to find the peak index for the given array ; Base Case ; Check for strictly increasing array ; If the strictly increasing condition is violated , then break ; Stores the value of i , which is a potential peak index ; Second traversal , for strictly decreasing array ; When the strictly decreasing condition is violated , then break ; If i = N - 1 , it means that ans is the peak index ; Otherwise , peak index doesn 't exist ; Driver Code
function peakIndex ( arr ) { var N = arr . length ; if ( arr . length < 3 ) return - 1 ; var i = 0 ; while ( i + 1 < N ) { if ( arr [ i + 1 ] < arr [ i ] arr [ i ] == arr [ i + 1 ] ) break ; i ++ ; } if ( i == 0 i == N - 1 ) return - 1 ; var ans = i ; while ( i < N - 1 ) { if ( arr [ i ] < arr [ i + 1 ] arr [ i ] == arr [ i + 1 ] ) break ; i ++ ; } if ( i == N - 1 ) return ans ; return - 1 ; } var arr = [ 0 , 1 , 0 ] ; document . write ( peakIndex ( arr ) ) ;
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | Set 2 | Function to check if the array has 2 elements whose sum is equal to the given value ; Sort the array in increasing order ; Traverse the array , nums [ ] ; Store the required number to be found ; Perform binary search ; Store the mid value ; If nums [ mid ] is greater than x , then update high to mid - 1 ; If nums [ mid ] is less than x , then update low to mid + 1 ; Otherwise ; If mid is equal i , check mid - 1 and mid + 1 ; Otherwise , print the pair and return ; If no such pair is found , then print - 1 ; Driver Code ; Function Call
function hasArrayTwoPairs ( nums , n , target ) { nums . sort ( ) ; var i ; for ( i = 0 ; i < n ; i ++ ) { var x = target - nums [ i ] ; var low = 0 , high = n - 1 ; while ( low <= high ) { var mid = low + ( Math . floor ( ( high - low ) / 2 ) ) ; if ( nums [ mid ] > x ) { high = mid - 1 ; } else if ( nums [ mid ] < x ) { low = mid + 1 ; } else { if ( mid == i ) { if ( ( mid - 1 >= 0 ) && nums [ mid - 1 ] == x ) { document . write ( nums [ i ] + " " ) ; document . write ( nums [ mid - 1 ] ) ; return ; } if ( ( mid + 1 < n ) && nums [ mid + 1 ] == x ) { document . write ( nums [ i ] + " " ) ; document . write ( nums [ mid + 1 ] ) ; return ; } break ; } else { document . write ( nums [ i ] + " " ) ; document . write ( nums [ mid ] ) ; return ; } } } } document . write ( - 1 ) ; } var A = [ 0 , - 1 , 2 , - 3 , 1 ] ; var X = - 2 ; var N = A . length ; hasArrayTwoPairs ( A , N , X ) ;
Smallest divisor of N closest to X | Function to find the divisor of N closest to the target ; Iterate till square root of N ; Check if divisors are equal ; Check if i is the closest ; Check if i is the closest ; Check if n / i is the closest ; Print the closest value ; Given N & X ; Function Call
function findClosest ( N , target ) { let closest = - 1 ; let diff = Number . MAX_VALUE ; for ( let i = 1 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( N / i == i ) { if ( Math . abs ( target - i ) < diff ) { diff = Math . abs ( target - i ) ; closest = i ; } } else { if ( Math . abs ( target - i ) < diff ) { diff = Math . abs ( target - i ) ; closest = i ; } if ( Math . abs ( target - N / i ) < diff ) { diff = Math . abs ( target - N / i ) ; closest = N / i ; } } } } document . write ( closest ) ; } let N = 16 , X = 5 ; findClosest ( N , X ) ;
Find a pair ( a , b ) such that Aa + Bb = N | Function to calculate the minimum power of A and B greater than N ; Stores the power of A which is greater than N ; Increment count by 1 ; Divide N by A ; Function to find a pair ( a , b ) such that A ^ a + B ^ b = N ; Calculate the minimum power of A greater than N ; Calculate the minimum power of B greater than N ; Make copy of A and B ; Traverse for every pair ( i , j ) ; Check if B ^ j + A ^ i = N To overcome the overflow problem use B = N - A rather than B + A = N ; Increment power B by 1 ; Increment power A by 1 ; Finally print - 1 if no pair is found ; Given A , B and N ; Function Call
function power ( A , N ) { let count = 0 ; if ( A == 1 ) return 0 ; while ( N > 0 ) { count ++ ; N /= A ; } return count ; } function Pairs ( N , A , B ) { let powerA , powerB ; powerA = power ( A , N ) ; powerB = power ( B , N ) ; let letialB = B , letialA = A ; A = 1 ; for ( let i = 0 ; i <= powerA ; i ++ ) { B = 1 ; for ( let j = 0 ; j <= powerB ; j ++ ) { if ( B == N - A ) { document . write ( i + " " + j ) ; return ; } B *= letialB ; } A *= letialA ; } document . write ( " " ) ; return ; } let N = 106 , A = 3 , B = 5 ; Pairs ( N , A , B ) ;
Count numbers from a given range that are not divisible by any of the array elements | Function to find the non - multiples till k ; Stores all unique multiples ; Iterate the array ; For finding duplicates only once ; Inserting all multiples into the set ; Returning only the count of numbers that are not divisible by any of the array elements ; Function to count the total values in the range [ L , R ] ; Count all values in the range using exclusion principle ; Driver Code ; Function Call
function findNonMultiples ( arr , n , k ) { let multiples = new Set ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( ! multiples . has ( arr [ i ] ) ) { for ( let j = 1 ; j <= k / arr [ i ] ; j ++ ) { multiples . add ( arr [ i ] * j ) ; } } } return k - multiples . size ; } function countValues ( arr , N , L , R ) { return findNonMultiples ( arr , N , R ) - findNonMultiples ( arr , N , L - 1 ) ; } let arr = [ 2 , 3 , 4 , 5 , 6 ] ; let N = arr . length ; let L = 1 , R = 20 ; document . write ( countValues ( arr , N , L , R ) ) ;
Minimum number of coins to be collected per hour to empty N piles in at most H hours | Function to find the minimum number of coins to be collected per hour to empty N piles in H hours ; Stores the minimum coins to be removed per hour ; Find the maximum array element ; Perform Binary Search ; Store the mid value of the range in K ; Find the total time taken to empty N piles by removing K coins per hour ; If total time does not exceed H ; Otherwise ; Print the required result ; Driver Code ; Function Call
function minCollectingSpeed ( piles , H ) { var ans = - 1 ; var low = 1 , high ; high = piles . reduce ( ( a , b ) => Math . max ( a , b ) ) ; while ( low <= high ) { var K = low + parseInt ( ( high - low ) / 2 ) ; var time = 0 ; piles . forEach ( ai => { time += parseInt ( ( ai + K - 1 ) / K ) ; } ) ; if ( time <= H ) { ans = K ; high = K - 1 ; } else { low = K + 1 ; } } document . write ( ans ) ; } var arr = [ 3 , 6 , 7 , 11 ] ; var H = 8 ; minCollectingSpeed ( arr , H ) ;
Count distinct pairs with given sum | Function to count distinct pairs in array whose sum equal to K ; Stores count of distinct pairs whose sum equal to K ; Sort the array ; Stores index of the left pointer ; Stores index of the right pointer ; Calculate count of distinct pairs whose sum equal to K ; If sum of current pair is equal to K ; Remove consecutive duplicate array elements ; Update i ; Remove consecutive duplicate array elements ; Update j ; Update cntPairs ; Update i ; Update j ; if sum of current pair less than K ; Update i ; Update j ; Driver Code
function cntDisPairs ( arr , N , K ) { var cntPairs = 0 ; arr . sort ( ) ; var i = 0 ; var j = N - 1 ; while ( i < j ) { if ( arr [ i ] + arr [ j ] == K ) { while ( i < j && arr [ i ] == arr [ i + 1 ] ) { i ++ ; } while ( i < j && arr [ j ] == arr [ j - 1 ] ) { j -- ; } cntPairs += 1 ; i ++ ; j -- ; } else if ( arr [ i ] + arr [ j ] < K ) { i ++ ; } else { j -- ; } } return cntPairs ; } var arr = [ 5 , 6 , 5 , 7 , 7 , 8 ] ; var N = arr . length ; var K = 13 ; document . write ( cntDisPairs ( arr , N , K ) ) ;
Queries to find longest subsequence having no similar adjacent elements with updates | Function to find the length of the longest subsequence such that no two adjacent elements are equal ; Replace element at index x with y ; Since x is 1 - indexed , decrement x by 1 ; Keep track of number of elements in subsequence ; If previous element is not same as current element ; Print the desired count ; Driver code ; Function Call
function longestSubsequence ( N , Q , arr , Queries ) { for ( let i = 0 ; i < Q ; i ++ ) { let x = Queries [ i ] [ 0 ] ; let y = Queries [ i ] [ 1 ] ; arr [ x - 1 ] = y ; let count = 1 ; for ( let j = 1 ; j < N ; j ++ ) { if ( arr [ j ] != arr [ j - 1 ] ) { count += 1 ; } } document . write ( count + " " ) ; } } let arr = [ 1 , 1 , 2 , 5 , 2 ] ; let N = arr . length ; let Q = 2 ; let Queries = [ [ 1 , 3 ] , [ 4 , 2 ] ] ; longestSubsequence ( N , Q , arr , Queries ) ;
Queries to find longest subsequence having no similar adjacent elements with updates | javascript program for the above approach ; Traverse the array arr ; If previous element is not same as current element ; Traverse the queries ; Replace element at index x with y ; Recalculate for index x ; Subtract contribution of element at index x ; Add contribution of y ; Recalculate for index x + 1 ; Subtract contribution of element at index x + 1 ; Adds contribution of y ; Replace the element ; Driver Code ; Function Call
function longestSubsequence ( N , Q , arr , Queries ) { var count = 1 ; for ( var i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) { count += 1 ; } } for ( var i = 0 ; i < Q ; i ++ ) { var x = Queries [ i ] [ 0 ] ; var y = Queries [ i ] [ 1 ] ; if ( x > 1 ) { if ( arr [ x - 1 ] != arr [ x - 2 ] ) { count -= 1 ; } if ( arr [ x - 2 ] != y ) { count += 1 ; } } if ( x < N ) { if ( arr [ x ] != arr [ x - 1 ] ) { count -= 1 ; } if ( y != arr [ x ] ) { count += 1 ; } } document . write ( count + " " ) ; arr [ x - 1 ] = y ; } } var arr = [ 1 , 1 , 2 , 5 , 2 ] ; var N = arr . length ; var Q = 2 ; var Queries = [ [ 1 , 3 ] , [ 4 , 2 ] ] ; longestSubsequence ( N , Q , arr , Queries ) ;
Sum of absolute differences of indices of occurrences of each array element | Function to find sum of differences of indices of occurrences of each unique array element ; Stores indices of each array element ; Store the indices ; Stores the sums ; Traverse the array ; Find sum for each element ; Iterate over the Map ; Calculate sum of occurrences of arr [ i ] ; Store sum for current element ; Print answer for each element ; Given array ; Given size ; Function call
function sum ( arr , n ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { var tmp = mp . get ( arr [ i ] ) ; tmp . push ( i ) ; mp . set ( arr [ i ] , tmp ) ; } else { mp . set ( arr [ i ] , [ i ] ) ; } } var ans = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { var sum = 0 ; mp . get ( arr [ i ] ) . forEach ( it => { sum += Math . abs ( it - i ) ; } ) ; ans [ i ] = sum ; } for ( var i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } return ; } var arr = [ 1 , 3 , 1 , 1 , 2 ] ; var n = arr . length ; sum ( arr , n ) ;
Convert vowels into upper case character in a given string | Function to convert vowels into uppercase ; Stores the length of str ; Driver Code
function conVowUpp ( str ) { var N = str . length ; for ( var i = 0 ; i < N ; i ++ ) { if ( str [ i ] === " " str [ i ] === " " str [ i ] === " " str [ i ] === " " str [ i ] === " " ) { document . write ( str [ i ] . toUpperCase ( ) ) ; } else { document . write ( str [ i ] ) ; } } } var str = " " ; conVowUpp ( str ) ;
Maximize number of days for which P chocolates can be distributed consecutively to N people | Stores the frequency of each type of chocolate ; Function to check if chocolates can be eaten for ' mid ' no . of days ; If cnt exceeds N , return true ; Function to find the maximum number of days for which chocolates can be eaten ; Store the frequency of each type of chocolate ; Initialize start and end with 0 and P respectively ; Calculate mid ; Check if chocolates can be distributed for mid days ; Check if chocolates can be distributed for more than mid consecutive days ; Driver code ; Function call
var mp = new Map ( ) ; var N , P ; function helper ( mid ) { var cnt = 0 ; mp . forEach ( ( value , ) => { var temp = value ; while ( temp >= mid ) { temp -= mid ; cnt ++ ; } } ) ; return cnt >= N ; } function findMaximumDays ( arr ) { for ( var i = 0 ; i < P ; i ++ ) { if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) else mp . set ( arr [ i ] , 1 ) ; } var start = 0 , end = P , ans = 0 ; while ( start <= end ) { var mid = start + parseInt ( ( end - start ) / 2 ) ; if ( mid != 0 && helper ( mid ) ) { ans = mid ; start = mid + 1 ; } else if ( mid == 0 ) { start = mid + 1 ; } else { end = mid - 1 ; } } return ans ; } N = 3 , P = 10 ; var arr = [ 1 , 2 , 2 , 1 , 1 , 3 , 3 , 3 , 2 , 4 ] ; document . write ( findMaximumDays ( arr ) ) ;
Count subarrays having sum modulo K same as the length of the subarray | Function that counts the subarrays having sum modulo k equal to the length of subarray ; Stores the count of subarrays ; Stores prefix sum of the array ; Calculate prefix sum array ; Generate all the subarrays ; Check if this subarray is a valid subarray or not ; Total count of subarrays ; Given arr [ ] ; Size of the array ; Given K ; Function Call
function countSubarrays ( a , n , k ) { var ans = 0 ; var pref = [ ] ; pref . push ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) pref . push ( ( a [ i ] + pref [ i ] ) % k ) ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = i ; j <= n ; j ++ ) { if ( ( pref [ j ] - pref [ i - 1 ] + k ) % k == j - i + 1 ) { ans ++ ; } } } document . write ( ans + ' ' ) ; } var arr = [ 2 , 3 , 5 , 3 , 1 , 5 ] ; var N = arr . length ; var K = 4 ; countSubarrays ( arr , N , K ) ;