codeparrot/xlcost-text-to-code huggingface dataset:
Minimum salary hike for each employee such that no employee feels unfair | Javascript Program to find the minimum hike of each employee such that no adjacent employee feels unfair ; As hikes are positive integers , keeping minimum value ; Pass - 1 : compare with previous employee ; Pass - 2 : compare with Next employee ; Driver Code ; Function Call ; result -> [ 2 , 1 , 3 , 2 , 1 , 2 ] ; Function Call ; result -> [ 1 , 2 , 3 , 1 ] ; Function Call ; result -> [ 1 , 2 ]
function findMinimumHike ( ratings , employees ) { let hikes = new Array ( employees ) ; for ( let i = 0 ; i < employees ; i ++ ) hikes [ i ] = 1 ; for ( let i = 1 ; i < employees ; i ++ ) { if ( ratings [ i - 1 ] < ratings [ i ] && hikes [ i - 1 ] >= hikes [ i ] ) hikes [ i ] = hikes [ i - 1 ] + 1 ; } for ( let i = employees - 2 ; i >= 0 ; i -- ) { if ( ratings [ i ] > ratings [ i + 1 ] && hikes [ i + 1 ] >= hikes [ i ] ) hikes [ i ] = hikes [ i + 1 ] + 1 ; } return hikes ; } let data = [ 5 , 3 , 4 , 2 , 1 , 6 ] ; let result = findMinimumHike ( data , data . length ) ; document . write ( " " + ( result ) . join ( " " ) + " " ) ; data = [ 1 , 3 , 5 , 4 ] ; result = findMinimumHike ( data , data . length ) ; document . write ( " " + ( result ) . join ( " " ) + " " ) ; data = [ 1 , 4 ] ; result = findMinimumHike ( data , data . length ) ; document . write ( " " + ( result ) . join ( " " ) + " " ) ;
Find sum of all nodes of the given perfect binary tree | function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; sum of nodes at last level ; sum of all nodes ; Driver Code
function sumNodes ( l ) { let leafNodeCount = Math . pow ( 2 , l - 1 ) ; let sumLastLevel = 0 ; sumLastLevel = ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ; let sum = sumLastLevel * l ; return sum ; } let l = 3 ; document . write ( sumNodes ( l ) ) ;
Longest subsequence with different adjacent characters | dp table ; A recursive function to find the update the dp [ ] [ ] table ; If we reach end of the String ; If subproblem has been computed ; Initialise variable to find the maximum length ; Choose the current character ; Omit the current character ; Return the store answer to the current subproblem ; Function to find the longest Subsequence with different adjacent character ; Length of the String s ; Initialise the memoisation table ; Return the final ans after every recursive call ; Driver Code ; Function call
let dp = new Array ( 100005 ) ; function calculate ( pos , prev , s ) { if ( pos == s . length ) { return 0 ; } if ( dp [ pos ] [ prev ] != - 1 ) return dp [ pos ] [ prev ] ; let val = 0 ; if ( s [ pos ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 != prev ) { val = Math . max ( val , 1 + calculate ( pos + 1 , s [ pos ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 , s ) ) ; } val = Math . max ( val , calculate ( pos + 1 , prev , s ) ) ; dp [ pos ] [ prev ] = val ; return dp [ pos ] [ prev ] ; } function longestSubsequence ( s ) { let n = s . length ; for ( let i = 0 ; i < 100005 ; i ++ ) { dp [ i ] = new Array ( 27 ) ; for ( let j = 0 ; j < 27 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return calculate ( 0 , 0 , s ) ; } let str = " " ; document . write ( longestSubsequence ( str ) ) ;
Constant time range add operation on an array | Utility method to add value val , to range [ lo , hi ] ; Utility method to get actual array from operation array ; convert array into prefix sum array ; method to print final updated array ; Driver code ; Range add Queries
function add ( arr , N , lo , hi , val ) { arr [ lo ] += val ; if ( hi != N - 1 ) arr [ hi + 1 ] -= val ; } function updateArray ( arr , N ) { for ( let i = 1 ; i < N ; i ++ ) arr [ i ] += arr [ i - 1 ] ; } function printArr ( arr , N ) { updateArray ( arr , N ) ; for ( let i = 0 ; i < N ; i ++ ) document . write ( " " + arr [ i ] + " " ) ; document . write ( " " ) ; } let N = 6 ; let arr = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = 0 ; } add ( arr , N , 0 , 2 , 100 ) ; add ( arr , N , 1 , 5 , 100 ) ; add ( arr , N , 2 , 3 , 100 ) ; printArr ( arr , N ) ;
Count of elements in an Array whose set bits are in a multiple of K | Function to find the count of numbers ; Get the set - bits count of each element ; Check if the setbits count is divisible by K ; Increment the count of required numbers by 1 ;  ; Driver code
function find_count ( arr , k ) { var ans = 0 ; for ( var i = 0 ; i <= arr . length ; i ++ ) { var x = countSetBits ( i ) ; if ( x % k == 0 ) ans += 1 ; } return ans ; } function countSetBits ( x ) { var setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } var arr = [ 12 , 345 , 2 , 68 , 7896 ] ; var K = 2 ; document . write ( find_count ( arr , K ) ) ;
Check if a number can be expressed as a product of exactly K prime divisors | function to find K prime divisors ; count number of 2 s that divide N ; N must be odd at this point . So we can skip one element ; divide the value of N ; increment count ; Condition to handle the case when n is a prime number greater than 2 ; check if maximum_split is less than K then it not possible ; initialise N and K
function KPrimeDivisors ( N , K ) { var maximum_split = 0 ; while ( N % 2 == 0 ) { maximum_split ++ ; N /= 2 ; } for ( i = 3 ; i * i <= N ; i = i + 2 ) { while ( N % i == 0 ) { N = N / i ; maximum_split ++ ; } } if ( N > 2 ) maximum_split ++ ; if ( maximum_split < K ) { document . write ( " " ) ; return ; } document . write ( " " ) ; } var N = 12 ; var K = 3 ; KPrimeDivisors ( N , K ) ;
Find sum in range L to R in given sequence of integers | Function to find the sum within the given range ; generating array from given sequence ; calculate the desired sum ; return the sum ; Initialise the range
function findSum ( L , R ) { var arr = [ ] ; var i = 0 ; var x = 2 ; while ( i <= R ) { arr . push ( i + x ) ; if ( i + 1 <= R ) arr . push ( i + 1 + x ) ; x *= - 1 ; i += 2 ; } var sum = 0 ; for ( var i = L ; i <= R ; ++ i ) sum += arr [ i ] ; return sum ; } var L = 0 , R = 5 ; document . write ( findSum ( L , R ) ) ;
Queries for GCD of all numbers of an array except elements in a given range | Calculating GCD using euclid algorithm ; Filling the prefix and suffix array ; Filling the prefix array following relation prefix ( i ) = GCD ( prefix ( i - 1 ) , arr ( i ) ) ; Filling the suffix array following the relation suffix ( i ) = GCD ( suffix ( i + 1 ) , arr ( i ) ) ; To calculate gcd of the numbers outside range ; If l = 0 , we need to tell GCD of numbers from r + 1 to n ; If r = n - 1 we need to return the gcd of numbers from 1 to l ; Driver code
function GCD ( a , b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } function FillPrefixSuffix ( prefix , arr , suffix , n ) { prefix [ 0 ] = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) prefix [ i ] = GCD ( prefix [ i - 1 ] , arr [ i ] ) ; suffix [ n - 1 ] = arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) suffix [ i ] = GCD ( suffix [ i + 1 ] , arr [ i ] ) ; } function GCDoutsideRange ( l , r , prefix , suffix , n ) { if ( l == 0 ) return suffix [ r + 1 ] ; if ( r == n - 1 ) return prefix [ l - 1 ] ; return GCD ( prefix [ l - 1 ] , suffix [ r + 1 ] ) ; } var arr = [ 2 , 6 , 9 ] ; var n = arr . length ; var prefix = Array ( n ) . fill ( 0 ) ; var suffix = Array ( n ) . fill ( 0 ) ; FillPrefixSuffix ( prefix , arr , suffix , n ) ; var l = 0 , r = 0 ; document . write ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; document . write ( " " ) ; l = 1 ; r = 1 ; document . write ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; document . write ( " " ) ; l = 1 ; r = 2 ; document . write ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; document . write ( " " ) ;
Count of pairs from arrays A and B such that element in A is greater than element in B at that index | Function to find the maximum count of values that follow the given condition ; Initializing the max - heap for the array A [ ] ; Adding the values of A [ ] into max heap ; Adding the values of B [ ] into max heap ; Counter variable ; Loop to iterate through the heap ; Comparing the values at the top . If the value of heap A [ ] is greater , then counter is incremented ; Driver code
function check ( A , B , N ) { let pq1 = [ ] ; let pq2 = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { pq1 . push ( A [ i ] ) ; } for ( let i = 0 ; i < N ; i ++ ) { pq2 . push ( B [ i ] ) ; } pq1 . sort ( function ( a , b ) { return a - b ; } ) ; pq1 . reverse ( ) ; pq2 . sort ( function ( a , b ) { return a - b ; } ) ; pq2 . reverse ( ) ; let c = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( pq1 [ 0 ] > pq2 [ 0 ] ) { c ++ ; pq1 . shift ( ) ; pq2 . shift ( ) ; } else { if ( pq2 . length == 0 ) { break ; } pq2 . shift ( ) ; } } return ( c ) ; } let A = [ 10 , 3 , 7 , 5 , 8 ] ; let B = [ 8 , 6 , 2 , 5 , 9 ] ; let N = A . length ; document . write ( check ( A , B , N ) ) ;
Queries for counts of array elements with values in given range | function to count elements within given range ; initialize result ; check if element is in range ; driver function ; Answer queries
function countInRange ( arr , n , x , y ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= x && arr [ i ] <= y ) count ++ ; } return count ; } let arr = [ 1 , 3 , 4 , 9 , 10 , 3 ] ; let n = arr . length ; let i = 1 , j = 4 ; document . write ( countInRange ( arr , n , i , j ) + " " ) ; i = 9 ; j = 12 ; document . write ( countInRange ( arr , n , i , j ) ) ;
Queries for counts of array elements with values in given range | function to find first index >= x ; function to find last index <= y ; function to count elements within given range ; initialize result ; Driver Code ; Preprocess array ; Answer queries
function lowerIndex ( arr , n , x ) { let l = 0 , h = n - 1 ; while ( l <= h ) { let mid = parseInt ( ( l + h ) / 2 , 10 ) ; if ( arr [ mid ] >= x ) h = mid - 1 ; else l = mid + 1 ; } return l ; } function upperIndex ( arr , n , y ) { let l = 0 , h = n - 1 ; while ( l <= h ) { let mid = parseInt ( ( l + h ) / 2 , 10 ) ; if ( arr [ mid ] <= y ) l = mid + 1 ; else h = mid - 1 ; } return h ; } function countInRange ( arr , n , x , y ) { let count = 0 ; count = upperIndex ( arr , n , y ) - lowerIndex ( arr , n , x ) + 1 ; return count ; } let arr = [ 1 , 4 , 4 , 9 , 10 , 3 ] ; let n = arr . length ; arr . sort ( function ( a , b ) { return a - b } ) ; let i = 1 , j = 4 ; document . write ( countInRange ( arr , n , i , j ) + " " ) ; ; i = 9 ; j = 12 ; document . write ( countInRange ( arr , n , i , j ) ) ;
Queries for decimal values of subarrays of a binary array | Fills pre [ ] ; returns the number represented by a binary subarray l to r ; if r is equal to n - 1 r + 1 does not exist ; Driver code
function precompute ( arr , n , pre ) { for ( let i = 0 ; i < n ; i ++ ) pre [ i ] = 0 ; pre [ n - 1 ] = arr [ n - 1 ] * ( Math . pow ( 2 , 0 ) ) ; for ( let i = n - 2 ; i >= 0 ; i -- ) pre [ i ] = pre [ i + 1 ] + arr [ i ] * ( 1 << ( n - 1 - i ) ) ; } function decimalOfSubarr ( arr , l , r , n , pre ) { if ( r != n - 1 ) return ( pre [ l ] - pre [ r + 1 ] ) / ( 1 << ( n - 1 - r ) ) ; return pre [ l ] / ( 1 << ( n - 1 - r ) ) ; } let arr = [ 1 , 0 , 1 , 0 , 1 , 1 ] ; let n = arr . length ; let pre = new Array ( n ) precompute ( arr , n , pre ) ; document . write ( decimalOfSubarr ( arr , 2 , 4 , n , pre ) + " " ) ; document . write ( decimalOfSubarr ( arr , 4 , 5 , n , pre ) ) ;
Count elements which divide all numbers in range L | function to count element Time complexity O ( n ^ 2 ) worst case ; answer for query ; 0 based index ; iterate for all elements ; check if the element divides all numbers in range ; no of elements ; if all elements are divisible by a [ i ] ; answer for every query ; Driver Code
function answerQuery ( a , n , l , r ) { var count = 0 ; l = l - 1 ; for ( i = l ; i < r ; i ++ ) { var element = a [ i ] ; var divisors = 0 ; for ( j = l ; j < r ; j ++ ) { if ( a [ j ] % a [ i ] == 0 ) divisors ++ ; else break ; } if ( divisors == ( r - l ) ) count ++ ; } return count ; } var a = [ 1 , 2 , 3 , 5 ] ; var n = a . length ; var l = 1 , r = 4 ; document . write ( answerQuery ( a , n , l , r ) + " " ) ; l = 2 ; r = 4 ; document . write ( answerQuery ( a , n , l , r ) ) ;
Minimum number of Binary strings to represent a Number | Function to find the minimum number of binary Strings to represent a number ; Storing digits in correct order ; Find the maximum digit in the array ; Traverse for all the binary Strings ; If digit at jth position is greater than 0 then substitute 1 ;  ; Driver code
function minBinary ( n ) { var digit = Array ( 10 ) . fill ( 0 ) ; var len = 0 ; while ( n > 0 ) { digit [ len ++ ] = n % 10 ; n = parseInt ( n / 10 ) ; } digit = reverse ( digit , 0 , len - 1 ) ; var ans = 0 ; for ( i = 0 ; i < len ; i ++ ) { ans = Math . max ( ans , digit [ i ] ) ; } document . write ( " " + " " + ans + " " ) ; for ( i = 1 ; i <= ans ; i ++ ) { var num = 0 ; for ( j = 0 ; j < len ; j ++ ) { if ( digit [ j ] > 0 ) { num = num * 10 + 1 ; digit [ j ] -- ; } else { num *= 10 ; } } document . write ( num + " " ) ; } } function reverse ( str , start , end ) { var temp ; while ( start <= end ) { temp = str [ start ] ; str [ start ] = str [ end ] ; str [ end ] = temp ; start ++ ; end -- ; } return str ; } var n = 564 ; minBinary ( n ) ;
Number whose sum of XOR with given array range is maximum | Javascript program to find smallest integer X such that sum of its XOR with range is maximum . ; Function to make prefix array which counts 1 's of each bit up to that number ; Making a prefix array which sums number of 1 's up to that position ; If j - th bit of a number is set then add one to previously counted 1 's ; Function to find X ; Initially taking maximum value all bits 1 ; Iterating over each bit ; get 1 ' s ▁ at ▁ ith ▁ bit ▁ between ▁ the ▁ ▁ range ▁ L - R ▁ by ▁ subtracting ▁ 1' s till Rth number - 1 's till L-1th number ; If 1 ' s ▁ are ▁ more ▁ than ▁ or ▁ equal ▁ to ▁ 0' s then unset the ith bit from answer ; Set ith bit to 0 by doing Xor with 1 ; Driver program Taking inputs
const MAX = 2147483647 ; let one = new Array ( 100001 ) ; for ( let i = 0 ; i < 100001 ; i ++ ) one [ i ] = new Array ( 32 ) ; function make_prefix ( A , n ) { for ( let j = 0 ; j < 32 ; j ++ ) one [ 0 ] [ j ] = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let a = A [ i - 1 ] ; for ( let j = 0 ; j < 32 ; j ++ ) { let x = Math . pow ( 2 , j ) ; if ( a & x ) one [ i ] [ j ] = 1 + one [ i - 1 ] [ j ] ; else one [ i ] [ j ] = one [ i - 1 ] [ j ] ; } } } function Solve ( L , R ) { let l = L , r = R ; let tot_bits = r - l + 1 ; let X = MAX ; for ( let i = 0 ; i < 31 ; i ++ ) { let x = one [ r ] [ i ] - one [ l - 1 ] [ i ] ; if ( x >= tot_bits - x ) { let ith_bit = Math . pow ( 2 , i ) ; X = X ^ ith_bit ; } } return X ; } let n = 5 , q = 3 ; let A = [ 210 , 11 , 48 , 22 , 133 ] ; let L = [ 1 , 4 , 2 ] , R = [ 3 , 14 , 4 ] ; make_prefix ( A , n ) ; for ( let j = 0 ; j < q ; j ++ ) document . write ( Solve ( L [ j ] , R [ j ] ) + " " ) ;
Array range queries over range queries | Function to execute type 1 query ; incrementing the array by 1 for type 1 queries ; Function to execute type 2 query ; If the query is of type 1 function call to type 1 query ; If the query is of type 2 recursive call to type 2 query ; Input size of array amd number of queries ; Build query matrix ; Perform queries ; printing the result
function type1 ( arr , start , limit ) { for ( let i = start ; i <= limit ; i ++ ) arr [ i ] ++ ; } function type2 ( arr , query , start , limit ) { for ( let i = start ; i <= limit ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) type1 ( arr , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else if ( query [ i ] [ 0 ] == 2 ) type2 ( arr , query , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } let n = 5 , m = 5 ; let arr = new Array ( n + 1 ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = 0 ; } let temp = [ 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 ] ; let query = new Array ( 6 ) ; for ( let i = 0 ; i < 6 ; i ++ ) { query [ i ] = new Array ( 4 ) ; for ( let j = 0 ; j < 4 ; j ++ ) { query [ i ] [ j ] = 0 ; } } let j = 0 ; for ( let i = 1 ; i <= m ; i ++ ) { query [ i ] [ 0 ] = temp [ j ++ ] ; query [ i ] [ 1 ] = temp [ j ++ ] ; query [ i ] [ 2 ] = temp [ j ++ ] ; } for ( let i = 1 ; i <= m ; i ++ ) if ( query [ i ] [ 0 ] == 1 ) type1 ( arr , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else if ( query [ i ] [ 0 ] == 2 ) type2 ( arr , query , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; for ( let i = 1 ; i <= n ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ;
Array range queries over range queries | Function to create the record array ; Driver Code ; Build query matrix ; If query is of type 2 then function call to record_sum ; If query is of type 1 then simply add 1 to the record array ; for type 1 queries adding the contains of record array to the main array record array ; printing the array
function record_sum ( record , l , r , n , adder ) { for ( let i = l ; i <= r ; i ++ ) { record [ i ] += adder ; } } let n = 5 , m = 5 ; let arr = new Array ( n ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = 0 ; } let query = [ [ 1 , 1 , 2 ] , [ 1 , 4 , 5 ] , [ 2 , 1 , 2 ] , [ 2 , 1 , 3 ] , [ 2 , 3 , 4 ] ] ; let record = new Array ( m ) ; for ( let i = 0 ; i < record . length ; i ++ ) { record [ i ] = 0 ; } for ( let i = m - 1 ; i >= 0 ; i -- ) { if ( query [ i ] [ 0 ] == 2 ) { record_sum ( record , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , m , record [ i ] + 1 ) ; } else { record_sum ( record , i , i , m , 1 ) ; } } for ( let i = 0 ; i < m ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) { record_sum ( arr , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , n , record [ i ] ) ; } } for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; }
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; Iterate over 1 to a to find distinct pairs ; For each integer from 1 to a b / n integers exists such that pair sum is divisible by n ; If ( i % n + b % n ) >= n one more pair is possible ; Return answer ; Driver code
function findCountOfPairs ( a , b , n ) { let ans = 0 ; for ( let i = 1 ; i <= a ; i ++ ) { ans += parseInt ( b / n , 10 ) ; ans += ( i % n + b % n ) >= n ? 1 : 0 ; } return ans ; } let a = 5 , b = 13 , n = 3 ; document . write ( findCountOfPairs ( a , b , n ) ) ;
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; if first element is bigger then swap ; count is store the number of pair . ; we use temp for breaking a loop . ; count when a is greater . ; Count when a is smaller but b is greater ; Count when a and b both are smaller ; breaking condition ; For storing The pair in count . ; return the number of pairs . ; Driver code
function findCountOfPairs ( a , b , n ) { if ( a > b ) { let temp1 = a ; a = b ; b = temp1 ; } let temp = 1 , count = 0 ; for ( let i = n ; temp > 0 ; i += n ) { if ( a >= i ) { temp = i - 1 ; } else if ( b >= i ) { temp = a ; } else if ( i > b ) { temp = a - ( i - b ) + 1 ; } if ( temp > 0 ) { count += temp ; } } return count ; } let a = 5 , b = 13 , n = 3 ; document . write ( findCountOfPairs ( a , b , n ) ) ;
Array range queries for elements with frequency same as value | Returns the count of number x with frequency x in the subarray from start to end ; map for frequency of elements ; store frequency of each element in arr [ start ; end ] ; Count elements with same frequency as value ; Driver code ; 2D array of queries with 2 columns ; calculating number of queries
function solveQuery ( start , end , arr ) { let mp = new Map ( ) ; for ( let i = start ; i <= end ; i ++ ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; let count = 0 ; for ( let [ key , value ] of mp . entries ( ) ) if ( key == value ) count ++ ; return count ; } let A = [ 1 , 2 , 2 , 3 , 3 , 3 ] ; let n = A . length ; let queries = [ [ 0 , 1 ] , [ 1 , 1 ] , [ 0 , 2 ] , [ 1 , 3 ] , [ 3 , 5 ] , [ 0 , 5 ] ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) { let start = queries [ i ] [ 0 ] ; let end = queries [ i ] [ 1 ] ; document . write ( " " + ( i + 1 ) + " " + solveQuery ( start , end , A ) + " " ) ; }
Buy minimum items without change and given coins | Javascript program of the above approach ; See if we can buy less than 10 items Using 10 Rs coins and one r Rs coin ; We can always buy 10 items ; Driver code
function minItems ( k , r ) { for ( let i = 1 ; i < 10 ; i ++ ) if ( ( i * k - r ) % 10 == 0 || ( i * k ) % 10 == 0 ) return i ; return 10 ; } let k = 15 ; let r = 2 ; document . write ( minItems ( k , r ) ) ;
Number of indexes with equal elements in given range | function that answers every query in O ( r - l ) ; traverse from l to r and count the required indexes ; Driver Code ; 1 - st query ; 2 nd query
function answer_query ( a , n , l , r ) { var count = 0 ; for ( var i = l ; i < r ; i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ; return count ; } var a = [ 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 ] var n = a . length ; var L , R ; L = 1 ; R = 8 ; document . write ( answer_query ( a , n , L , R ) + " " ) ; L = 0 ; R = 4 ; document . write ( answer_query ( a , n , L , R ) ) ;
Diagonal Sum of a Binary Tree | Node Structure ; to map the node with level - index ; recursvise function to calculate sum of elements where level - index is same . ; if there is no child then return ; add the element in the group of node whose level - index is equal ; left child call ; right child call ; Function call ; for different values of level - index add te sum of those node to answer ; build binary tree ; Function Call ; print the daigonal sums
class Node { constructor ( data ) { this . data = data ; this . left = null ; this . right = null ; } } let grid = new Map ( ) ; function addConsideringGrid ( root , level , index ) { if ( root == null ) return ; if ( grid . has ( level - index ) ) { grid . set ( level - index , grid . get ( level - index ) + ( root . data ) ) ; } else { grid . set ( level - index , root . data ) ; } addConsideringGrid ( root . left , level + 1 , index - 1 ) ; addConsideringGrid ( root . right , level + 1 , index + 1 ) ; } function diagonalSum ( root ) { addConsideringGrid ( root , 0 , 0 ) ; let ans = [ ] ; for ( let [ key , value ] of grid ) { ans . push ( value ) ; } return ans ; } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 9 ) ; root . left . right = new Node ( 6 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 5 ) ; root . right . left . right = new Node ( 7 ) ; root . right . left . left = new Node ( 12 ) ; root . left . right . left = new Node ( 11 ) ; root . left . left . right = new Node ( 10 ) ; let v = diagonalSum ( root ) ; for ( let i = 0 ; i < v . length ; i ++ ) document . write ( v [ i ] + " " ) ;
Number of indexes with equal elements in given range | JavaScript program to count the number of indexes in range L R such that Ai = Ai + 1 ; array to store count of index from 0 to i that obey condition ; precomputing prefixans [ ] array ; traverse to compute the prefixans [ ] array ; function that answers every query in O ( 1 ) ; Driver Code ; pre - computation ; 1 - st query ; 2 nd query
const N = 1000 ; let prefixans = new Uint8Array ( N ) ; function countIndex ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == a [ i + 1 ] ) prefixans [ i ] = 1 ; if ( i != 0 ) prefixans [ i ] += prefixans [ i - 1 ] ; } } function answer_query ( l , r ) { if ( l == 0 ) return prefixans [ r - 1 ] ; else return prefixans [ r - 1 ] - prefixans [ l - 1 ] ; } let a = [ 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 ] ; let n = a . length ; countIndex ( a , n ) ; let L , R ; L = 1 ; R = 8 ; document . write ( answer_query ( L , R ) + " " ) ; L = 0 ; R = 4 ; document . write ( answer_query ( L , R ) + " " ) ;