codeparrot/xlcost-text-to-code huggingface dataset:
Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree | Structure of a Tree node ; Function to create a new node ; Function to find the min value of node for each level ; Count is used to diffentiate each level of the tree ; Function to check whether the nodes in the level below it are smaller by performing post order traversal ; Traverse the left subtree ; Traverse right subtree ; Check from minimum values computed at each level ; Function to print count of nodes from all lower levels having values less than the the nodes in the current level ; Stores the number of levels ; Stores the required count of nodes for each level ; 4 / \ 3 5 / \ / \ 10 2 3 1
class Node { constructor ( key ) { this . left = null ; this . right = null ; this . key = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return ( temp ) ; } function calculateMin ( root , levelMin ) { let qt = [ ] ; qt . push ( root ) ; let count = 1 ; let min_v = Number . MAX_VALUE ; while ( qt . length > 0 ) { let temp = qt [ 0 ] ; min_v = Math . min ( min_v , temp . key ) ; qt . shift ( ) ; if ( temp . left != null ) { qt . push ( temp . left ) ; } if ( temp . right != null ) { qt . push ( temp . right ) ; } count -- ; if ( count == 0 ) { levelMin . push ( min_v ) ; min_v = Number . MAX_VALUE ; count = qt . length ; } } } function findNodes ( root , levelMin , levelResult , level ) { if ( root == null ) return ; findNodes ( root . left , levelMin , levelResult , level + 1 ) ; findNodes ( root . right , levelMin , levelResult , level + 1 ) ; for ( let i = 0 ; i < level ; i ++ ) { if ( root . key <= levelMin [ i ] ) { levelResult [ i ] += 1 ; } } } function printNodes ( root ) { let levelMin = [ ] ; calculateMin ( root , levelMin ) ; let numLevels = levelMin . length ; let levelResult = new Array ( numLevels ) ; levelResult . fill ( 0 ) ; findNodes ( root , levelMin , levelResult , 0 ) ; for ( let i = 0 ; i < levelResult . length ; i ++ ) { document . write ( levelResult [ i ] + " " ) ; } } let root = newNode ( 4 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 5 ) ; root . right . left = newNode ( 3 ) ; root . right . right = newNode ( 1 ) ; root . left . left = newNode ( 10 ) ; root . left . right = newNode ( 2 ) ; printNodes ( root ) ;
Count subarrays which contains both the maximum and minimum array element | Function to count subarray containing both maximum and minimum array elements ; If the length of the array is less than 2 ; Find the index of maximum element ; Find the index of minimum element ; If i > j , then swap the value of i and j ; Return the answer ; Function to return max_element index ; Function to return min_element index ; Driver Code ; Function call
function countSubArray ( arr , n ) { if ( n < 2 ) return n ; let i = max_element ( arr ) ; let j = min_element ( arr ) ; if ( i > j ) { let tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; } return ( i + 1 ) * ( n - j ) ; } function max_element ( arr ) { let idx = 0 ; let max = arr [ 0 ] ; for ( let i = 1 ; i < arr . length ; i ++ ) { if ( max < arr [ i ] ) { max = arr [ i ] ; idx = i ; } } return idx ; } function min_element ( arr ) { let idx = 0 ; let min = arr [ 0 ] ; for ( let i = 1 ; i < arr . length ; i ++ ) { if ( arr [ i ] < min ) { min = arr [ i ] ; idx = i ; } } return idx ; } let arr = [ 4 , 1 , 2 , 3 ] ; let n = arr . length ; document . write ( countSubArray ( arr , n ) ) ;
Count pairs of leaf nodes in a Binary Tree which are at most K distance apart | Structure of a Node ; Constructor of the class ; Stores the count of required pairs ; Function to perform dfs to find pair of leaf nodes at most K distance apart ; Return empty array if node is null ; If node is a leaf node and return res ; Traverse to the left ; Traverse to the right ; Update the distance between left and right leaf node ; Count all pair of leaf nodes which are at most K distance apart ; Return res to parent node ; 1 / \ 2 3 / 4 ; Given distance K ; Function Call
class Node { constructor ( item ) { this . left = null ; this . right = null ; this . data = item ; } } let root ; class GFG { } let result ; function dfs ( root , distance ) { if ( root == null ) { let arr = new Array ( distance + 1 ) ; arr . fill ( 0 ) ; return arr ; } if ( root . left == null && root . right == null ) { let res = new Array ( distance + 1 ) ; res . fill ( 0 ) ; res [ 1 ] ++ ; return res ; } let left = dfs ( root . left , distance ) ; let right = dfs ( root . right , distance ) ; let res = new Array ( distance + 1 ) ; res . fill ( 0 ) ; for ( let i = res . length - 2 ; i >= 1 ; i -- ) { res [ i + 1 ] = left [ i ] + right [ i ] ; } for ( let l = 1 ; l < left . length ; l ++ ) { for ( let r = 0 ; r < right . length ; r ++ ) { if ( l + r <= distance ) { result += left [ l ] * right [ r ] ; } } } return res ; } let tree = new GFG ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; result = 0 ; let K = 3 ; dfs ( tree . root , K ) ; document . write ( result ) ;
Maximum absolute difference between any two level sum in a N | Function to find the maximum absolute difference of level sum ; Create the adjacency list ; Initialize value of maximum and minimum level sum ; Do Level order traversal keeping track of nodes at every level ; Get the size of queue when the level order traversal for one level finishes ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue the children of dequeued node ; Update the maximum level sum value ; Update the minimum level sum value ; Return the result ; Number of nodes and edges ; Edges of the N - ary tree ; Given cost ; Function call
function maxAbsDiffLevelSum ( N , M , cost , Edges ) { let adj = new Array ( N ) ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; for ( let i = 0 ; i < M ; i ++ ) { let u = Edges [ i ] [ 0 ] ; let v = Edges [ i ] [ 1 ] ; adj [ u ] . push ( v ) ; } let maxSum = cost [ 0 ] , minSum = cost [ 0 ] ; let q = [ ] ; q . push ( 0 ) ; while ( q . length != 0 ) { let count = q . length ; let sum = 0 ; while ( count -- > 0 ) { let temp = q [ 0 ] ; q . shift ( ) ; sum = sum + cost [ temp ] ; for ( let i = 0 ; i < adj [ temp ] . length ; i ++ ) { q . push ( adj [ temp ] [ i ] ) ; } } maxSum = Math . max ( sum , maxSum ) ; minSum = Math . min ( sum , minSum ) ; } document . write ( Math . abs ( maxSum - minSum ) ) ; } let N = 10 , M = 9 ; let Edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 0 , 3 ] , [ 1 , 4 ] , [ 1 , 5 ] , [ 3 , 6 ] , [ 6 , 7 ] , [ 6 , 8 ] , [ 6 , 9 ] ] ; let cost = [ 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 ] ; maxAbsDiffLevelSum ( N , M , cost , Edges ) ;
Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray | Function to find the minimum cost of splitting the array into subarrays ; Size of the array ; Get the maximum element ; dp [ ] will store the minimum cost upto index i ; Initialise the first element ; Create the frequency array ; Update the frequency ; Counting the cost of the duplicate element ; Minimum cost of operation from 0 to j ; Total cost of the array ; Driver Code ; Given cost K ; Function Call
function findMinCost ( a , k ) { var n = a . length ; var max_ele = a . reduce ( ( a , b ) => Math . max ( a , b ) ) var dp = Array ( n + 1 ) . fill ( 1000000000 ) ; dp [ 0 ] = 0 ; for ( var i = 0 ; i < n ; ++ i ) { var freq = Array ( max_ele + 1 ) . fill ( 0 ) ; for ( var j = i ; j < n ; ++ j ) { freq [ a [ j ] ] ++ ; var cost = 0 ; for ( var x = 0 ; x <= max_ele ; ++ x ) { cost += ( freq [ x ] == 1 ) ? 0 : freq [ x ] ; } dp [ j + 1 ] = Math . min ( dp [ i ] + cost + k , dp [ j + 1 ] ) ; } } return dp [ n ] ; } var arr = [ 1 , 2 , 1 , 1 , 1 ] ; var K = 2 ; document . write ( findMinCost ( arr , K ) ) ;
Find subarray with given sum | Set 2 ( Handles Negative Numbers ) | Function to print subarray with sum as given sum ; cur_sum to keep track of cummulative sum till that polet ; check whether cur_sum - sum = 0 , if 0 it means the sub array is starting from index 0 - so stop ; if hashMap already has the value , means we already have subarray with the sum - so stop ; if value is not present then add to hashmap ; if end is - 1 : means we have reached end without the sum ; Driver program
function subArraySum ( arr , n , sum ) { let cur_sum = 0 ; let start = 0 ; let end = - 1 ; let hashMap = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { cur_sum = cur_sum + arr [ i ] ; if ( cur_sum - sum == 0 ) { start = 0 ; end = i ; break ; } if ( hashMap . has ( cur_sum - sum ) ) { start = hashMap . get ( cur_sum - sum ) + 1 ; end = i ; break ; } hashMap . set ( cur_sum , i ) ; } if ( end == - 1 ) { document . write ( " " ) ; } else { document . write ( " " + start + " " + end ) ; } } let arr = [ 10 , 2 , - 2 , - 20 , 10 ] ; let n = arr . length ; let sum = - 10 ; subArraySum ( arr , n , sum ) ;
Queries to find Kth greatest character in a range [ L , R ] from a string with updates | Maximum Size of a String ; Fenwick Tree to store the frequencies of 26 alphabets ; Size of the String . ; Function to update Fenwick Tree for Character c at index val ; Add val to current node Fenwick Tree ; Move index to parent node in update View ; Function to get sum of frequencies of character c till index ; Stores the sum ; Add current element of Fenwick tree to sum ; Move index to parent node in getSum View ; Function to create the Fenwick tree ; Function to print the kth largest character in the range of l to r ; Stores the count of characters ; Stores the required character ; Calculate frequency of C in the given range ; Increase count ; If count exceeds K ; Required character found ; Function to update character at pos by character s ; 0 based index system ; Driver Code ; Makes the String 1 - based indexed ; Number of queries ; Construct the Fenwick Tree
let maxn = 100005 ; let BITree = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { BITree [ i ] = new Array ( maxn ) ; for ( let j = 0 ; j < maxn ; j ++ ) { BITree [ i ] [ j ] = 0 ; } } let N ; function update_BITree ( index , C , val ) { while ( index <= N ) { BITree [ C . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ index ] += val ; index += ( index & - index ) ; } } function sum_BITree ( index , C ) { let s = 0 ; while ( index > 0 ) { s += BITree [ C . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ index ] ; index -= ( index & - index ) ; } return s ; } function buildTree ( str ) { for ( let i = 1 ; i <= N ; i ++ ) { update_BITree ( i , str [ i ] , 1 ) ; } document . write ( " " ) ; } function printCharacter ( str , l , r , k ) { let count = 0 ; let ans = 0 ; for ( let C = ' ' . charCodeAt ( 0 ) ; C >= ' ' . charCodeAt ( 0 ) ; C -- ) { let times = sum_BITree ( r , String . fromCharCode ( C ) ) - sum_BITree ( l - 1 , String . fromCharCode ( C ) ) ; count += times ; if ( count >= k ) { ans = String . fromCharCode ( C ) ; break ; } } return ans ; } function updateTree ( str , pos , s ) { let index = pos ; update_BITree ( index , str [ index ] , - 1 ) ; str = str . substring ( 0 , index ) + s + str . substring ( index + 1 ) ; update_BITree ( index , s , 1 ) ; } let str = " " ; N = str . length ; str = ' ' + str ; let Q = 3 ; buildTree ( str ) ; document . write ( printCharacter ( str , 1 , 2 , 2 ) + " " ) ; updateTree ( str , 4 , ' ' ) ; document . write ( printCharacter ( str , 1 , 5 , 4 ) + " " ) ;
Minimum number of edges required to be removed from an Undirected Graph to make it acyclic | Stores the adjacency list ; Stores if a vertex is visited or not ; Function to perform DFS Traversal to count the number and size of all connected components ; Mark the current node as visited ; Traverse the adjacency list of the current node ; For every unvisited node ; Recursive DFS call ; Function to add undirected edge in the graph ; Function to calculate minimum number of edges to be removed ; Create Adjacency list ; Iterate over all the nodes ; Print the readonly count ; Driver Code
let vec = new Array ( 100001 ) ; let vis = new Array ( 100001 ) ; vis . fill ( false ) ; let cc = 1 ; function dfs ( node ) { vis [ node ] = true ; for ( let x = 0 ; x < vec [ node ] . length ; x ++ ) { if ( ! vis [ vec [ node ] [ x ] ] ) { cc ++ ; dfs ( vec [ node ] [ x ] ) ; } } } function addEdge ( u , v ) { vec [ u ] . push ( v ) ; vec [ v ] . push ( u ) ; } function minEdgeRemoved ( N , M , Edges ) { for ( let i = 0 ; i < M ; i ++ ) { addEdge ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } let k = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { if ( ! vis [ i ] ) { cc = 1 ; dfs ( i ) ; k ++ ; } } document . write ( ( M - N + k ) + " " ) ; } let N = 3 , M = 2 ; let Edges = [ [ 1 , 2 ] , [ 2 , 3 ] ] ; for ( let i = 0 ; i < vec . length ; i ++ ) vec [ i ] = [ ] ; minEdgeRemoved ( N , M , Edges ) ;
Check if a Binary Tree consists of a pair of leaf nodes with sum K | Stores if a pair exists or not ; Struct binary tree node ; Creates a new node ; Function to check if a pair of leaf nodes exists with sum K ; Checks if root is null ; Checks if the current node is a leaf node ; Checks for a valid pair of leaf nodes ; Insert value of current node into the set ; Traverse left and right subtree ; Construct binary tree ; Stores the leaf nodes
let pairFound = false ; class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function newNode ( data ) { let temp = new Node ( data ) ; return temp ; } function pairSum ( root , target , S ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { if ( S . has ( target - root . data ) ) { document . write ( target - root . data + " " + root . data ) ; pairFound = true ; return ; } else S . add ( root . data ) ; } pairSum ( root . left , target , S ) ; pairSum ( root . right , target , S ) ; } let root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; let S = new Set ( ) ; let K = 13 ; pairSum ( root , K , S ) ; if ( pairFound == false ) document . write ( " " ) ;
Maximum non | Function to prlet the maximum rooks and their positions ; Initialize row and col array ; Marking the location of already placed rooks ; Prlet number of non - attacking rooks that can be placed ; To store the placed rook location ; Prlet lexographically smallest order ; Size of board ; Number of rooks already placed ; Position of rooks ; Function call
function countRooks ( n , k , pos ) { let row = new Array ( n ) . fill ( 0 ) ; let col = new Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { row [ i ] = 0 ; col [ i ] = 0 ; } for ( let i = 0 ; i < k ; i ++ ) { row [ pos [ i ] [ 0 ] - 1 ] = 1 ; col [ pos [ i ] [ 1 ] - 1 ] = 1 ; } let res = n - k ; document . write ( res + " " + " " ) ; let ri = 0 , ci = 0 ; while ( res -- > 0 ) { while ( row [ ri ] == 1 ) { ri ++ ; } while ( col [ ci ] == 1 ) { ci ++ ; } document . write ( ( ri + 1 ) + " " + ( ci + 1 ) + " " + " " ) ; ri ++ ; ci ++ ; } } let N = 4 ; let K = 2 ; let pos = [ [ 1 , 4 ] , [ 2 , 2 ] ] ; countRooks ( N , K , pos ) ;
Minimum insertions to form a palindrome with permutations allowed | Function will return number of characters to be added ; To store string length ; To store number of characters occurring odd number of times ; To store count of each character ; To store occurrence of each character ; To count characters with odd occurrence ; As one character can be odd return res - 1 but if string is already palindrome return 0 ; Driver program
function minInsertion ( str ) { let n = str . length ; let res = 0 ; let count = new Array ( 26 ) ; for ( let i = 0 ; i < count . length ; i ++ ) { count [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) count [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( count [ i ] % 2 == 1 ) res ++ ; } return ( res == 0 ) ? 0 : res - 1 ; } let str = " " ; document . write ( minInsertion ( str ) ) ;
Generate a string which differs by only a single character from all given strings | Function to check if a given string differs by a single character from all the strings in an array ; Traverse over the strings ; Stores the count of characters differing from the strings ; If differs by more than one character ; Function to find the string which only differ at one position from the all given strings of the array ; Size of the array ; Length of a string ; Replace i - th character by all possible characters ; Check if it differs by a single character from all other strings ; If desired string is obtained ; Print the answer ; Driver code ; Function call
function check ( ans , s , n , m ) { for ( var i = 1 ; i < n ; ++ i ) { var count = 0 ; for ( var j = 0 ; j < m ; ++ j ) { if ( ans [ j ] !== s [ i ] [ j ] ) count ++ ; } if ( count > 1 ) return false ; } return true ; } function findString ( s ) { var n = s . length ; var ans = s [ 0 ] ; var m = ans . length ; var flag = 0 ; for ( var i = 0 ; i < m ; ++ i ) { for ( var j = 0 ; j < 26 ; ++ j ) { var x = ans ; x = x . replace ( x [ i ] , String . fromCharCode ( j + " " . charCodeAt ( 0 ) ) ) ; if ( check ( x , s , n , m ) ) { ans = x ; flag = 1 ; break ; } } if ( flag === 1 ) break ; } if ( flag === 0 ) return " " ; else return ans ; } var s = [ " " , " " ] ; document . write ( findString ( s ) ) ;
Minimum increments of Non | Function to return to the minimum number of operations required to make the array non - decreasing ; Stores the count of operations ; If arr [ i ] > arr [ i + 1 ] , add arr [ i ] - arr [ i + 1 ] to the answer Otherwise , add 0 ; Driver Code
function getMinOps ( arr ) { let ans = 0 ; for ( let i = 0 ; i < arr . length - 1 ; i ++ ) { ans += Math . max ( arr [ i ] - arr [ i + 1 ] , 0 ) ; } return ans ; } let arr = [ 1 , 3 , 1 , 2 , 4 ] ; document . write ( getMinOps ( arr ) ) ;
Maximum difference between frequency of two elements such that element having greater frequency is also greater | Return the maximum difference between frequencies of any two elements such that element with greater frequency is also greater in value . ; Finding the frequency of each element . ; finding difference such that element having greater frequency is also greater in value . ; Driver code
function maxdiff ( arr , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) freq . set ( arr [ i ] , freq . get ( arr [ i ] ) == null ? 1 : freq . get ( arr [ i ] ) + 1 ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( freq . get ( arr [ i ] ) > freq . get ( arr [ j ] ) && arr [ i ] > arr [ j ] ) ans = Math . max ( ans , freq . get ( arr [ i ] ) - freq . get ( arr [ j ] ) ) ; else if ( freq . get ( arr [ i ] ) < freq . get ( arr [ j ] ) && arr [ i ] < arr [ j ] ) ans = Math . max ( ans , freq . get ( arr [ j ] ) - freq . get ( arr [ i ] ) ) ; } } return ans ; } let arr = [ 3 , 1 , 3 , 2 , 3 , 2 ] ; let n = arr . length ; document . write ( maxdiff ( arr , n ) ) ;
Difference between highest and least frequencies in an array | Javascript Code for Difference between highest and least frequencies in an array ; sort the array ; checking consecutive elements ; Driver Code
function findDiff ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; let count = 0 , max_count = 0 , min_count = n ; for ( let i = 0 ; i < ( n - 1 ) ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { count += 1 ; continue ; } else { max_count = Math . max ( max_count , count ) ; min_count = Math . min ( min_count , count ) ; count = 0 ; } } return ( max_count - min_count ) ; } let arr = [ 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 ] ; let n = arr . length ; document . write ( findDiff ( arr , n ) ) ;
Minimum Decrements on Subarrays required to reduce all Array elements to zero | Function to count the minimum number of subarrays that are required to be decremented by 1 ; Base Case ; Initialize ans to first element ; For A [ i ] > A [ i - 1 ] , operation ( A [ i ] - A [ i - 1 ] ) is required ; Return the answer ; Driver Code
function min_operations ( A ) { if ( A . length == 0 ) return 0 ; let ans = A [ 0 ] ; for ( let i = 1 ; i < A . length ; i ++ ) { ans += Math . max ( A [ i ] - A [ i - 1 ] , 0 ) ; } return ans ; } let A = [ 1 , 2 , 3 , 2 , 1 ] ; document . write ( min_operations ( A ) ) ;
Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph | Maximum number of nodes or vertices that can be present in the graph ; Store the parent of each vertex ; Stores the size of each set ; Function to initialize the parent of each vertices ; Function to find the representative of the set which contain element v ; Path compression technique to optimize the time complexity ; Function to merge two different set into a single set by finding the representative of each set and merge the smallest set with the larger one ; Finding the set representative of each element ; Check if they have different set repersentative ; Compare the set sizes ; Assign parent of smaller set to the larger one ; Add the size of smaller set to the larger one ; Function to check the vertices are on the same set or not ; Check if they have same set representative or not ; Driver Code ; Connected vertices and taking them into single set ; Number of queries ; Function call
let MAX_NODES = 100005 ; let parent = new Array ( MAX_NODES ) ; let size_set = new Array ( MAX_NODES ) ; function make_set ( v ) { parent [ v ] = v ; size_set [ v ] = 1 ; } function find_set ( v ) { if ( v == parent [ v ] ) return v ; return parent [ v ] = find_set ( parent [ v ] ) ; } function union_set ( a , b ) { a = find_set ( a ) ; b = find_set ( b ) ; if ( a != b ) { if ( size_set [ a ] < size_set [ b ] ) { let temp = a ; a = b ; b = temp ; } parent [ b ] = a ; size_set [ a ] += size_set [ b ] ; } } function check ( a , b ) { a = find_set ( a ) ; b = find_set ( b ) ; return ( a == b ) ? " " : " " ; } let n = 5 , m = 3 ; make_set ( 1 ) ; make_set ( 2 ) ; make_set ( 3 ) ; make_set ( 4 ) ; make_set ( 5 ) ; union_set ( 1 , 3 ) ; union_set ( 3 , 4 ) ; union_set ( 3 , 5 ) ; let q = 3 ; document . write ( check ( 1 , 5 ) + " " ) ; document . write ( check ( 3 , 2 ) + " " ) ; document . write ( check ( 5 , 2 ) + " " ) ;
Count of N | Function to calculate the total count of N - digit numbers such that the sum of digits at even positions and odd positions are divisible by A and B respectively ; For single digit numbers ; Largest possible number ; Count of possible odd digits ; Count of possible even digits ; Calculate total count of sequences of length even_count with sum divisible by A where first digit can be zero ; Calculate total count of sequences of length odd_count with sum divisible by B where cannot be zero ; Return their product as answer ; Driver Code
function count ( N , A , B ) { if ( N == 1 ) { return 9 / B + 1 ; } let max_sum = 9 * N ; let odd_count = N / 2 + N % 2 ; let even_count = N - odd_count ; let dp = new Array ( even_count ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < even_count ; i ++ ) { for ( var j = 0 ; j < max_sum + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i <= 9 ; i ++ ) dp [ 0 ] [ i % A ] ++ ; for ( let i = 1 ; i < even_count ; i ++ ) { for ( let j = 0 ; j <= 9 ; j ++ ) { for ( let k = 0 ; k <= max_sum ; k ++ ) { if ( dp [ i - 1 ] [ k ] > 0 ) dp [ i ] [ ( j + k ) % A ] += dp [ i - 1 ] [ k ] ; } } } let dp1 = new Array ( odd_count ) ; for ( var i = 0 ; i < dp1 . length ; i ++ ) { dp1 [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < odd_count ; i ++ ) { for ( var j = 0 ; j < max_sum + 1 ; j ++ ) { dp1 [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= 9 ; i ++ ) dp1 [ 0 ] [ i % B ] ++ ; for ( let i = 1 ; i < odd_count ; i ++ ) { for ( let j = 0 ; j <= 9 ; j ++ ) { for ( let k = 0 ; k <= max_sum ; k ++ ) { if ( dp1 [ i - 1 ] [ k ] > 0 ) dp1 [ i ] [ ( j + k ) % B ] += dp1 [ i - 1 ] [ k ] ; } } } return dp [ even_count - 1 ] [ 0 ] * dp1 [ odd_count - 1 ] [ 0 ] ; } let N = 2 , A = 2 , B = 5 ; document . write ( count ( N , A , B ) ) ;