codeparrot/xlcost-text-to-code huggingface dataset:
Queries to count array elements from a given range having a single set bit | Check if N has only one set bit ; Function to build segment tree ; If se is leaf node ; Update the node ; Stores mid value of segment [ ss , se ] ; Recursive call for left Subtree ; Recursive call for right Subtree ; Update the Current Node ; Function to update a value at Xth index ; If ss is equal to X ; Update the Xth node ; Update the tree ; Stores the mid of segment [ ss , se ] ; Update current node ; Count of numbers having one set bit ; If L > se or R < ss Invalid Range ; If [ ss , se ] lies inside the [ L , R ] ; Stores the mid of segment [ ss , se ] ; Return the sum after recursively traversing left and right subtree ; Function to solve queries ; Initialise Segment tree ; Build segment tree ; Perform queries ; Input ; Function call to solve queries
function check ( N ) { if ( N == 0 ) return 0 ; return ! ( N & ( N - 1 ) ) ; } function build_seg_tree ( ss , se , si , tree , arr ) { if ( ss == se ) { tree [ si ] = check ( arr [ ss ] ) ; return ; } var mid = parseInt ( ( ss + se ) / 2 ) ; build_seg_tree ( ss , mid , 2 * si + 1 , tree , arr ) ; build_seg_tree ( mid + 1 , se , 2 * si + 2 , tree , arr ) ; tree [ si ] = tree [ 2 * si + 1 ] + tree [ 2 * si + 2 ] ; } function update ( ss , se , si , X , V , tree , arr ) { if ( ss == se ) { if ( ss == X ) { arr [ X ] = V ; tree [ si ] = check ( V ) ; } return ; } var mid = parseInt ( ( ss + se ) / 2 ) ; if ( X <= mid ) update ( ss , mid , 2 * si + 1 , X , V , tree , arr ) ; else update ( mid + 1 , se , 2 * si + 2 , X , V , tree , arr ) ; tree [ si ] = tree [ 2 * si + 1 ] + tree [ 2 * si + 2 ] ; } function query ( l , r , ss , se , si , tree ) { if ( r < ss l > se ) return 0 ; if ( l <= ss && r >= se ) return tree [ si ] ; var mid = parseInt ( ( ss + se ) / 2 ) ; return query ( l , r , ss , mid , 2 * si + 1 , tree ) + query ( l , r , mid + 1 , se , 2 * si + 2 , tree ) ; } function Query ( arr , N , Q ) { var tree = new Array ( 4 * N ) ; tree . fill ( 0 ) ; build_seg_tree ( 0 , N - 1 , 0 , tree , arr ) ; for ( var i = 0 ; i < Q . length ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) document . write ( query ( Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 , N - 1 , 0 , tree ) + " " ) ; else update ( 0 , N - 1 , 0 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , tree , arr ) ; } } var arr = [ 12 , 11 , 16 , 2 , 32 ] ; var Q = [ [ 1 , 0 , 2 ] , [ 2 , 4 , 24 ] , [ 1 , 1 , 4 ] ] ; var N = arr . length ; Query ( arr , N , Q ) ;
Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Binary Search ; Checks if sum of first ' mid ' natural numbers is greater than equal to X ; Update res ; Update high ; Update low ; Input ; Finds minimum value of N such that sum of first N natural number >= X
function isGreaterEqual ( N , X ) { return parseInt ( ( N * ( N + 1 ) ) / 2 ) >= X ; } function minimumPossible ( X ) { let low = 1 , high = X , res = - 1 ; while ( low <= high ) { let mid = low + parseInt ( ( high - low ) / 2 ) ; if ( isGreaterEqual ( mid , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } return res ; } let X = 14 ; document . write ( minimumPossible ( X ) ) ;
Find sum of non | Find the sum of all non - repeated elements in an array ; sort all elements of array ; Driver code
function findSum ( arr , n ) { arr . sort ( ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) sum = sum + arr [ i ] ; } return sum ; } let arr = [ 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 ] ; let n = arr . length ; document . write ( findSum ( arr , n ) ) ;
Check if a Binary Tree contains duplicate subtrees of size 2 or more | Javascript program to find if there is a duplicate sub - tree of size 2 or more . ; A binary tree Node has data , pointer to left child and a pointer to right child ; This function returns empty string if tree contains a duplicate subtree of size 2 or more . ; If current node is NULL , return marker ; If left subtree has a duplicate subtree . ; Do same for right subtree ; Serialize current subtree ; If current subtree already exists in hash table . [ Note that size of a serialized tree with single node is 3 as it has two marker nodes . ; Function to find if the Binary Tree contains duplicate subtrees of size 2 or more ; Driver program to test above functions
let MARKER = ' ' ; class Node { constructor ( data ) { this . data = data ; } } function dupSubUtil ( root , subtrees ) { let s = " " ; if ( root == null ) return s + MARKER ; let lStr = dupSubUtil ( root . left , subtrees ) ; if ( lStr == ( s ) ) return s ; let rStr = dupSubUtil ( root . right , subtrees ) ; if ( rStr == ( s ) ) return s ; s = s + root . data + lStr + rStr ; if ( s . length > 3 && subtrees . has ( s ) ) return " " ; subtrees . add ( s ) ; return s ; } function dupSub ( root ) { let subtrees = new Set ( ) ; return dupSubUtil ( root , subtrees ) ; } let root = new Node ( ' ' ) ; root . left = new Node ( ' ' ) ; root . right = new Node ( ' ' ) ; root . left . left = new Node ( ' ' ) ; root . left . right = new Node ( ' ' ) ; root . right . right = new Node ( ' ' ) ; root . right . right . right = new Node ( ' ' ) ; root . right . right . left = new Node ( ' ' ) ; let str = dupSub ( root ) ; if ( str == ( " " ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum substring removals required to make all remaining characters of a string same | Function to count minimum operations required to make all characters equal by repeatedly removing subString ; Remove consecutive duplicate characters from str ; Stores length of the String ; Stores frequency of each distinct characters of the String str ; Iterate over all the characters of the String str ; Update frequency of str [ i ] ; Decrementing the frequency of the String str [ 0 ] ; Decrementing the frequency of the String str [ N - 1 ] ; Stores the required count ( Max Integer value ) ; Iterate over all characters of the String str ; Update ans ; Given String
function minOperationNeeded ( str ) { str = modstring ( str ) ; var N = str . length ; var res = new Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; ++ i ) { res [ str [ i ] . charCodeAt ( 0 ) ] += 1 ; } res [ str [ 0 ] . charCodeAt ( 0 ) ] -= 1 ; res [ str [ N - 1 ] . charCodeAt ( 0 ) ] -= 1 ; var ans = 2147483647 ; for ( var i = 0 ; i < N ; ++ i ) { ans = Math . min ( ans , res [ str [ i ] . charCodeAt ( 0 ) ] ) ; } document . write ( ans + 1 + " " ) ; } function modstring ( str ) { var s = " " ; var b = true ; for ( var i = 1 ; i < str . length ; ++ i ) { if ( str [ i - 1 ] !== str [ i ] ) b = true ; if ( b ) { s += str [ i - 1 ] ; b = false ; } } return s . split ( " " ) ; } var str = " " ; minOperationNeeded ( str . split ( " " ) ) ;
Print all root to leaf paths of an N | Structure of an N ary tree node ; Function to print the root to leaf path of the given N - ary Tree ; Print elements in the vector ; Utility function to print all root to leaf paths of an Nary Tree ; If root is null ; Insert current node 's  data into the vector ; If current node is a leaf node ; Print the path ; Pop the leaf node and return ; Recur for all children of the current node ; Recursive Function Call ; Function to print root to leaf path ; If root is null , return ; Stores the root to leaf path ; Utility function call ; Given N - Ary tree ; Function Call
class Node { constructor ( x ) { this . data = x ; this . child = [ ] ; } } function printPath ( vec ) { for ( let ele = 0 ; ele < vec . length ; ele ++ ) { document . write ( vec [ ele ] + " " ) ; } document . write ( " " ) ; } function printAllRootToLeafPaths ( root , vec ) { if ( root == null ) return ; vec . push ( root . data ) ; if ( root . child . length == 0 ) { printPath ( vec ) ; vec . pop ( ) ; return ; } for ( let i = 0 ; i < root . child . length ; i ++ ) printAllRootToLeafPaths ( root . child [ i ] , vec ) ; vec . pop ( ) ; } function printAllRootToLeaf_Paths ( root ) { if ( root == null ) return ; let vec = [ ] ; printAllRootToLeafPaths ( root , vec ) ; } let root = new Node ( 1 ) ; ( root . child ) . push ( new Node ( 2 ) ) ; ( root . child ) . push ( new Node ( 3 ) ) ; ( root . child [ 0 ] . child ) . push ( new Node ( 4 ) ) ; ( root . child [ 1 ] . child ) . push ( new Node ( 5 ) ) ; ( root . child [ 1 ] . child ) . push ( new Node ( 6 ) ) ; ( root . child [ 1 ] . child [ 1 ] . child ) . push ( new Node ( 7 ) ) ; ( root . child [ 1 ] . child [ 1 ] . child ) . push ( new Node ( 8 ) ) ; printAllRootToLeaf_Paths ( root ) ;
Minimum removals to make a string concatenation of a substring of 0 s followed by a substring of 1 s | Function to count minimum removals required to make a given string concatenation of substring of 0 s followed by substring of 1 s ; Stores the length of the string ; Precompute the count of 0 s ; Check for the last character ; Traverse and update zeroCount array ; If current character is 0 , ; Update aCount [ i ] as aCount [ i + 1 ] + 1 ; Update as aCount [ i + 1 ] ; Keeps track of deleted 1 s ; Stores the count of removals ; Traverse the array ; If current character is 1 ; Update ans ; If all 1 s are deleted ; Return the minimum number of deletions ; Driver Code
function minimumDeletions ( s ) { let n = s . length ; let zeroCount = [ ] ; zeroCount [ n - 1 ] = ( s [ n - 1 ] == ' ' ) ? 1 : 0 ; for ( let i = n - 2 ; i >= 0 ; i -- ) zeroCount [ i ] = ( s [ i ] == ' ' ) ? zeroCount [ i + 1 ] + 1 : zeroCount [ i + 1 ] ; let oneCount = 0 ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { ans = Math . min ( ans , oneCount + zeroCount [ i ] ) ; oneCount ++ ; } } ans = Math . min ( ans , oneCount ) ; return ( ans == Number . MAX_VALUE ) ? 0 : ans ; } let str = " " ; document . write ( minimumDeletions ( str ) ) ;
Pairs of Positive Negative values in an array | Print pair with negative and positive value ; For each element of array . ; Try to find the negative value of arr [ i ] from i + 1 to n ; If absolute values are equal print pair . ; If size of vector is 0 , therefore there is no element with positive negative value , print "0" ; Sort the vector ; Print the pair with negative positive value . ; Driven Program
function printPairs ( arr , n ) { let v = [ ] ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( arr [ i ] ) == Math . abs ( arr [ j ] ) ) v . push ( Math . abs ( arr [ i ] ) ) ; if ( v . length == 0 ) return ; v . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < v . length ; i ++ ) document . write ( - v [ i ] + " " + v [ i ] ) ; } let arr = [ 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 ] ; let n = arr . length ; printPairs ( arr , n ) ;
Check if an array can be divided into pairs whose sum is divisible by k | Returns true if arr [ 0. . n - 1 ] can be divided leto pairs with sum divisible by k . ; An odd length array cannot be divided leto pairs ; Create a frequency array to count occurrences of all remainders when divided by k . ; Count occurrences of all remainders ; Traverse input array and use freq [ ] to decide if given array can be divided in pairs ; Remainder of current element ; If remainder with current element divides k leto two halves . ; Then there must be even occurrences of such remainder ; If remainder is 0 , then there must be two elements with 0 remainder ; Then there must be even occurrences of such remainder ; Else number of occurrences of remainder must be equal to number of occurrences of k - remainder ; Driver program ; Function call
function canPairs ( ar , k ) { if ( ar . length % 2 == 1 ) return false ; let hm = new Map ( ) ; for ( let i = 0 ; i < ar . length ; i ++ ) { let rem = ( ( ar [ i ] % k ) + k ) % k ; if ( ! hm . has ( rem ) ) { hm . set ( rem , 0 ) ; } hm . set ( rem , hm . get ( rem ) + 1 ) ; } for ( let i = 0 ; i < ar . length ; i ++ ) { let rem = ( ( ar [ i ] % k ) + k ) % k ; if ( 2 * rem == k ) { if ( hm . get ( rem ) % 2 == 1 ) return false ; } else if ( rem == 0 ) { if ( hm . get ( rem ) % 2 == 1 ) return false ; } else { if ( hm . get ( k - rem ) != hm . get ( rem ) ) return false ; } } return true ; } let arr = [ 92 , 75 , 65 , 48 , 45 , 35 ] ; let k = 10 ; let ans = canPairs ( arr , k ) ; if ( ans ) document . write ( " " ) ; else document . write ( " " ) ;
Find three element from different three arrays such that a + b + c = sum | Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Driver Code
function findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) { for ( var i = 0 ; i < n1 ; i ++ ) for ( var j = 0 ; j < n2 ; j ++ ) for ( var k = 0 ; k < n3 ; k ++ ) if ( a1 [ i ] + a2 [ j ] + a3 [ k ] == sum ) return true ; return false ; } var a1 = [ 1 , 2 , 3 , 4 , 5 ] ; var a2 = [ 2 , 3 , 6 , 1 , 2 ] ; var a3 = [ 3 , 2 , 4 , 5 , 6 ] ; var sum = 9 ; var n1 = a1 . length ; var n2 = a2 . length ; var n3 = a3 . length ; findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) ? document . write ( " " ) : document . write ( " " ) ;
Count quadruplets with sum K from given array | Function to return the number of quadruplets having given sum ; Initialize variables ; Initialize answer ; All possible first elements ; All possible second element ; Use map to find the fourth element ; All possible third elements ; Calculate number of valid 4 th elements ; Update the twice_count ; Unordered pairs ; Return answer ; Given array arr [ ] ; Given sum S ; Function Call
function countSum ( a , n , sum ) { var i , j , k , l ; var count = 0 ; for ( i = 0 ; i < n - 3 ; i ++ ) { for ( j = i + 1 ; j < n - 2 ; j ++ ) { var req = sum - a [ i ] - a [ j ] ; var m = new Map ( ) ; for ( k = j + 1 ; k < n ; k ++ ) { if ( m . has ( a [ k ] ) ) m . set ( a [ k ] , m . get ( a [ k ] ) + 1 ) else m . set ( a [ k ] , 1 ) } var twice_count = 0 ; for ( k = j + 1 ; k < n ; k ++ ) { if ( m . has ( req - a [ k ] ) ) twice_count += m . get ( req - a [ k ] ) ; if ( ( req - a [ k ] ) == a [ k ] ) twice_count -- ; } count += parseInt ( twice_count / 2 ) ; } } return count ; } var arr = [ 4 , 5 , 3 , 1 , 2 , 4 ] ; var S = 13 ; var N = arr . length ; document . write ( countSum ( arr , N , S ) ) ;
Check if all subarrays contains at least one unique element | Function to check if all subarrays have at least one unique element ; Generate all subarray ; Store frequency of subarray 's elements  map<int, int> hm; ; Traverse the array over the range [ i , N ] ; Update frequency of current subarray in map hm [ arr [ j ] ] ++ ; ; Increment count ; Decrement count ; If all subarrays have at least 1 unique element ; Given array [ ] arr ; Function Call
function check ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) { var hm = new Map ( ) ; var count = 0 ; for ( var j = i ; j < n ; j ++ ) { if ( hm . has ( arr [ j ] ) ) hm . set ( arr [ j ] , hm . get ( arr [ j ] ) + 1 ) else hm . set ( arr [ j ] , 1 ) if ( hm . get ( arr [ j ] ) == 1 ) count ++ ; if ( hm . get ( arr [ j ] ) == 2 ) count -- ; if ( count == 0 ) return " " ; } } return " " ; } var arr = [ 1 , 2 , 1 ] ; var N = arr . length ; document . write ( check ( arr , N ) ) ;
Count all disjoint pairs having absolute difference at least K from a given array | Function to count distinct pairs with absolute difference atleast K ; Track the element that have been paired ; Stores count of distinct pairs ; Pick all elements one by one ; If already visited ; If already visited ; If difference is at least K ; Mark element as visited and increment the count ; Print the final count ; Given arr [ ] ; Size of array ; Given difference K ; Function Call
function countPairsWithDiffK ( arr , N , K ) { var vis = new Array ( N ) ; vis . fill ( 0 ) ; var count = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( vis [ i ] == 1 ) continue ; for ( var j = i + 1 ; j < N ; j ++ ) { if ( vis [ j ] == 1 ) continue ; if ( Math . abs ( arr [ i ] - arr [ j ] ) >= K ) { count ++ ; vis [ i ] = 1 ; vis [ j ] = 1 ; break ; } } } document . write ( count + " " ) ; } var arr = [ 1 , 3 , 3 , 5 ] ; var N = arr . length ; var K = 2 ; countPairsWithDiffK ( arr , N , K ) ;
Check if two trees are Mirror | A binary tree node ; * Given two trees , return true if they are mirror of each other ; Base case : Both empty ; If only one is empty ; * Both non - empty , compare them recursively Note that in recursive calls , we * pass left of one tree and right of other tree ; Driver code to test above methods
class Node { Node ( data ) { this . data = data ; this . left = this . right = null ; } } var a , b ; function areMirror ( a , b ) { if ( a == null && b == null ) return true ; if ( a == null b == null ) return false ; return a . data == b . data && areMirror ( a . left , b . right ) && areMirror ( a . right , b . left ) ; } a = new Node ( 1 ) ; b = new Node ( 1 ) ; left = new Node ( 2 ) ; right = new Node ( 3 ) ; left . left = new Node ( 4 ) ; left . right = new Node ( 5 ) ; left = new Node ( 3 ) ; right = new Node ( 2 ) ; right . left = new Node ( 5 ) ; right . right = new Node ( 4 ) ; if ( areMirror ( a , b ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Find the length of largest subarray with 0 sum | Returns length of the maximum length subarray with 0 sum ; Creates an empty hashMap hM ; Initialize sum of elements ; Initialize result ; Traverse through the given array ; Add current element to sum ; Look this sum in hash table ; Else put this sum in hash table ; Driver program
function maxLen ( arr ) { let hM = new Map ( ) ; let sum = 0 ; let max_len = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ; if ( sum == 0 ) max_len = i + 1 ; let prev_i = hM . get ( sum ) ; if ( prev_i != null ) max_len = Math . max ( max_len , i - prev_i ) ; else hM . set ( sum , i ) ; } return max_len ; } let arr = [ 15 , - 2 , 2 , - 8 , 1 , 7 , 10 , 23 ] ; document . write ( " " + maxLen ( arr ) ) ;
Print alternate elements of an array | Function to print Alternate elements of the given array ; Print elements at odd positions ; Print elements of array ; Driver Code
function printAlter ( arr , N ) { for ( var currIndex = 0 ; currIndex < N ; currIndex += 2 ) { document . write ( arr [ currIndex ] + " " ) ; } } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var N = 5 ; printAlter ( arr , N ) ;
Longest Increasing consecutive subsequence | function that returns the length of the longest increasing subsequence whose adjacent element differ by 1 ; stores the index of elements ; stores the length of the longest subsequence that ends with a [ i ] ; iterate for all element ; if a [ i ] - 1 is present before i - th index ; last index of a [ i ] - 1 ; Driver Code
function longestSubsequence ( a , n ) { var mp = new Map ( ) ; var dp = Array ( n ) . fill ( 0 ) ; var maximum = - 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { if ( mp . has ( a [ i ] - 1 ) ) { var lastIndex = mp . get ( a [ i ] - 1 ) - 1 ; dp [ i ] = 1 + dp [ lastIndex ] ; } else dp [ i ] = 1 ; mp . set ( a [ i ] , i + 1 ) ; maximum = Math . max ( maximum , dp [ i ] ) ; } return maximum ; } var a = [ 3 , 10 , 3 , 11 , 4 , 5 , 6 , 7 , 8 , 12 ] ; var n = a . length ; document . write ( longestSubsequence ( a , n ) ) ;
Longest subsequence such that difference between adjacents is one | Set 2 | function to find longest subsequence such that difference between adjacents is one ; hash table to map the array element with the length of the longest subsequence of which it is a part of and is the last element of that subsequence ; to store the longest length subsequence ; traverse the array elements ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' um ' and its length of subsequence is greater than ' len ' ; if ' arr [ i ] + 1' is in ' um ' and its length of subsequence is greater than ' len ' ; update arr [ i ] subsequence length in ' um ' ; update longest length ; required longest length subsequence ; Driver Code
function longLenSub ( arr , n ) { let um = new Map ( ) ; let longLen = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let len = 0 ; if ( um . has ( arr [ i ] - 1 ) && len < um . get ( arr [ i ] - 1 ) ) len = um . get ( arr [ i ] - 1 ) ; if ( um . has ( arr [ i ] + 1 ) && len < um . get ( arr [ i ] + 1 ) ) len = um . get ( arr [ i ] + 1 ) ; um . set ( arr [ i ] , len + 1 ) ; if ( longLen < um . get ( arr [ i ] ) ) longLen = um . get ( arr [ i ] ) ; } return longLen ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 3 , 2 ] ; let n = arr . length ; document . write ( " " + longLenSub ( arr , n ) ) ;
Count smaller primes on the right of each array element | Javascript Program for the above approach ; Function to check if a number is prime or not ; Function to update a Binary Tree ; Function to find the sum of all the elements which are less than or equal to index ; Function to find the number of smaller primes on the right for every array element ; Iterate the array in backwards ; Calculating the required number of primes ; If current array element is prime ; Update the Fenwick tree ; Driver Code ; Function call
let maxn = 1e6 + 5 ; let BITree = new Array ( maxn ) ; BITree . fill ( 0 ) ; function is_prime ( n ) { if ( n <= 1 ) return false ; for ( let i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function update_bitree ( BITree , index , value ) { while ( index <= maxn ) { BITree [ index ] += value ; index += ( index & ( - index ) ) ; } } function sum_bitree ( BITree , index ) { let s = 0 ; while ( index > 0 ) { s += BITree [ index ] ; index -= ( index & ( - index ) ) ; } return s ; } function countSmallerPrimes ( BITree , ar , N ) { let ans = new Array ( N ) ; for ( let i = N - 1 ; i >= 0 ; i -- ) { ans [ i ] = sum_bitree ( BITree , ar [ i ] ) ; if ( is_prime ( ar [ i ] ) ) update_bitree ( BITree , ar [ i ] , 1 ) ; } for ( let i = 0 ; i < N ; i ++ ) document . write ( ans [ i ] + " " ) ; } let ar = [ 5 , 5 , 17 , 9 , 12 , 15 , 11 , 7 , 39 , 3 ] ; let N = ar . length ; countSmallerPrimes ( BITree , ar , N ) ;
Count distinct elements in every window of size k | Counts distinct elements in window of size k ; Traverse the window ; Check if element arr [ i ] exists in arr [ 0. . i - 1 ] ; Counts distinct elements in all windows of size k ; Traverse through every window ; Driver program
function countWindowDistinct ( win , k ) { let dist_count = 0 ; for ( let i = 0 ; i < k ; i ++ ) { let j ; for ( j = 0 ; j < i ; j ++ ) if ( win [ i ] == win [ j ] ) break ; if ( j == i ) dist_count ++ ; } return dist_count ; } function countDistinct ( arr , n , k ) { for ( let i = 0 ; i <= n - k ; i ++ ) document . write ( countWindowDistinct ( arr . slice ( i , arr . length ) , k ) + " " ) ; } let arr = [ 1 , 2 , 1 , 3 , 4 , 2 , 3 ] , k = 4 ; countDistinct ( arr , arr . length , k ) ;
Maximum score assigned to a subsequence of numerically consecutive and distinct array elements | Function to find the maximum score possible ; Base Case ; If previously occurred subproblem occurred ; Check if lastpicked element differs by 1 from the current element ; Calculate score by including the current element ; Calculate score by excluding the current element ; Return maximum score from the two possibilities ; Function to prlet maximum score ; DP array to store results ; Initialise dp with - 1 ; Function call ; Given arrays ; Function Call
function maximumSum ( a , b , n , index , lastpicked , dp ) { if ( index == n ) return 0 ; if ( dp [ index ] [ lastpicked + 1 ] != - 1 ) return dp [ index ] [ lastpicked + 1 ] ; let option1 = 0 , option2 = 0 ; if ( lastpicked == - 1 a [ lastpicked ] != a [ index ] ) { option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index , dp ) ; } option2 = maximumSum ( a , b , n , index + 1 , lastpicked , dp ) ; return dp [ index ] [ lastpicked + 1 ] = Math . max ( option1 , option2 ) ; } function maximumPolets ( arr , brr , n ) { let index = 0 , lastPicked = - 1 ; let dp = new Array ( n + 5 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < dp . length ; i ++ ) { for ( var j = 0 ; j < dp . length ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( maximumSum ( arr , brr , n , index , lastPicked , dp ) ) ; } let arr = [ 1 , 2 , 3 , 3 , 3 , 1 ] ; let brr = [ - 1 , 2 , 10 , 20 , - 10 , - 9 ] ; let N = arr . length ; maximumPolets ( arr , brr , N ) ;