codeparrot/xlcost-text-to-code huggingface dataset:
Minimize cost to empty given array where cost of removing an element is its absolute difference with Time instant | JavaScript program for above approach ; Function to find the minimum cost to delete all array elements ; Sort the input array ; Store the maximum time to delete the array in the worst case ; Store the result in cost [ ] [ ] table ; Initialize the table cost [ ] [ ] ; Base Case ; Store the minimum of all cost values of the previous index ; Iterate from range [ 1 , n ] using variable i ; Update prev ; Iterate from range [ 1 , m ] using variable j ; Update cost [ i ] [ j ] ; Update the prev ; Store the minimum cost to delete all elements ; Find the minimum of all values of cost [ n ] [ j ] ; Print minimum cost ; Driver Code ; Function Call
let INF = 10000 ; function minCost ( arr , n ) { arr . sort ( ) ; let m = 2 * n ; let cost = new Array ( n + 1 ) ; for ( var i = 0 ; i < cost . length ; i ++ ) { cost [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i <= n ; i ++ ) { for ( let j = 0 ; j <= m ; j ++ ) { cost [ i ] [ j ] = INF ; } } cost [ 0 ] [ 0 ] = 0 ; let prev = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { prev = cost [ i - 1 ] [ 0 ] ; for ( let j = 1 ; j <= m ; j ++ ) { cost [ i ] [ j ] = Math . min ( cost [ i ] [ j ] , prev + Math . abs ( j - arr [ i - 1 ] ) ) ; prev = Math . min ( prev , cost [ i - 1 ] [ j ] ) ; } } let minCost = INF ; for ( let j = 1 ; j <= m ; j ++ ) { minCost = Math . min ( minCost , cost [ n ] [ j ] ) ; } document . write ( minCost ) ; } let arr = [ 4 , 2 , 4 , 4 , 5 , 2 ] ; let N = arr . length ; minCost ( arr , N ) ;
Space optimization using bit manipulations | index >> 5 corresponds to dividing index by 32 index & 31 corresponds to modulo operation of index by 32 Function to check value of bit position whether it is zero or one ; Sets value of bit for corresponding index ; Driver code ; Size that will be used is actual_size / 32 ceil is used to initialize the array with positive number ; Array is dynamically initialized as we are calculating size at run time ; Iterate through every index from a to b and call setbit ( ) if it is a multiple of 2 or 5
function checkbit ( array , index ) { return array [ index >> 5 ] & ( 1 << ( index & 31 ) ) ; } function setbit ( array , index ) { array [ index >> 5 ] |= ( 1 << ( index & 31 ) ) ; } let a = 2 , b = 10 ; let size = Math . abs ( b - a ) ; size = Math . ceil ( size / 32 ) ; let array = new Array ( size ) ; for ( let i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) setbit ( array , i - a ) ; document . write ( " " ) ; for ( let i = a ; i <= b ; i ++ ) if ( checkbit ( array , i - a ) ) document . write ( i + " " ) ;
Longest Span with same Sum in two Binary arrays | A Simple Javascript program to find longest common subarray of two binary arrays with same sum ; Returns length of the longest common sum in arr1 [ ] and arr2 [ ] . Both are of same size n . ; Initialize result ; One by one pick all possible starting points of subarrays ; Initialize sums of current subarrays ; Conider all points for starting with arr [ i ] ; Update sums ; If sums are same and current length is more than maxLen , update maxLen ; Driver Code
let arr1 = [ 0 , 1 , 0 , 1 , 1 , 1 , 1 ] ; let arr2 = [ 1 , 1 , 1 , 1 , 1 , 0 , 1 ] ; function longestCommonSum ( n ) { let maxLen = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let sum1 = 0 , sum2 = 0 ; for ( let j = i ; j < n ; j ++ ) { sum1 += arr1 [ j ] ; sum2 += arr2 [ j ] ; if ( sum1 == sum2 ) { let len = j - i + 1 ; if ( len > maxLen ) maxLen = len ; } } } return maxLen ; } document . write ( " " + " " ) ; document . write ( longestCommonSum ( arr1 . length ) ) ;
Number of Longest Increasing Subsequences | Function to count the number of LIS in the array nums [ ] ; Base Case ; Initialize dp_l array with 1 s ; Initialize dp_c array with 1 s ; If current element is smaller ; Store the maximum element from dp_l ; Stores the count of LIS ; Traverse dp_l and dp_c simultaneously ; Update the count ; Return the count of LIS ; Given array arr [ ] ; Function Call
function findNumberOfLIS ( nums ) { if ( nums . length == 0 ) return 0 ; var n = nums . length ; var dp_l = Array ( n ) . fill ( 1 ) ; var dp_c = Array ( n ) . fill ( 1 ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < i ; j ++ ) { if ( nums [ i ] <= nums [ j ] ) continue ; if ( dp_l [ j ] + 1 > dp_l [ i ] ) { dp_l [ i ] = dp_l [ j ] + 1 ; dp_c [ i ] = dp_c [ j ] ; } else if ( dp_l [ j ] + 1 == dp_l [ i ] ) dp_c [ i ] += dp_c [ j ] ; } } var max_length = 0 ; dp_l . forEach ( i => { max_length = Math . max ( i , max_length ) ; } ) ; var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( dp_l [ i ] == max_length ) count += dp_c [ i ] ; } return count ; } var arr = [ 1 , 3 , 5 , 4 , 7 ] ; document . write ( findNumberOfLIS ( arr ) ) ;
Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels | Structure of Tree node ; Function to create new Tree node ; Function to check if given binary tree satisfies the required conditions ; Queue to store nodes of each level ; Stores the current level of the binary tree ; Traverse until the queue is empty ; Stores the number of nodes present in the current level ; Insert left and right child of node into the queue ; If the level is even ; If the nodes in this level are in strictly increasing order or not ; If the level is odd ; If the nodes in this level are in strictly decreasing order or not ; Increment the level count ; Construct a Binary Tree ; Function Call
class Node { constructor ( data ) { this . left = null ; this . right = null ; this . val = data ; } } function newNode ( data ) { let temp = new Node ( data ) ; return temp ; } function checkEvenOddLevel ( root ) { if ( root == null ) return true ; let q = [ ] ; q . push ( root ) ; let level = 0 ; while ( q . length > 0 ) { let list = [ ] ; let size = q . length ; for ( let i = 0 ; i < size ; i ++ ) { let node = q [ 0 ] ; q . shift ( ) ; list . push ( node . val ) ; if ( node . left != null ) q . push ( node . left ) ; if ( node . right != null ) q . push ( node . right ) ; } if ( level % 2 == 0 ) { for ( let i = 0 ; i < list . length - 1 ; i ++ ) { if ( list [ i + 1 ] > list [ i ] ) continue ; return false ; } } else if ( level % 2 == 1 ) { for ( let i = 0 ; i < list . length - 1 ; i ++ ) { if ( list [ i + 1 ] < list [ i ] ) continue ; return false ; } } level ++ ; } return true ; } let root = null ; root = newNode ( 2 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 7 ) ; root . right . right = newNode ( 11 ) ; root . left . left . left = newNode ( 10 ) ; root . left . left . right = newNode ( 5 ) ; root . left . right . right = newNode ( 1 ) ; if ( checkEvenOddLevel ( root ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Minimum length of Run Length Encoding possible by removing at most K characters from a given string | JavaScript program to implement the above approach ; Function which solves the desired problem ; Base Case ; If the entire string has been traversed ; If precomputed subproblem occurred ; Minimum run length encoding by removing the current character ; Minimum run length encoding by retaining the current character ; If the current and the previous characters match ; Otherwise ; Function to return minimum run - length encoding for string s by removing atmost k characters ; Driver Code
let maxN = 20 ; let dp = new Array ( maxN ) ; function solve ( s , n , idx , k , last , count ) { if ( k < 0 ) return n + 1 ; if ( idx == n ) return 0 ; let ans = dp [ idx ] [ k ] [ last - ' ' . charCodeAt ( ) ] [ count ] ; if ( ans != - 1 ) return ans ; ans = n + 1 ; ans = Math . min ( ans , solve ( s , n , idx + 1 , k - 1 , last , count ) ) ; let inc = 0 ; if ( count == 1 count == 9 count == 99 ) inc = 1 ; if ( last == s [ idx ] . charCodeAt ( ) ) { ans = Math . min ( ans , inc + solve ( s , n , idx + 1 , k , s [ idx ] . charCodeAt ( ) , count + 1 ) ) ; } else { ans = Math . min ( ans , 1 + solve ( s , n , idx + 1 , k , s [ idx ] . charCodeAt ( ) , 1 ) ) ; } dp [ idx ] [ k ] [ last - ' ' . charCodeAt ( ) ] [ count ] = ans ; return dp [ idx ] [ k ] [ last - ' ' . charCodeAt ( ) ] [ count ] ; } function MinRunLengthEncoding ( s , n , k ) { for ( let i = 0 ; i < maxN ; i ++ ) { dp [ i ] = new Array ( maxN ) ; for ( let j = 0 ; j < maxN ; j ++ ) { dp [ i ] [ j ] = new Array ( 27 ) ; for ( let k = 0 ; k < 27 ; k ++ ) { dp [ i ] [ j ] [ k ] = new Array ( maxN ) ; for ( let l = 0 ; l < maxN ; l ++ ) { dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } } } return solve ( s , n , 0 , k , 123 , 0 ) ; } let S = " " ; let N = 9 , K = 2 ; document . write ( MinRunLengthEncoding ( S , N , K ) ) ;
Count unique paths is a matrix whose product of elements contains odd number of divisors | Javascript program for the above approach ; Function to calculate and store all the paths product in vector ; Base Case ; If reaches the bottom right corner and product is a perfect square ; Find square root ; If square root is an integer ; Move towards down in the matrix ; Move towards right in the matrix ; Driver Code ; Given matrix mat [ ] [ ] ; Function call
let countPaths = 0 ; function CountUniquePaths ( a , i , j , m , n , ans ) { if ( i >= m j >= n ) return ; if ( i == m - 1 && j == n - 1 ) { let sr = Math . sqrt ( ans * a [ i ] [ j ] ) ; if ( ( sr - Math . floor ( sr ) ) == 0 ) countPaths ++ ; } CountUniquePaths ( a , i + 1 , j , m , n , ans * a [ i ] [ j ] ) ; CountUniquePaths ( a , i , j + 1 , m , n , ans * a [ i ] [ j ] ) ; } let M = 3 , N = 2 ; let mat = [ [ 1 , 1 ] , [ 3 , 1 ] , [ 3 , 1 ] ] ; CountUniquePaths ( mat , 0 , 0 , M , N , 1 ) ; document . write ( countPaths ) ;
Maximize sum by selecting M elements from the start or end of rows of a Matrix | Function to select m elements having maximum sum ; Base case ; If precomputed subproblem occurred ; Either skip the current row ; Iterate through all the possible segments of current row ; Check if it is possible to select elements from i to j ; Compuete the sum of i to j as calculated ; Store the computed answer and return ; Function to precompute the prefix sum for every row of the matrix ; Preprocessing to calculate sum from i to j ; Utility function to select m elements having maximum sum ; Preprocessing step ; Initialize dp array with - 1 ; Stores maximum sum of M elements ; Given N ; Given M ; Given matrix ; Function call
function mElementsWithMaxSum ( matrix , M , block , dp ) { if ( block == matrix . length ) return 0 ; if ( dp [ block ] [ M ] != - 1 ) return dp [ block ] [ M ] ; var ans = mElementsWithMaxSum ( matrix , M , block + 1 , dp ) ; for ( var i = 0 ; i < matrix [ block ] . length ; i ++ ) { for ( var j = i ; j < matrix [ block ] . length ; j ++ ) { if ( j - i + 1 <= M ) { ans = Math . max ( ans , matrix [ block ] [ j ] - ( ( i - 1 ) >= 0 ? matrix [ block ] [ i - 1 ] : 0 ) + mElementsWithMaxSum ( matrix , M - j + i - 1 , block + 1 , dp ) ) ; } } } return dp [ block ] [ M ] = ans ; } function preComputing ( matrix , N ) { for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < matrix [ i ] . length ; j ++ ) { matrix [ i ] [ j ] = ( j > 0 ? matrix [ i ] [ j - 1 ] : 0 ) + matrix [ i ] [ j ] ; } } } function mElementsWithMaxSumUtil ( matrix , M , N ) { preComputing ( matrix , N ) ; var dp = Array . from ( Array ( N + 5 ) , ( ) => Array ( M + 5 ) . fill ( - 1 ) ) ; var sum = mElementsWithMaxSum ( matrix , M , 0 , dp ) ; document . write ( sum ) ; } var N = 3 ; var M = 4 ; var matrix = [ [ 2 , 3 , 5 ] , [ - 1 , 7 ] , [ 8 , 10 ] ] ; mElementsWithMaxSumUtil ( matrix , M , N ) ;
Find the last remaining element after repeated removal of odd and even indexed elements alternately | Function to calculate the last remaining element from the sequence ; If dp [ n ] is already calculated ; Base Case : ; Recursive call ; Return the value of dp [ n ] ; Given N ; Stores the ; Function call
function lastRemaining ( n , dp ) { if ( dp . hasOwnProperty ( n ) ) return dp [ n ] ; if ( n === 1 ) return 1 ; else dp [ n ] = 2 * ( 1 + parseInt ( n / 2 ) - lastRemaining ( parseInt ( n / 2 ) , dp ) ) ; return dp [ n ] ; } var N = 5 ; var dp = { } ; document . write ( lastRemaining ( N , dp ) ) ;
Maximum subsequence sum possible by multiplying each element by its index | Let initialize dp array ; Function to find the maximum sum of the subsequence formed ; Base Case ; If already calculated state occurs ; Include the current element ; Exclude the current element ; Update the maximum ans ; Function to calculate maximum sum of the subsequence obtained ; Letialise the dp array with - 1 ; Prlet the maximum sum possible ; Given array ; Size of the array ; Function Call
let dp = new Array ( 1005 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } function maximumSumUtil ( a , index , count , n ) { if ( index > n count > n + 1 ) { return 0 ; } if ( dp [ index ] [ count ] != - 1 ) return dp [ index ] [ count ] ; let ans1 = maximumSumUtil ( a , index + 1 , count + 1 , n ) + a [ index ] * count ; let ans2 = maximumSumUtil ( a , index + 1 , count , n ) ; return ( dp [ index ] [ count ] = Math . max ( ans1 , ans2 ) ) ; } function maximumSum ( arr , N ) { for ( let i = 0 ; i < 1005 ; i ++ ) { for ( let j = 0 ; j < 1005 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( maximumSumUtil ( arr , 0 , 1 , N - 1 ) ) ; } let arr = [ - 1 , 2 , - 10 , 4 , - 20 ] ; let N = arr . length ; maximumSum ( arr , N ) ;
Minimum Steps to obtain N from 1 by the given operations | JavaScript program to implement the above approach ; Base Case ; Recursive Call for n - 1 ; Check if n is divisible by 2 ; Check if n is divisible by 3 ; Returns a tuple ( a , b ) , where a : Minimum steps to obtain x from 1 b : Previous number ; Function that find the optimal solution ; Print the length ; Exit condition for loop = - 1 when n has reached 1 ; Return the sequence in reverse order ; Given N ; Function call
function find_sequence ( n ) { if ( n == 1 ) return [ 1 , - 1 ] ; var arr = find_sequence ( n - 1 ) ; var ans = [ arr [ 0 ] + 1 , n - 1 ] ; if ( n % 2 == 0 ) { var div_by_2 = find_sequence ( n / 2 ) ; if ( div_by_2 [ 0 ] < ans [ 0 ] ) ans = [ div_by_2 [ 0 ] + 1 , n / 2 ] ; } if ( n % 3 == 0 ) { var div_by_3 = find_sequence ( n / 3 ) ; if ( div_by_3 [ 0 ] < ans [ 0 ] ) var ans = [ div_by_3 [ 0 ] + 1 , n / 3 ] ; } return ans ; } function find_solution ( n ) { var a = find_sequence ( n ) ; document . write ( a [ 0 ] + " " ) ; var sequence = [ ] ; sequence . push ( n ) ; while ( a [ 1 ] != - 1 ) { sequence . push ( a [ 1 ] ) ; var arr = find_sequence ( a [ 1 ] ) ; a [ 1 ] = arr [ 1 ] ; } sequence . reverse ( ) ; return sequence ; } var n = 5 ; var i = find_solution ( n ) ; i . forEach ( j => { document . write ( j + " " ) ; } ) ;
Count of vessels completely filled after a given time | JavaScript program to implement the above approach ; Function to find the number of completely filled vessels ; Store the vessels ; Assuming all water is present in the vessel at the first level ; Store the number of vessel that are completely full ; Traverse all the levels ; Number of vessel at each level is j ; Calculate the exceeded amount of water ; If current vessel has less than 1 unit of water then continue ; One more vessel is full ; If left bottom vessel present ; If right bottom vessel present ; Number of levels ; Number of seconds ; Function call
var n , t ; function FindNoOfFullVessels ( n , t ) { var Matrix = Array . from ( Array ( n ) , ( ) => Array ( n ) . fill ( 0 ) ) ; Matrix [ 0 ] [ 0 ] = t * 1.0 ; var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j <= i ; j ++ ) { var exceededwater = Matrix [ i ] [ j ] - 1 ; if ( exceededwater < 0 ) continue ; ans ++ ; if ( i + 1 < n ) Matrix [ i + 1 ] [ j ] += ( exceededwater / 2 ) ; if ( i + 1 < n && j + 1 < n ) Matrix [ i + 1 ] [ j + 1 ] += ( exceededwater / 2 ) ; } } return ans ; } var N = 3 ; var T = 4 ; document . write ( FindNoOfFullVessels ( N , T ) ) ;
Sort an array in wave form | A utility method to swap two numbers . ; This function sorts arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] >= arr [ 5 ] . . ; Sort the input array ; Swap adjacent elements ; Driver code
function swap ( arr , x , y ) { let temp = arr [ x ] ; arr [ x ] = arr [ y ] ; arr [ y ] = temp } function sortInWave ( arr , n ) { arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < n - 1 ; i += 2 ) swap ( arr , i , i + 1 ) ; } let arr = [ 10 , 90 , 49 , 2 , 1 , 5 , 23 ] ; let n = arr . length ; sortInWave ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Count of maximum occurring subsequence using only those characters whose indices are in GP | Function to count maximum occurring subsequence using only those characters whose indexes are in GP ; Iterate till the length of the given string ; Update ans for 1 - length subsequence ; Update ans for 2 - length subsequence ; Return the answer ; Given string s ; Function Call
function findMaxTimes ( S ) { var arr = Array ( 26 ) . fill ( 0 ) ; var dp = Array . from ( Array ( 26 ) , ( ) => Array ( 26 ) . fill ( 0 ) ) ; for ( var i = 0 ; i < S . length ; i ++ ) { var now = S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; for ( var j = 0 ; j < 26 ; j ++ ) { dp [ j ] [ now ] += arr [ j ] ; } arr [ now ] ++ ; } var ans = 0 ; for ( var i = 0 ; i < 26 ; i ++ ) ans = Math . max ( ans , arr [ i ] ) ; for ( var i = 0 ; i < 26 ; i ++ ) { for ( var j = 0 ; j < 26 ; j ++ ) { ans = Math . max ( ans , dp [ i ] [ j ] ) ; } } return ans ; } var S = " " ; document . write ( findMaxTimes ( S ) ) ;
Sort an array in wave form | A utility method to swap two numbers . ; This function sorts arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] ... . ; Traverse all even elements ; If current even element is smaller than previous ; If current even element is smaller than next ; driver code
function swap ( arr , a , b ) { let temp = arr [ a ] ; arr [ a ] = arr [ b ] ; arr [ b ] = temp ; } function sortInWave ( arr , n ) { for ( let i = 0 ; i < n ; i += 2 ) { if ( i > 0 && arr [ i - 1 ] > arr [ i ] ) swap ( arr , i - 1 , i ) ; if ( i < n - 1 && arr [ i ] < arr [ i + 1 ] ) swap ( arr , i , i + 1 ) ; } } let arr = [ 10 , 90 , 49 , 2 , 1 , 5 , 23 ] ; let n = arr . length ; sortInWave ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;
Count all possible unique sum of series K , K + 1 , K + 2 , K + 3 , K + 4 , ... , K + N | Function to count the unique sum ; Initialize array fsum [ ] with 0 ; Initialize array rsum [ ] with 0 ; Set fsum [ 0 ] as ar [ 0 ] ; Set rsum [ 0 ] as ar [ n ] ; For each i update fsum [ i ] with ar [ i ] + fsum [ i - 1 ] ; For each i from n - 1 , update rsum [ i ] with ar [ i ] + fsum [ i + 1 ] ; K represent size of subset as explained above ; Using above relation ; Return the result ; Given a number N ; Function Call
function count_unique_sum ( n ) { let i ; let ans = 1 ; let ar = new Array ( n + 1 ) ; let fsum = new Array ( n + 1 ) ; let rsum = new Array ( n + 1 ) ; fsum . fill ( 0 ) ; rsum . fill ( 0 ) ; for ( i = 0 ; i <= n ; i ++ ) { ar [ i ] = i ; } fsum [ 0 ] = ar [ 0 ] ; rsum [ n ] = ar [ n ] ; for ( i = 1 ; i <= n ; i ++ ) { fsum [ i ] = ar [ i ] + fsum [ i - 1 ] ; } for ( i = n - 1 ; i >= 0 ; i -- ) { rsum [ i ] = ar [ i ] + rsum [ i + 1 ] ; } for ( let k = 2 ; k <= n ; k ++ ) { ans += 1 + rsum [ n + 1 - k ] - fsum [ k - 1 ] ; } return ans ; } let N = 4 ; document . write ( count_unique_sum ( N ) ) ;
Merge an array of size n into another array of size m + n | Function to move m elements at the end of array * mPlusN [ ] ; Merges array N [ ] of size n into array mPlusN [ ] of size m + n ; Current index of i / p part of mPlusN [ ] ; Current index of N [ ] ; Current index of output mPlusN [ ] ; Take an element from mPlusN [ ] if a ) value of the picked element is smaller and we have not reached end of it b ) We have reached end of N [ ] ; Otherwise take element from N [ ] ; Utility that prints out an array on a line ; Initialize arrays ; Move the m elements at the end of mPlusN ; Merge N [ ] into mPlusN [ ] ; Print the resultant mPlusN
function moveToEnd ( mPlusN , size ) { let i = 0 ; let j = size - 1 ; for ( i = size - 1 ; i >= 0 ; i -- ) { if ( mPlusN [ i ] != - 1 ) { mPlusN [ j ] = mPlusN [ i ] ; j -- ; } } } function merge ( mPlusN , N , m , n ) { let i = n ; let j = 0 ; let k = 0 ; while ( k < ( m + n ) ) { if ( ( i < ( m + n ) && mPlusN [ i ] <= N [ j ] ) || ( j == n ) ) { mPlusN [ k ] = mPlusN [ i ] ; k ++ ; i ++ ; } else { mPlusN [ k ] = N [ j ] ; k ++ ; j ++ ; } } } function printArray ( arr , size ) { let i = 0 ; for ( i = 0 ; i < size ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let mPlusN = [ 2 , 8 , - 1 , - 1 , - 1 , 13 , - 1 , 15 , 20 ] ; let N = [ 5 , 7 , 9 , 25 ] let n = N . length ; let m = mPlusN . length - n ; moveToEnd ( mPlusN , m + n ) ; merge ( mPlusN , N , m , n ) ; printArray ( mPlusN , m + n ) ;
Count of N | Function to find maximum between two numbers ; Function to find minimum between two numbers ; Function to return the count of such numbers ; For 1 - digit numbers , the count is 10 irrespective of K ; dp [ j ] stores the number of such i - digit numbers ending with j ; Stores the results of length i ; Initialize count for 1 - digit numbers ; Compute values for count of digits greater than 1 ; Find the range of allowed numbers if last digit is j ; Perform Range update ; Prefix sum to find actual count of i - digit numbers ending with j ; Update dp [ ] ; Stores the final answer ; Return the final answer ; Driver Code
function max ( num1 , num2 ) { return ( num1 > num2 ) ? num1 : num2 ; } function min ( num1 , num2 ) { return ( num1 > num2 ) ? num2 : num1 ; } function getCount ( n , k ) { if ( n == 1 ) return 10 ; var dp = Array ( 11 ) . fill ( 0 ) ; var next = Array ( 11 ) . fill ( 0 ) ; for ( var i = 1 ; i <= 9 ; i ++ ) dp [ i ] = 1 ; for ( var i = 2 ; i <= n ; i ++ ) { for ( var j = 0 ; j <= 9 ; j ++ ) { var l = Math . max ( 0 , ( j - k ) ) ; var r = Math . min ( 9 , ( j + k ) ) ; next [ l ] += dp [ j ] ; next [ r + 1 ] -= dp [ j ] ; } for ( var j = 1 ; j <= 9 ; j ++ ) next [ j ] += next [ j - 1 ] ; for ( var j = 0 ; j < 10 ; j ++ ) { dp [ j ] = next [ j ] ; next [ j ] = 0 ; } } var count = 0 ; for ( var i = 0 ; i <= 9 ; i ++ ) count += dp [ i ] ; return count ; } var n = 2 , k = 1 ; document . write ( getCount ( n , k ) ) ;
Sort 1 to N by swapping adjacent elements | Return true if array can be sorted otherwise false ; Check bool array B and sorts elements for continuous sequence of 1 ; Sort array A from i to j ; Check if array is sorted or not ; Driver Code
function sortedAfterSwap ( A , B , n ) { let i , j ; for ( i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] ) { j = i ; while ( B [ j ] ) { j ++ ; } A . sort ( ) ; i = j ; } } for ( i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) { return false ; } } return true ; } let A = [ 1 , 2 , 5 , 3 , 4 , 6 ] ; let B = [ false , true , true , true , false ] ; let n = A . length ; if ( sortedAfterSwap ( A , B , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Sort 1 to N by swapping adjacent elements | Return true if array can be sorted otherwise false ; Check if array is sorted or not ; Driver Code
function sortedAfterSwap ( A , B , n ) { let t = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] != 0 ) { if ( A [ i ] != i + 1 ) t = A [ i ] ; A [ i ] = A [ i + 1 ] ; A [ i + 1 ] = t ; } } for ( let i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) return 0 ; } return 1 ; } let A = [ 1 , 2 , 5 , 3 , 4 , 6 ] ; let B = [ 0 , 1 , 1 , 1 , 0 ] ; let n = A . length ; if ( sortedAfterSwap ( A , B , n ) == 0 ) document . write ( " " ) ; else document . write ( " " ) ;