codeparrot/xlcost-text-to-code huggingface dataset:
Check if the sum of a subarray within a given range is a perfect square or not | Function to calculate the square root of the sum of a subarray in a given range ; Calculate the sum of array elements within a given range ; Finding the square root ; If a perfect square is found ; Reduce the search space if the value is greater than sum ; Reduce the search space if the value if smaller than sum ; Given Array ; Given range ; Function Call
function checkForPerfectSquare ( arr , i , j ) { let mid , sum = 0 ; for ( let m = i ; m <= j ; m ++ ) { sum += arr [ m ] ; } let low = 0 , high = parseInt ( sum / 2 ) ; while ( low <= high ) { mid = low + parseInt ( ( high - low ) / 2 ) ; if ( mid * mid == sum ) { return mid ; } else if ( mid * mid > sum ) { high = mid - 1 ; } else { low = mid + 1 ; } } return - 1 ; } let arr = [ 2 , 19 , 33 , 48 , 90 , 100 ] ; let L = 1 , R = 3 ; document . write ( checkForPerfectSquare ( arr , L , R ) ) ;
Count of subarrays forming an Arithmetic Progression ( AP ) | Function to find the total count of subarrays ; Iterate over each subarray ; Difference between first two terms of subarray ; Iterate over the subarray from i to j ; Check if the difference of all adjacent elements is same ; Given array ; Function Call
function calcSubarray ( A , N ) { let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { let flag = true ; let comm_diff = A [ i + 1 ] - A [ i ] ; for ( let k = i ; k < j ; k ++ ) { if ( A [ k + 1 ] - A [ k ] == comm_diff ) { continue ; } else { flag = false ; break ; } } if ( flag ) { count ++ ; } } } return count ; } let A = [ 8 , 7 , 4 , 1 , 0 ] ; let N = A . length ; document . write ( calcSubarray ( A , N ) ) ;
Largest Sum Contiguous Subarray having unique elements | Function to calculate required maximum subarray sum ; Initialize two pointers ; Stores the unique elements ; Insert the first element ; Current max sum ; Global maximum sum ; Update sum & increment j auto pos = s . find ( 3 ) ; ; Add the current element ; Update sum and increment i and remove arr [ i ] from set ; Remove the element from start position ; Return the maximum sum ; Given array arr [ ] ; Function Call ; Print the maximum sum
function maxSumSubarray ( arr , n ) { var i = 0 , j = 1 ; var set = new Set ( ) ; set . add ( arr [ 0 ] ) ; var sum = arr [ 0 ] ; var maxsum = sum ; while ( i < n - 1 && j < n ) { var is_in = set . has ( arr [ j ] ) ; if ( ! is_in ) { sum = sum + arr [ j ] ; maxsum = Math . max ( sum , maxsum ) ; set . add ( arr [ j ++ ] ) ; } else { sum -= arr [ i ] ; set . delete ( arr [ i ++ ] ) ; } } return maxsum ; } var arr = [ 1 , 2 , 3 , 1 , 5 ] ; var ans = maxSumSubarray ( arr , 5 ) ; document . write ( ans ) ;
Count of numbers having only one unset bit in a range [ L , R ] | Function to count numbers in the range [ l , r ] having exactly one unset bit ; Stores the required count ; Iterate over the range ; Calculate number of bits ; Calculate number of set bits ; If count of unset bits is 1 ; Increment answer ; Return the answer ; Driver Code ; Function Call
function count_numbers ( L , R ) { let ans = 0 ; for ( let n = L ; n <= R ; n ++ ) { let no_of_bits = Math . floor ( Math . log ( n ) + 1 ) ; let no_of_set_bits = bitCount ( n ) ; if ( no_of_bits - no_of_set_bits == 1 ) { ans ++ ; } } return ans ; } function bitCount ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } let L = 4 , R = 9 ; document . write ( count_numbers ( L , R ) ) ;
Count of numbers having only one unset bit in a range [ L , R ] | Function to count numbers in the range [ L , R ] having exactly one unset bit ; Stores the count elements having one zero in binary ; Stores the maximum number of bits needed to represent number ; Loop over for zero bit position ; Number having zero_bit as unset and remaining bits set ; Sets all bits before zero_bit ; Set the bit at position j ; Set the bit position at j ; If cur is in the range [ L , R ] , then increment ans ; Return ans ; Driver Code ; Function Call
function count_numbers ( L , R ) { let ans = 0 ; let LogR = ( Math . log ( R ) + 1 ) ; for ( let zero_bit = 0 ; zero_bit < LogR ; zero_bit ++ ) { let cur = 0 ; for ( let j = 0 ; j < zero_bit ; j ++ ) { cur |= ( 1 << j ) ; } for ( let j = zero_bit + 1 ; j < LogR ; j ++ ) { cur |= ( 1 << j ) ; if ( cur >= L && cur <= R ) { ans ++ ; } } } return ans ; } let L = 4 , R = 9 ; document . write ( count_numbers ( L , R ) ) ;
Sum of all distances between occurrences of same characters in a given string | Function to calculate the sum of distances between occurrences of same characters in a String ; If similar characters are found ; Add the difference of their positions ; Return the answer ; Driver Code
function findSum ( s ) { let sum = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { for ( let j = i + 1 ; j < s . length ; j ++ ) { if ( s [ i ] == s [ j ] ) { sum += ( j - i ) ; } } } return sum ; } let s = " " ; document . write ( findSum ( s ) + " " ) ;
Count array elements that can be represented as sum of at least two consecutive array elements | Function to find the number of array elements that can be represented as the sum of two or more consecutive array elements ; Stores the frequencies of array elements ; Stores required count ; Update frequency of each array element ; Find sum of all subarrays ; Increment ans by cnt [ sum ] ; Reset cnt [ sum ] by 0 ; Return ans ; Given array ; Function call
function countElements ( a , n ) { var cnt = Array ( n + 1 ) . fill ( 0 ) ; var ans = 0 ; for ( k = 0 ; k < n ; k ++ ) { cnt [ a [ k ] ] ++ ; } for ( l = 0 ; l < n ; ++ l ) { var sum = 0 ; for ( r = l ; r < n ; ++ r ) { sum += a [ r ] ; if ( l == r ) continue ; if ( sum <= n ) { ans += cnt [ sum ] ; cnt [ sum ] = 0 ; } } } return ans ; } var a = [ 1 , 1 , 1 , 1 , 1 ] ; document . write ( countElements ( a , a . length ) ) ;
Maximum GCD among all pairs ( i , j ) of first N natural numbers | Function to find maximum gcd of all pairs possible from first n natural numbers ; Stores maximum gcd ; Iterate over all possible pairs ; Update maximum GCD ; Driver Code
function maxGCD ( n ) { var maxHcf = Number . MIN_VALUE ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = i + 1 ; j <= n ; j ++ ) { maxHcf = Math . max ( maxHcf , __gcd ( i , j ) ) ; } } return maxHcf ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var n = 4 ; document . write ( maxGCD ( n ) ) ;
Maximum GCD among all pairs ( i , j ) of first N natural numbers | Function to find the maximum GCD among all the pairs from first n natural numbers ; Return max GCD ; Driver Code
function maxGCD ( n ) { return parseInt ( n / 2 ) ; } var n = 4 ; document . write ( maxGCD ( n ) ) ;
Modify given array by incrementing first occurrence of every element by K | Print the required final sequence ; Stores the array element - index pairs ; Stores the required sequence ; Insert all array elements ; If current element has not occurred previously ; Otherwise ; Iterator to the first index containing sol [ i ] ; Remove that occurrence ; Increment by K ; Insert the incremented element at that index ; Print the final sequence ; Driver Code
function printSequence ( A , n , k ) { var mp = new Map ( ) ; var sol = [ ] ; A . forEach ( x => { sol . push ( x ) ; } ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( ! mp . has ( sol [ i ] ) || mp . get ( sol [ i ] ) . size == 0 ) { var tmp = new Set ( ) ; tmp . add ( i ) mp . set ( sol [ i ] , tmp ) } else { var idxx = [ ... mp . get ( sol [ i ] ) ] . sort ( ( a , b ) => a - b ) [ 0 ] ; var idx = idxx ; var x = mp . get ( sol [ i ] ) ; x . delete ( idxx ) ; mp . set ( sol [ i ] , x ) ; sol [ idx ] += k ; if ( ! mp . has ( sol [ idx ] ) ) mp . set ( sol [ idx ] , new Set ( ) ) x = mp . get ( sol [ idx ] ) ; x . add ( idx ) ; mp . set ( sol [ idx ] , x ) ; x = mp . get ( sol [ i ] ) ; x . add ( i ) ; mp . set ( sol [ i ] , x ) ; } } sol . forEach ( x => { document . write ( x + " " ) ; } ) ; } var N = 5 ; var K = 6 ; var A = [ 1 , 4 , 1 , 1 , 4 ] ; printSequence ( A , N , K ) ;
Longest subarray forming a Geometic Progression ( GP ) | Function to return the length of the longest subarray forming a GP in a sorted array ; Base Case ; Stores the length of GP and the common ratio ; Stores the maximum length of the GP ; Traverse the array ; Check if the common ratio is valid for GP ; If the current common ratio is equal to previous common ratio ; Increment the length of the GP ; Store the max length of GP ; Otherwise ; Update the common ratio ; Update the length of GP ; Store the max length of GP ; Update the length of GP ; Store the max length of GP ; Return the max length of GP ; Given array ; Length of the array ; Function call
function longestGP ( A , N ) { if ( N < 2 ) return N ; let length = 1 , common_ratio = 1 ; let maxlength = 1 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i + 1 ] % A [ i ] == 0 ) { if ( A [ i + 1 ] / A [ i ] == common_ratio ) { length = length + 1 ; maxlength = Math . max ( maxlength , length ) ; } else { common_ratio = A [ i + 1 ] / A [ i ] ; length = 2 ; } } else { maxlength = Math . max ( maxlength , length ) ; length = 1 ; } } maxlength = Math . max ( maxlength , length ) ; return maxlength ; } let arr = [ 1 , 2 , 4 , 7 , 14 , 28 , 56 , 89 ] ; let N = arr . length ; document . write ( longestGP ( arr , N ) ) ;
Maximum number of uncrossed lines between two given arrays | Function to count maximum number of uncrossed lines between the two given arrays ; Stores the length of lcs obtained upto every index ; Iterate over first array ; Iterate over second array ; Update value in dp table ; If both characters are equal ; Update the length of lcs ; If both characters are not equal ; Update the table ; Return the answer ; Given array A [ ] and B [ ] ; Function call
function uncrossedLines ( a , b , n , m ) { let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { dp [ i ] = new Array ( m + 1 ) ; for ( let j = 0 ; j < ( m + 1 ) ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i <= n ; i ++ ) { for ( let j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) dp [ i ] [ j ] = 0 ; else if ( a [ i - 1 ] == b [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } return dp [ n ] [ m ] ; } let A = [ 3 , 9 , 2 ] ; let B = [ 3 , 2 , 9 ] ; let N = A . length ; let M = B . length ; document . write ( uncrossedLines ( A , B , N , M ) ) ;
Minimize flips required to make all shortest paths from top | Function to count the minimum number of flips required ; Dimensions of matrix ; Stores the count the flips ; Check if element is same or not ; Return the final count ; Given Matrix ; Given path as a string ; Function Call
function minFlips ( mat , s ) { let N = mat . length ; let M = mat [ 0 ] . length ; let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] != s [ ( i + j ) ] - ' ' ) { count ++ ; } } } return count ; } let mat = [ [ 1 , 0 , 1 ] , [ 0 , 1 , 1 ] , [ 0 , 0 , 0 ] ] ; let s = " " ; document . write ( minFlips ( mat , s ) ) ;
Generate a pair of integers from a range [ L , R ] whose LCM also lies within the range | javascript implementation of the above approach ; Checking if any pair is possible or not in range ( l , r ) ; If not possible print ( - 1 ) ; Prvar LCM pair ; Driver code ; Function call
function lcmpair ( l , r ) { var x , y ; x = l ; y = 2 * l ; if ( y > r ) { document . write ( " " ) ; } else { document . write ( " " + x + " " + y + " " ) ; } } var l = 13 , r = 69 ; lcmpair ( l , r ) ;
Count non | Function to reverse a number ; Store the reverse of N ; Return reverse of N ; Function to get the count of non - palindromic numbers having same first and last digit ; Store the required count ; Traverse the array ; Store reverse of arr [ i ] ; Check for palindrome ; IF non - palindromic ; Check if first and last digits are equal ; Driver Code
function revNum ( N ) { var x = 0 ; while ( N ) { x = x * 10 + ( N % 10 ) ; N = N / 10 ; } return x ; } function ctNonPalin ( arr , N ) { var Res = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var x = revNum ( arr [ i ] ) ; if ( x == arr [ i ] ) { continue ; } else { Res += arr [ i ] % 10 == N % 10 ; } } return Res ; } var arr = [ 121 , 134 , 2342 , 4514 ] ; var N = arr . length ; document . write ( ctNonPalin ( arr , N ) ) ;
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | Function to find the smallest subarray with sum greater than or equal target ; DP table to store the computed subproblems ; Initialize first column with 0 ; Initialize first row with 0 ; Check for invalid condition ; Fill up the dp table ; Print the minimum length ; Driver code
function minlt ( arr , target , n ) { var dp = Array . from ( Array ( arr . length + 1 ) , ( ) => Array ( target + 1 ) . fill ( - 1 ) ) ; for ( var i = 0 ; i < arr . length + 1 ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( var j = 0 ; j < target + 1 ; j ++ ) dp [ 0 ] [ j ] = 1000000000 ; for ( var i = 1 ; i <= arr . length ; i ++ ) { for ( var j = 1 ; j <= target ; j ++ ) { if ( arr [ i - 1 ] > j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } else { dp [ i ] [ j ] = Math . min ( dp [ i - 1 ] [ j ] , ( dp [ i ] [ j - arr [ i - 1 ] ] ) != 1000000000 ? ( dp [ i ] [ j - arr [ i - 1 ] ] + 1 ) : 1000000000 ) ; } } } if ( dp [ arr . length ] [ target ] == 1000000000 ) { return - 1 ; } else { return dp [ arr . length ] [ target ] ; } } var arr = [ 2 , 3 , 5 , 4 , 1 ] ; var target = 11 ; var n = arr . length ; document . write ( minlt ( arr , target , n ) ) ;
Minimum absolute difference of server loads | Function which returns the minimum difference of loads ; Compute the overall server load ; Stores the results of subproblems ; Fill the partition table in bottom up manner ; If i - th server is included ; If i - th server is excluded ; Server A load : total_sum - ans Server B load : ans Diff : abs ( total_sum - 2 * ans ) ; Driver Code ; Function call
function minServerLoads ( n , servers ) { var totalLoad = 0 ; servers . forEach ( i => { totalLoad += i ; } ) ; var requiredLoad = parseInt ( totalLoad / 2 ) ; var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( requiredLoad + 1 ) . fill ( 0 ) ) ; for ( var i = 1 ; i < n + 1 ; i ++ ) { for ( var j = 1 ; j < requiredLoad + 1 ; j ++ ) { if ( servers [ i - 1 ] > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; else dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , servers [ i - 1 ] + dp [ i - 1 ] [ j - servers [ i - 1 ] ] ) ; } } return totalLoad - 2 * dp [ n ] [ requiredLoad ] ; } var N = 5 ; var servers = [ 1 , 2 , 3 , 4 , 5 ] ; document . write ( minServerLoads ( N , servers ) ) ;
Length of longest subarray with positive product | Function to find the length of longest subarray whose product is positive ; Stores the length of current subarray with positive product ; Stores the length of current subarray with negative product ; Stores the length of the longest subarray with positive product ; Reset the value ; If current element is positive ; Increment the length of subarray with positive product ; If at least one element is present in the subarray with negative product ; Update res ; If current element is negative ; Increment the length of subarray with negative product ; If at least one element is present in the subarray with positive product ; Update res ; Driver Code
function maxLenSub ( arr , N ) { var Pos = 0 ; var Neg = 0 ; var res = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { Pos = Neg = 0 ; } else if ( arr [ i ] > 0 ) { Pos += 1 ; if ( Neg != 0 ) { Neg += 1 ; } res = Math . max ( res , Pos ) ; } else { [ Pos , Neg ] = [ Neg , Pos ] ; Neg += 1 ; if ( Pos != 0 ) { Pos += 1 ; } res = Math . max ( res , Pos ) ; } } return res ; } var arr = [ - 1 , - 2 , - 3 , 0 , 1 ] ; var N = arr . length ; document . write ( maxLenSub ( arr , N ) ) ;
Print all strings of maximum length from an array of strings | Function to find the length of the longest String from the given array of Strings ; Traverse the array ; Stores the length of current String ; Update maximum length ; Return the maximum length ; Function to print the longest Strings from the array ; Find the Strings having length equals to len ; Print the resultant vector of Strings ; Function to print all the longest Strings from the array ; Find the length of longest String ; Find and print all the Strings having length equals to max ; Driver Code
function maxLength ( arr ) { let len = Number . MIN_VALUE ; let N = arr . length ; for ( let i = 0 ; i < N ; i ++ ) { let l = arr [ i ] . length ; if ( len < l ) { len = l ; } } return len ; } function maxStrings ( arr , len ) { let N = arr . length ; let ans = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( len == arr [ i ] . length ) { ans . push ( arr [ i ] ) ; } } for ( let i = 0 ; i < ans . length ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } function printStrings ( arr ) { let max = maxLength ( arr ) ; maxStrings ( arr , max ) ; } let arr = [ " " , " " , " " , " " , " " ] ; printStrings ( arr ) ;
Minimize count of flips required such that no substring of 0 s have length exceeding K | Function to return minimum number of flips required ; Base Case ; Stores the count of minimum number of flips ; Stores the count of zeros in current subString ; If current character is 0 ; Continue ongoing subString ; Start a new subString ; If k consecutive zeroes are obtained ; End segment ; Return the result ; Driver Code ; Function call
function min_flips ( str , k ) { if ( str . length == 0 ) return 0 ; let ans = 0 ; let cnt_zeros = 0 ; for ( let ch in str . split ( ' ' ) ) { if ( str [ ch ] == ' ' ) { ++ cnt_zeros ; } else { cnt_zeros = 0 ; } if ( cnt_zeros == k ) { ++ ans ; cnt_zeros = 0 ; } } return ans ; } let str = " " ; let k = 3 ; document . write ( min_flips ( str , k ) ) ;
Find all possible subarrays having product less than or equal to K | Function to return all possible subarrays having product less than or equal to K ; Store the required subarrays ; Stores the product of current subarray ; Stores the starting index of the current subarray ; Check for empty array ; Iterate over the array ; Calculate product ; If product exceeds K ; Reduce product ; Increase starting index of current subarray ; Stores the subarray elements ; Store the subarray elements ; Add the subarrays to the list ; Return the final list of subarrays ; Driver Code
function maxSubArray ( arr , n , K ) { let solution = [ ] ; let multi = 1 ; let start = 0 ; if ( n <= 1 K < 0 ) { return solution ; } for ( let i = 0 ; i < n ; i ++ ) { multi = multi * arr [ i ] ; while ( multi > K ) { multi = Math . floor ( multi / arr [ start ] ) ; start ++ ; } let list = [ ] ; for ( let j = i ; j >= start ; j -- ) { list . unshift ( arr [ j ] ) ; solution . push ( list ) ; } } return solution ; } let arr = [ 2 , 7 , 1 , 4 ] ; let n = arr . length ; let K = 7 ; let v = maxSubArray ( arr , n , K ) ; document . write ( " " ) ; let first = true ; for ( let x = 0 ; x < v . length ; x ++ ) { if ( ! first ) { document . write ( " " ) ; } else { first = false ; } document . write ( " " ) ; let ff = true ; for ( let y = 0 ; y < v [ x ] . length ; y ++ ) { if ( ! ff ) { document . write ( " " ) ; } else { ff = false ; } document . write ( v [ x ] [ y ] ) ; } document . write ( " " ) ; } document . write ( " " ) ;
Count of collisions at a point ( X , Y ) | Function to find the count of possible pairs of collisions ; Stores the time at which points reach the origin ; Calculate time for each point ; Sort the times ; Counting total collisions ; Count of elements arriving at a given point at the same time ; Driver Code ; Given set of points with speed ; Function call
function solve ( D , N , X , Y ) { let T = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let x = D [ i ] [ 0 ] ; let y = D [ i ] [ 1 ] ; let speed = D [ i ] [ 2 ] ; let time = ( ( x * x - X * X ) + ( y * y - Y * Y ) ) / ( speed * speed ) ; T . push ( time ) ; } T . sort ( function ( a , b ) { return a - b ; } ) ; let i = 0 ; let total = 0 ; while ( i < ( T . length - 1 ) ) { let count = 1 ; while ( ( i < ( T . length - 1 ) ) && ( T [ i ] == T [ i + 1 ] ) ) { count += 1 ; i += 1 ; } total += ( count * ( count - 1 ) ) / 2 ; i += 1 ; } return total ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = 5 ; let D = [ [ 5 , 12 , 1 ] , [ 16 , 63 , 5 ] , [ - 10 , 24 , 2 ] , [ 7 , 24 , 2 ] , [ - 24 , 7 , 2 ] ] ; let X = 0 , Y = 0 ; document . write ( solve ( D , N , X , Y ) . toFixed ( 1 ) ) ;
Longest subarray forming an Arithmetic Progression ( AP ) | Function to return the length of longest subarray forming an AP ; Minimum possible length of required subarray is 2 ; Stores the length of the current subarray ; Stores the common difference of the current AP ; Stores the common difference of the previous AP ; If the common differences are found to be equal ; Continue the previous subarray ; Start a new subarray ; Update the length to store maximum length ; Update the length to store maximum length ; Return the length of the longest subarray ; Driver Code
function getMaxLength ( arr , N ) { let res = 2 ; let dist = 2 ; let curradj = ( arr [ 1 ] - arr [ 0 ] ) ; let prevadj = ( arr [ 1 ] - arr [ 0 ] ) ; for ( let i = 2 ; i < N ; i ++ ) { curradj = arr [ i ] - arr [ i - 1 ] ; if ( curradj == prevadj ) { dist ++ ; } else { prevadj = curradj ; res = Math . max ( res , dist ) ; dist = 2 ; } } res = Math . max ( res , dist ) ; return res ; } let arr = [ 10 , 7 , 4 , 6 , 8 , 10 , 11 ] ; let N = arr . length ; document . write ( getMaxLength ( arr , N ) ) ;
Minimize cost to empty a given string by removing characters alphabetically | Function to find the minimum cost required to remove each character of the string in alphabetical order ; Stores the frequency of characters of the string ; Iterate through the string ; Count the number of characters smaller than the present character ; If no smaller character precedes current character ; Increase the frequency of the current character ; Return the total cost ; Given string str ; Function call
function minSteps ( str , N ) { var smaller , cost = 0 ; var f = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { var curr_ele = str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; smaller = 0 ; for ( var j = 0 ; j <= curr_ele ; j ++ ) { if ( f [ j ] !== 0 ) smaller += f [ j ] ; } if ( smaller === 0 ) cost += i + 1 ; else cost += i - smaller + 1 ; f [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } return cost ; } var str = " " ; var N = str . length ; document . write ( minSteps ( str , N ) ) ;
Count of Rectangles with area K made up of only 1 s from given Binary Arrays | Function to find the subarrays of all possible lengths made up of only 1 s ; Stores the frequency of the subarrays ; Check if the previous value was also 0 ; If the previous value was 1 ; Find the subarrays of each size from 1 to count ; If A [ ] is of the form ... .111 ; Function to find the count of all possible rectangles ; Size of each of the arrays ; Stores the count of subarrays of each size consisting of only 1 s from array A [ ] ; Stores the count of subarrays of each size consisting of only 1 s from array B [ ] ; Iterating over all subarrays consisting of only 1 s in A [ ] ; If i is a factor of K , then there is a subarray of size K / i in B [ ] ; Driver Code
function findSubarrays ( a ) { let n = a . length ; let freq = new Array ( n + 1 ) . fill ( 0 ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { if ( count == 0 ) continue ; else { let value = count ; for ( let j = 1 ; j <= count ; j ++ ) { freq [ j ] += value ; value -- ; } count = 0 ; } } else count ++ ; } if ( count > 0 ) { let value = count ; for ( let j = 1 ; j <= count ; j ++ ) { freq [ j ] += value ; value -- ; } } return freq ; } function countRectangles ( a , b , K ) { let n = a . length ; let m = b . length ; let subA = findSubarrays ( a ) ; let subB = findSubarrays ( b ) ; let total = 0 ; for ( let i = 1 ; i < subA . length ; i ++ ) { if ( K % i == 0 && ( K / i ) <= m ) { total = total + subA [ i ] * subB [ K / i ] ; } } document . write ( total ) ; } let a = [ 0 , 0 , 1 , 1 ] ; let b = [ 1 , 0 , 1 ] ; let K = 2 ; countRectangles ( a , b , K ) ;