codeparrot/xlcost-text-to-code huggingface dataset:
Print Longest Bitonic subsequence ( Space Optimized Approach ) | Function to print the longest bitonic subsequence ; Function to generate the longest bitonic subsequence ; Store the lengths of LIS ending at every index ; Store the lengths of LDS ending at every index ; Compute LIS for all indices ; Compute LDS for all indices ; Find the index having maximum value of lis [ i ] + lds [ i ] - 1 ; Stores the count of elements in increasing order in Bitonic subsequence ; Store the increasing subsequence ; Sort the bitonic subsequence to arrange smaller elements at the beginning ; Stores the count of elements in decreasing order in Bitonic subsequence ; Print the longest bitonic sequence ; Driver Code
function printRes ( res ) { var n = res . length ; for ( var i = 0 ; i < n ; i ++ ) { document . write ( res [ i ] + " " ) ; } } function printLBS ( arr , N ) { var lis = Array ( N ) ; var lds = Array ( N ) ; for ( var i = 0 ; i < N ; i ++ ) { lis [ i ] = lds [ i ] = 1 ; } for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { if ( lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } } } for ( var i = N - 1 ; i >= 0 ; i -- ) { for ( var j = N - 1 ; j > i ; j -- ) { if ( arr [ j ] < arr [ i ] ) { if ( lds [ i ] < lds [ j ] + 1 ) lds [ i ] = lds [ j ] + 1 ; } } } var MaxVal = arr [ 0 ] , inx = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( MaxVal < lis [ i ] + lds [ i ] - 1 ) { MaxVal = lis [ i ] + lds [ i ] - 1 ; inx = i ; } } var ct1 = lis [ inx ] ; var res = [ ] ; for ( var i = inx ; i >= 0 && ct1 > 0 ; i -- ) { if ( lis [ i ] == ct1 ) { res . push ( arr [ i ] ) ; ct1 -- ; } } res . reverse ( ) ; var ct2 = lds [ inx ] - 1 ; for ( var i = inx ; i < N && ct2 > 0 ; i ++ ) { if ( lds [ i ] == ct2 ) { res . push ( arr [ i ] ) ; ct2 -- ; } } printRes ( res ) ; } var arr = [ 80 , 60 , 30 , 40 , 20 , 10 ] ; var N = arr . length ; printLBS ( arr , N ) ;
Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible | Function to check if the array can be partitioned into atmost d subarray with sum atmost T ; Initial partition ; Current sum ; If current sum exceeds T ; Create a new partition ; If count of partitions exceed d ; Function to find the minimum possible value of T ; Stores the maximum and total sum of elements ; Maximum element ; Sum of all elements ; Calculate median T ; If atmost D partitions possible ; Check for smaller T ; Otherwise ; Check for larger T ; Print the minimum T required ; Driver Code ; Function call
function possible ( T , arr , n , d ) { let partition = 1 ; let total = 0 ; for ( let i = 0 ; i < n ; i ++ ) { total = total + arr [ i ] ; if ( total > T ) { partition = partition + 1 ; total = arr [ i ] ; if ( partition > d ) { return false ; } } } return true ; } function calcT ( n , d , arr ) { let mx = - 1 , sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { mx = Math . max ( mx , arr [ i ] ) ; sum = sum + arr [ i ] ; } let lb = mx ; let ub = sum ; while ( lb < ub ) { let T_mid = lb + ( ub - lb ) / 2 ; if ( possible ( T_mid , arr , n , d ) == true ) { ub = T_mid ; } else { lb = T_mid + 1 ; } } document . write ( lb ) ; } let d = 2 ; let arr = [ 1 , 1 , 1 , 1 , 1 ] ; let n = arr . length ; calcT ( n , d , arr ) ;
Minimize splits to generate monotonous Substrings from given String | Function to return final result ; Initialize variable to keep track of ongoing sequence ; If current sequence is neither increasing nor decreasing ; If prev char is greater ; If prev char is same ; Otherwise ; If current sequence is Non - Decreasing ; If prev char is smaller ; If prev char is same ; Update ongoing ; Otherwise ; Update ongoing ; Increase count ; If current sequence is Non - Increasing ; If prev char is greater , then update ongoing with D ; If prev char is equal , then update current with D ; Otherwise , update ongoing with N and increment count ; Return count + 1 ; Driver Code
function minReqSubstring ( s , n ) { let ongoing = ' ' ; let count = 0 , l = s . length ; for ( let i = 1 ; i < l ; i ++ ) { if ( ongoing == ' ' ) { if ( s [ i ] < s [ i - 1 ] ) { ongoing = ' ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' ' ; } else { ongoing = ' ' ; } } else if ( ongoing == ' ' ) { if ( s [ i ] > s [ i - 1 ] ) { ongoing = ' ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' ' ; } else { ongoing = ' ' ; count += 1 ; } } else { if ( s [ i ] < s [ i - 1 ] ) { ongoing = ' ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' ' ; } else { ongoing = ' ' ; count += 1 ; } } } return count + 1 ; } let S = " " ; let n = S . length ; document . write ( minReqSubstring ( S , n ) ) ;
Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K | Function to calculate the minimum number of operations required ; Stores the total number of operations ; Iterate over the array ; If the sum of pair of adjacent elements exceed k . ; If current element exceeds k ; Reduce arr [ i ] to k ; Update arr [ i + 1 ] accordingly ; Update answer ; Driver Code
function minimum_required_operations ( arr , n , k ) { let answer = 0 ; let mod = 1000000007 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + arr [ i + 1 ] > k ) { if ( arr [ i ] > k ) { answer += ( arr [ i ] - k ) ; arr [ i ] = k ; } answer += ( arr [ i ] + arr [ i + 1 ] ) - k ; arr [ i + 1 ] = ( k - arr [ i ] ) ; answer %= mod ; } } return answer ; } let a = [ 9 , 50 , 4 , 14 , 42 , 89 ] ; let k = 10 ; let n = a . length ; document . write ( minimum_required_operations ( a , n , k ) ) ;
Lexicographic smallest permutation of a String containing the second String as a Substring | Function to prlet the desired lexicographic smaller String ; Calculate length of the String ; Stores the frequencies of characters of String str1 ; Stores the frequencies of characters of String str2 ; Decrease the frequency of second String from that of of the first String ; To store the resultant String ; To find the index of first character of the String str2 ; Append the characters in lexicographical order ; Append all the current character ( i + ' a ' ) ; If we reach first character of String str2 append str2 ; Return the resultant String ; Driver code
function findSmallestString ( str1 , str2 ) { let freq1 = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; let freq2 = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; let n1 = str1 . length ; let n2 = str2 . length ; for ( let i = 0 ; i < n1 ; ++ i ) { freq1 [ str1 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; } for ( let i = 0 ; i < n2 ; ++ i ) { freq2 [ str2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; } for ( let i = 0 ; i < 26 ; ++ i ) { freq1 [ i ] -= freq2 [ i ] ; } let res = " " ; let minIndex = str2 [ 0 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; for ( let i = 0 ; i < 26 ; ++ i ) { for ( let j = 0 ; j < freq1 [ i ] ; ++ j ) { res += String . fromCharCode ( i + ' ' . charCodeAt ( ) ) ; } if ( i == minIndex ) { res += str2 ; } } return res ; } let str1 = " " ; let str2 = " " ; document . write ( findSmallestString ( str1 , str2 ) ) ;
Determine the winner of a game of deleting Characters from a String | Function to find the winner of the game when both players play optimally ; Stores the frequency of all digit ; Stores the scores of player1 and player2 respectively ; Iterate to store frequencies ; Turn for the player1 ; Add score of player1 ; Add score of player2 ; Check if its a draw ; If score of player 1 is greater ; Otherwise ; Driver code
function determineWinner ( str ) { let A = Array . from ( { length : 10 } , ( _ , i ) => 0 ) ; let sum1 = 0 , sum2 = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { A [ str [ i ] . charCodeAt ( ) - 48 ] ++ ; } for ( let i = 0 ; i <= 9 ; i ++ ) { if ( i % 2 != 0 ) { sum1 = sum1 + A [ i ] ; } else { sum2 = sum2 + A [ i ] ; } } if ( sum1 == sum2 ) { document . write ( " " ) ; } else if ( sum1 > sum2 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let str = " " ; determineWinner ( str ) ;
Generate a String from given Strings P and Q based on the given conditions | Function to generate a string S from string P and Q according to the given conditions ; Stores the frequencies ; Counts occurrences of each characters ; Reduce the count of the character which is also present in Q ; Stores the resultant string ; Index in [ ] freq to segregate the string ; Add Q to the resulting string ; Driver Code ; Function call
function manipulateStrings ( P , Q ) { var freq = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < P . length ; i ++ ) { freq [ P [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < Q . length ; i ++ ) { freq [ Q [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] -- ; } var sb = " " ; var pos = Q [ 0 ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; for ( var i = 0 ; i <= pos ; i ++ ) { while ( freq [ i ] > 0 ) { var c = String . fromCharCode ( " " . charCodeAt ( 0 ) + i ) ; sb += c ; freq [ i ] -- ; } } sb += Q ; for ( var i = pos + 1 ; i < 26 ; i ++ ) { while ( freq [ i ] > 0 ) { var c = String . fromCharCode ( " " . charCodeAt ( 0 ) + i ) ; sb += c ; freq [ i ] -- ; } } document . write ( sb ) ; } var P = " " ; var Q = " " ; manipulateStrings ( P , Q ) ;
Longest Subarrays having each Array element as the maximum | Function to find the maximum length of Subarrays for each element of the array having it as the maximum ; Initialize the bounds ; Iterate to find greater element on the left ; If greater element is found ; Decrement left pointer ; If boundary is exceeded ; Iterate to find greater element on the right ; If greater element is found ; Increment right pointer ; If boundary is exceeded ; Length of longest subarray where arr [ i ] is the largest ; Print the answer ; Driver Code
function solve ( n , arr ) { let i , ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { let left = Math . max ( i - 1 , 0 ) ; let right = Math . min ( n - 1 , i + 1 ) ; while ( left >= 0 ) { if ( arr [ left ] > arr [ i ] ) { left ++ ; break ; } left -- ; } if ( left < 0 ) left ++ ; while ( right < n ) { if ( arr [ right ] > arr [ i ] ) { right -- ; break ; } right ++ ; } if ( right >= n ) right -- ; ans = 1 + right - left ; document . write ( ans + " " ) ; } } let arr = [ 4 , 2 , 1 ] ; let n = arr . length ; solve ( n , arr ) ;
Count of Substrings having Sum equal to their Length | Function to count the number of substrings with sum equal to length ; Stores the count of substrings ; Add character to sum ; Add count of substrings to result ; Increase count of subarrays ; Return count ; Driver Code
function countSubstrings ( s , n ) { var count = 0 , sum = 0 ; var mp = new Map ( ) ; if ( mp . has ( 0 ) ) mp . set ( 0 , mp . get ( 0 ) + 1 ) else mp . set ( 0 , 1 ) ; for ( var i = 0 ; i < n ; ++ i ) { sum += ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; if ( mp . has ( sum - ( i + 1 ) ) ) count += mp . get ( sum - ( i + 1 ) ) ; if ( mp . has ( sum - ( i + 1 ) ) ) mp . set ( sum - ( i + 1 ) , mp . get ( sum - ( i + 1 ) ) + 1 ) else mp . set ( sum - ( i + 1 ) , 1 ) } return count ; } var str = " " ; var n = str . length ; document . write ( countSubstrings ( str , n ) ) ;
Count of ways to split an Array into three contiguous Subarrays having increasing Sum | Function to count the number of ways to split array into three contiguous subarrays of the required type ; Stores the prefix sums ; Stores the suffix sums ; Traverse the given array ; Updating curr_subarray_sum until it is less than prefix_sum [ s - 1 ] ; Increase count ; Decrease curr_subarray_sum by arr [ s [ ] ; Return count ; Driver Code
function findCount ( arr , n ) { let prefix_sum = Array . from ( { length : n } , ( _ , i ) => 0 ) ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; let suffix_sum = Array . from ( { length : n } , ( _ , i ) => 0 ) ; suffix_sum [ n - 1 ] = arr [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) suffix_sum [ i ] = suffix_sum [ i + 1 ] + arr [ i ] ; let s = 1 , e = 1 ; let curr_subarray_sum = 0 , count = 0 ; while ( s < n - 1 && e < n - 1 ) { while ( e < n - 1 && curr_subarray_sum < prefix_sum [ s - 1 ] ) { curr_subarray_sum += arr [ e ++ ] ; } if ( curr_subarray_sum <= suffix_sum [ e ] ) { count ++ ; } curr_subarray_sum -= arr [ s ++ ] ; } return count ; } let arr = [ 2 , 3 , 1 , 7 ] ; let n = arr . length ; document . write ( findCount ( arr , n ) ) ;
Minimum number of operations required to maximize the Binary String | Function to find the number of operations required ; Swap 0 ' s ▁ and ▁ 1' s ; Return the answer ; Driver Code
function minOperation ( s , n ) { let ans = 0 ; let i = 0 , j = n - 1 ; while ( i < j ) { if ( s [ i ] == ' ' && s [ j ] == ' ' ) { ans ++ ; i ++ ; j -- ; continue ; } if ( s [ i ] == ' ' ) { i ++ ; } if ( s [ j ] == ' ' ) { j -- ; } } return ans ; } let n = 8 ; let s = " " ; document . write ( minOperation ( s , n ) ) ;
Count of N digit Numbers having no pair of equal consecutive Digits | Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Calculate the total count of valid ( i - 1 ) - digit numbers ; Update dp [ ] [ ] table ; Calculate the count of required N - digit numbers ; Driver Code
function count ( N ) { if ( N == 1 ) { document . write ( ( 10 ) + " " ) ; return ; } var dp = Array . from ( Array ( N ) , ( ) => Array ( 10 ) . fill ( 0 ) ) ; for ( var i = 1 ; i < 10 ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( var i = 1 ; i < N ; i ++ ) { var temp = 0 ; for ( var j = 0 ; j < 10 ; j ++ ) temp += dp [ i - 1 ] [ j ] ; for ( var j = 0 ; j < 10 ; j ++ ) dp [ i ] [ j ] = temp - dp [ i - 1 ] [ j ] ; } var ans = 0 ; for ( var i = 0 ; i < 10 ; i ++ ) ans += dp [ N - 1 ] [ i ] ; document . write ( ans ) ; } var N = 2 ; count ( N ) ;
Count of N digit Numbers having no pair of equal consecutive Digits | Iterative Function to calculate ( x ^ y ) % mod in O ( log y ) ; Initialize result ; Update x if x >= mod ; If x is divisible by mod ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Driver Code
function power ( x , y , mod ) { let res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } function count ( N ) { if ( N == 1 ) { document . write ( 10 ) ; return ; } document . write ( power ( 9 , N , 1000000007 ) ) ; } let N = 3 ; count ( N ) ;
Check if an Array can be Sorted by picking only the corner Array elements | Function to check if an array can be sorted using given operations ; If sequence becomes increasing after an already non - decreasing to non - increasing pattern ; If a decreasing pattern is observed ; Driver Code
function check ( arr , n ) { var i , g ; g = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 0 && g == 1 ) return false ; if ( arr [ i ] - arr [ i - 1 ] < 0 ) g = 1 ; } return true ; } var arr = [ 2 , 3 , 4 , 10 , 4 , 3 , 1 ] ; var n = arr . length ; if ( check ( arr , n ) == true ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Queries to find the count of connected Non | Count of connected cells ; Function to return the representative of the Set to which x belongs ; If x is parent of itself ; x is representative of the Set ; Otherwise ; Path Compression ; Unites the set that includes x and the set that includes y ; Find the representatives ( or the root nodes ) for x an y ; If both are in the same set ; Decrement count ; If x ' s ▁ rank ▁ is ▁ less ▁ than ▁ y ' s rank ; Otherwise ; Then move x under y ( doesn 't matter which one goes where) ; And increment the result tree 's  rank by 1 ; Function to count the number of connected cells in the matrix ; Store result for queries ; Store representative of each element ; Initially , all elements are in their own set ; Stores the rank ( depth ) of each node ; If the grid [ x * m + y ] is already set , store the result ; Set grid [ x * m + y ] to 1 ; Increment count . ; Check for all adjacent cells to do a Union with neighbour 's  set if neighbour is also 1 ; Store result . ; Driver Code
let ctr = 0 ; function find ( parent , x ) { if ( parent [ x ] == x ) return x ; parent [ x ] = find ( parent , parent [ x ] ) ; return parent [ x ] ; } function setUnion ( parent , rank , x , y ) { let parentx = find ( parent , x ) ; let parenty = find ( parent , y ) ; if ( parenty == parentx ) return ; ctr -- ; if ( rank [ parentx ] < rank [ parenty ] ) { parent [ parentx ] = parenty ; } else if ( rank [ parentx ] > rank [ parenty ] ) { parent [ parenty ] = parentx ; } else { parent [ parentx ] = parenty ; rank [ parenty ] ++ ; } } function solve ( n , m , query ) { let result = new Array ( query . length ) ; let parent = new Array ( n * m ) ; for ( let i = 0 ; i < n * m ; i ++ ) parent [ i ] = i ; let rank = new Array ( n * m ) ; let grid = new Array ( n * m ) ; for ( let i = 0 ; i < ( n * m ) ; i ++ ) { rank [ i ] = 0 ; grid [ i ] = false ; } for ( let i = 0 ; i < query . length ; i ++ ) { let x = query [ i ] [ 0 ] ; let y = query [ i ] [ 1 ] ; if ( grid [ m * x + y ] == true ) { result [ i ] = ctr ; continue ; } grid [ m * x + y ] = true ; ctr ++ ; if ( x > 0 && grid [ m * ( x - 1 ) + y ] == true ) setUnion ( parent , rank , m * x + y , m * ( x - 1 ) + y ) ; if ( y > 0 && grid [ m * ( x ) + y - 1 ] == true ) setUnion ( parent , rank , m * x + y , m * ( x ) + y - 1 ) ; if ( x < n - 1 && grid [ m * ( x + 1 ) + y ] == true ) setUnion ( parent , rank , m * x + y , m * ( x + 1 ) + y ) ; if ( y < m - 1 && grid [ m * ( x ) + y + 1 ] == true ) setUnion ( parent , rank , m * x + y , m * ( x ) + y + 1 ) ; result [ i ] = ctr ; } return result ; } let N = 3 , M = 3 , K = 4 ; let query = [ [ 0 , 0 ] , [ 1 , 1 ] , [ 1 , 0 ] , [ 1 , 2 ] ] ; let result = solve ( N , M , query ) ; for ( let i = 0 ; i < K ; i ++ ) document . write ( result [ i ] + " " ) ;
Minimum Sum of a pair at least K distance apart from an Array | Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Find the suffix array ; Iterate in the array ; Update minimum sum ; Prlet the answer ; Driver Code
function findMinSum ( A , K ) { let n = A . length ; let suffix_min = Array . from ( { length : n } , ( _ , i ) => 0 ) ; suffix_min [ n - 1 ] = A [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) suffix_min [ i ] = Math . min ( suffix_min [ i + 1 ] , A [ i ] ) ; let min_sum = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( i + K < n ) min_sum = Math . min ( min_sum , A [ i ] + suffix_min [ i + K ] ) ; } document . write ( min_sum ) ; } let A = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let K = 2 ; findMinSum ( A , K ) ;
Maximum number of envelopes that can be put inside other bigger envelopes | Function that returns the maximum number of envelopes that can be inserted into another envelopes ; Number of envelopes ; Sort the envelopes in non - decreasing order ; Initialize dp [ ] array ; To store the result ; Loop through the array ; Find envelopes count for each envelope ; Store maximum envelopes count ; Return the result ; Given the envelopes ; Function Call
function maxEnvelopes ( envelopes ) { var N = envelopes . length ; if ( N == 0 ) return N ; envelopes . sort ( ) ; var dp = Array ( N ) ; var max_envelope = 1 ; dp [ 0 ] = 1 ; for ( var i = 1 ; i < N ; ++ i ) { dp [ i ] = 1 ; for ( var j = 0 ; j < i ; ++ j ) { if ( envelopes [ i ] [ 0 ] > envelopes [ j ] [ 0 ] && envelopes [ i ] [ 1 ] > envelopes [ j ] [ 1 ] && dp [ i ] < dp [ j ] + 1 ) dp [ i ] = dp [ j ] + 1 ; } max_envelope = Math . max ( max_envelope , dp [ i ] ) ; } return max_envelope ; } var envelopes = [ [ 4 , 3 ] , [ 5 , 3 ] , [ 5 , 6 ] , [ 1 , 2 ] ] ; document . write ( maxEnvelopes ( envelopes ) ) ;
Check if each element of an Array is the Sum of any two elements of another Array | Function to check if each element of B [ ] can be formed by adding two elements of array A [ ] ; Store each element of B [ ] ; Traverse all possible pairs of array ; If A [ i ] + A [ j ] is present in the set ; Remove A [ i ] + A [ j ] from the set ; If set is empty ; Otherwise ; Driver Code
function checkPossible ( A , B , n ) { var values = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) { values . add ( B [ i ] ) ; } for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) { if ( values . has ( A [ i ] + A [ j ] ) ) { values . delete ( A [ i ] + A [ j ] ) ; if ( values . size == 0 ) break ; } } } if ( values . size == 0 ) return " " ; else return " " ; } var N = 5 ; var A = [ 3 , 5 , 1 , 4 , 2 ] ; var B = [ 3 , 4 , 5 , 6 , 7 ] ; document . write ( checkPossible ( A , B , N ) ) ;
Count of Array elements greater than or equal to twice the Median of K trailing Array elements | Javascript Program to implement the above approach ; Function to find the count of array elements >= twice the median of K trailing array elements ; Stores frequencies ; Stores the array elements ; Count the frequencies of the array elements ; Iterating from d to n - 1 index means ( d + 1 ) th element to nth element ; To check the median ; Iterate over the frequencies of the elements ; Add the frequencies ; Check if the low_median value is obtained or not , if yes then do not change as it will be minimum ; Check if the high_median value is obtained or not , if yes then do not change it as it will be maximum ; Store 2 * median of K trailing elements ; If the current >= 2 * median ; Decrease the frequency for ( k - 1 ) - th element ; Increase the frequency of the current element ; Print the count ; Driver Code
const N = 2e5 ; const V = 500 ; function solve ( n , d , input ) { let a = new Array ( N ) ; let cnt = new Array ( V + 1 ) ; for ( let i = 0 ; i < n ; ++ i ) a [ i ] = input [ i ] ; let answer = 0 ; for ( let i = 0 ; i < d ; ++ i ) cnt [ a [ i ] ] ++ ; for ( let i = d ; i <= n - 1 ; ++ i ) { let acc = 0 ; let low_median = - 1 , high_median = - 1 ; for ( let v = 0 ; v <= V ; ++ v ) { acc += cnt [ v ] ; if ( low_median == - 1 && acc >= parseInt ( Math . floor ( ( d + 1 ) / 2.0 ) ) ) low_median = v ; if ( high_median == - 1 && acc >= parseInt ( Math . ceil ( ( d + 1 ) / 2.0 ) ) ) high_median = v ; } let double_median = low_median + high_median ; if ( a [ i ] >= double_median ) answer ++ ; cnt [ a [ i - d ] ] -- ; cnt [ a [ i ] ] ++ ; } document . write ( answer ) ; } let input = [ 1 , 2 , 2 , 4 , 5 ] ; let n = input . length ; let k = 3 ; solve ( n , k , input ) ;
Smallest positive integer X satisfying the given equation | Function to find out the smallest positive integer for the equation ; Stores the minimum ; Iterate till K ; Check if n is divisible by i ; Return the answer ; Driver Code
function findMinSoln ( n , k ) { var minSoln = 1000000000 ; for ( var i = 1 ; i < k ; i ++ ) { if ( n % i == 0 ) minSoln = Math . min ( minSoln , ( n / i ) * k + i ) ; } return minSoln ; } var n = 4 , k = 6 ; document . write ( findMinSoln ( n , k ) ) ;