codeparrot/xlcost-text-to-code huggingface dataset:
Minimum flips required to keep all 1 s together in a Binary string | Javascript implementation for Minimum number of flips required in a binary string such that all the 1 aTMs are together ; Length of the binary string ; Initial state of the dp dp [ 0 ] [ 0 ] will be 1 if the current bit is 1 and we have to flip it ; Initial state of the dp dp [ 0 ] [ 1 ] will be 1 if the current bit is 0 and we have to flip it ; dp [ i ] [ 0 ] = Flips required to make all previous bits zero Flip required to make current bit zero ; dp [ i ] [ 1 ] = minimum flips required to make all previous states 0 or make previous states 1 satisfying the condition ; Minimum of answer and flips required to make all bits 0 ; Driver code
function minFlip ( a ) { let n = a . length ; let dp = new Array ( n + 2 ) . fill ( 0 ) . map ( ( t ) => new Array ( 2 ) . fill ( 0 ) ) if ( a . charAt ( 0 ) == ' ' ) { dp [ 0 ] [ 0 ] = 1 ; } else dp [ 0 ] [ 0 ] = 0 ; if ( a . charAt ( 0 ) == ' ' ) dp [ 0 ] [ 1 ] = 1 ; else dp [ 0 ] [ 1 ] = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( a . charAt ( i ) == ' ' ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + 1 ; } else dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ; if ( a . charAt ( i ) == ' ' ) { dp [ i ] [ 1 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + 1 ; } else dp [ i ] [ 1 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; } let answer = Number . MAX_SAFE_INTEGER ; for ( let i = 0 ; i < n ; i ++ ) { answer = Math . min ( answer , dp [ i ] [ 1 ] + dp [ n - 1 ] [ 0 ] - dp [ i ] [ 0 ] ) ; } return Math . min ( answer , dp [ n - 1 ] [ 0 ] ) ; } let s = " " ; document . write ( minFlip ( s ) ) ;
Length of longest increasing index dividing subsequence | Function to find the length of the longest increasing sub - sequence such that the index of the element is divisible by index of previous element ; Initialize the dp [ ] array with 1 as a single element will be of 1 length ; Traverse the given array ; If the index is divisible by the previous index ; If increasing subsequence identified ; Longest length is stored ; Driver code
function LIIDS ( arr , N ) { let dp = [ ] ; let ans = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { dp [ i ] = 1 ; } for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = i + i ; j <= N ; j += i ) { if ( j < arr . length && arr [ j ] > arr [ i ] ) { dp [ j ] = Math . max ( dp [ j ] , dp [ i ] + 1 ) ; } } ans = Math . max ( ans , dp [ i ] ) ; } return ans ; } let arr = [ 1 , 2 , 3 , 7 , 9 , 10 ] ; let N = arr . length ; document . write ( LIIDS ( arr , N ) ) ;
Count of subarrays having exactly K perfect square numbers | A utility function to check if the number n is perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to find number of subarrays with sum exactly equal to k ; Map to store number of subarrays starting from index zero having particular value of sum . ; To store the sum of element traverse so far ; Add current element to currsum ; If currsum = K , then a new subarray is found ; If currsum > K then find the no . of subarrays with sum currsum - K and exclude those subarrays ; Add currsum to count of different values of sum ; Return the final result ; Function to count the subarray with K perfect square numbers ; Update the array element ; If current element is perfect square then update the arr [ i ] to 1 ; Else change arr [ i ] to 0 ; Function Call ; Driver code ; Function Call
function isPerfectSquare ( x ) { let sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } function findSubarraySum ( arr , n , k ) { let prevSum = new Map ( ) ; let res = 0 ; let currsum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == K ) { res ++ ; } if ( prevSum . has ( currsum - K ) ) res += ( prevSum . get ( currsum - K ) ) ; prevSum . set ( currsum , prevSum . get ( currsum ) == null ? 1 : prevSum . get ( currsum ) + 1 ) ; } return res ; } function countSubarray ( arr , n , k ) { for ( let i = 0 ; i < n ; i ++ ) { if ( isPerfectSquare ( arr [ i ] ) ) { arr [ i ] = 1 ; } else { arr [ i ] = 0 ; } } document . write ( findSubarraySum ( arr , n , K ) ) ; } let arr = [ 2 , 4 , 9 , 2 ] ; let K = 2 ; let N = arr . length ; countSubarray ( arr , N , K ) ;
Number of ways to split N as sum of K numbers from the given range | Javascript implementation to count the number of ways to divide N in K groups such that each group has elements in range [ L , R ] ; Function to count the number of ways to divide the number N in K groups such that each group has number of elements in range [ L , R ] ; Base Case ; if N is divides completely into less than k groups ; put all possible values greater equal to prev ; Function to count the number of ways to divide the number N ; Driver Code
var mod = 1000000007 ; function calculate ( pos , left , k , L , R ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; var answer = 0 ; for ( var i = L ; i <= R ; i ++ ) { if ( i > left ) break ; answer = ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod ; } return answer ; } function countWaystoDivide ( n , k , L , R ) { return calculate ( 0 , n , k , L , R ) ; } var N = 12 ; var K = 3 ; var L = 1 ; var R = 5 ; document . write ( countWaystoDivide ( N , K , L , R ) ) ;
Check if it is possible to get given sum by taking one element from each row | Function that prints whether is it possible to make sum equal to K ; Base case ; Condition if we can make sum equal to current column by using above rows ; Iterate through current column and check whether we can make sum less than or equal to k ; Printing whether is it possible or not ; Driver Code
function PossibleSum ( n , m , v , k ) { var dp = Array . from ( Array ( n + 1 ) , ( ) => Array ( k + 1 ) . fill ( 0 ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j <= k ; j ++ ) { if ( dp [ i ] [ j ] == 1 ) { for ( var d = 0 ; d < m ; d ++ ) { if ( ( j + v [ i ] [ d ] ) <= k ) { dp [ i + 1 ] [ j + v [ i ] [ d ] ] = 1 ; } } } } } if ( dp [ n ] [ k ] == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } var N = 2 , M = 10 , K = 5 ; var arr = [ [ 4 , 0 , 15 , 3 , 2 , 20 , 10 , 1 , 5 , 4 ] , [ 4 , 0 , 10 , 3 , 2 , 25 , 4 , 1 , 5 , 4 ] ] ; PossibleSum ( N , M , arr , K ) ;
Modify array by merging elements with addition such that it consists of only Primes . | DP array to store the ans for max 20 elements ; To check whether the number is prime or not ; Function to check whether the array can be modify so that there are only primes ; If curr is prime and all elements are visited , return true ; If all elements are not visited , set curr = 0 , to search for new prime sum ; If all elements are visited ; If the current sum is not prime return false ; If this state is already calculated , return the answer directly ; Try all state of mask ; If ith index is not set ; Add the current element and set ith index and recur ; If subset can be formed then return true ; After every possibility of mask , if the subset is not formed , return false by memoizing . ; Driver code
dp = Array ( 1 << 20 ) . fill ( 0 ) ; function isprime ( n ) { if ( n == 1 ) return false ; for ( var i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } function solve ( arr , curr , mask , n ) { if ( isprime ( curr ) ) { if ( mask == ( 1 << n ) - 1 ) { return true ; } curr = 0 ; } if ( mask == ( 1 << n ) - 1 ) { if ( ! isprime ( curr ) ) { return false ; } } if ( dp [ mask ] ) return dp [ mask ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( ! ( mask & 1 << i ) ) { if ( solve ( arr , curr + arr [ i ] , mask 1 << i , n ) ) { return true ; } } } return dp [ mask ] = false ; } var arr = [ 3 , 6 , 7 , 13 ] var n = arr . length if ( solve ( arr , 0 , 0 , n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Maximum sum in circular array such that no two elements are adjacent | Set 2 | Store the maximum possible at each index ; When i exceeds the index of the last element simply return 0 ; If the value has already been calculated , directly return it from the dp array ; The next states are don 't take  this element and go to (i + 1)th state  else take this element  and go to (i + 2)th state ; function to find the max value ; subarr contains elements from 0 to arr . size ( ) - 2 ; Initializing all the values with - 1 ; Calculating maximum possible sum for first case ; subarr contains elements from 1 to arr . size ( ) - 1 ; Re - initializing all values with - 1 ; Calculating maximum possible sum for second case ; Printing the maximum between them ; Driver code
let dp = [ ] ; function maxSum ( i , subarr ) { if ( i >= subarr . length ) return 0 ; if ( dp [ i ] != - 1 ) return dp [ i ] ; dp [ i ] = Math . max ( maxSum ( i + 1 , subarr ) , subarr [ i ] + maxSum ( i + 2 , subarr ) ) ; return dp [ i ] ; } function Func ( arr ) { let subarr = arr ; subarr . pop ( ) ; dp = new Array ( subarr . length ) ; for ( let i = 0 ; i < subarr . length ; i ++ ) { dp [ i ] = - 1 ; } let max1 = maxSum ( 0 , subarr ) ; subarr = arr ; subarr . shift ( ) ; dp = [ ] ; dp = new Array ( subarr . length ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = - 1 ; } let max2 = maxSum ( 0 , subarr ) ; document . write ( Math . max ( max1 , max2 ) + " " ) ; } let arr = [ 1 , 2 , 3 , 1 ] ; Func ( arr ) ;
Count all square sub | Javascript program to count total number of k x k sub matrix whose sum is greater than the given number S ; Function to create Prefix sum matrix from the given matrix ; Store first value in table ; Initialize first row of matrix ; Initialize first column of matrix ; Initialize rest table with sum ; Utility Function to count the submatrix whose sum is greater than the S ; Loop to iterate over all the possible positions of the given matrix mat [ ] [ ] ; Condition to check , if K x K is first sub matrix ; Condition to check sub - matrix has no margin at top ; Condition when sub matrix has no margin at left ; Condition when submatrix has margin at top and left ; Increment count , If sub matrix sum is greater than S ; Function to count submatrix of size k x k such that sum if greater than or equal to S ; For loop to initialize prefix sum matrix with zero , initially ; Function to create the prefix sum matrix ; Driver Code ; Print total number of sub matrix
var dim = 5 ; function createTable ( mtrx , k , p , dp ) { dp [ 0 ] [ 0 ] = mtrx [ 0 ] [ 0 ] ; for ( var j = 1 ; j < dim ; j ++ ) { dp [ 0 ] [ j ] = mtrx [ 0 ] [ j ] + dp [ 0 ] [ j - 1 ] ; } for ( var i = 1 ; i < dim ; i ++ ) { dp [ i ] [ 0 ] = mtrx [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ; } for ( var i = 1 ; i < dim ; i ++ ) { for ( var j = 1 ; j < dim ; j ++ ) { dp [ i ] [ j ] = mtrx [ i ] [ j ] + dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ; } } } function countSubMatrixUtil ( dp , k , p ) { var count = 0 ; var subMatSum = 0 ; for ( var i = k - 1 ; i < dim ; i ++ ) { for ( var j = k - 1 ; j < dim ; j ++ ) { if ( i == ( k - 1 ) || j == ( k - 1 ) ) { if ( i == ( k - 1 ) && j == ( k - 1 ) ) { subMatSum = dp [ i ] [ j ] ; } else if ( i == ( k - 1 ) ) { subMatSum = dp [ i ] [ j ] - dp [ i ] [ j - k ] ; } else { subMatSum = dp [ i ] [ j ] - dp [ i - k ] [ j ] ; } } else { subMatSum = dp [ i ] [ j ] - dp [ i - k ] [ j ] - dp [ i ] [ j - k ] + dp [ i - k ] [ j - k ] ; } if ( subMatSum >= p ) { count ++ ; } } } return count ; } function countSubMatrix ( mtrx , k , p ) { var dp = Array . from ( Array ( dim ) , ( ) => Array ( dim ) ) ; for ( var i = 0 ; i < dim ; i ++ ) { for ( var j = 0 ; j < dim ; j ++ ) { dp [ i ] [ j ] = 0 ; } } createTable ( mtrx , k , p , dp ) ; return countSubMatrixUtil ( dp , k , p ) ; } var mtrx = [ [ 1 , 7 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 , 2 ] , [ 3 , 9 , 6 , 7 , 3 ] , [ 4 , 3 , 2 , 4 , 5 ] , [ 5 , 1 , 5 , 3 , 1 ] ] ; var k = 3 ; var p = 35 ; document . write ( countSubMatrix ( mtrx , k , p ) ) ;
Check if a substring can be Palindromic by replacing K characters for Q queries | Function to find whether String can be made palindromic or not for each queries ; To store the count of ith character of subString str [ 0. . . i ] ; Current character ; Update dp [ ] [ ] on the basis recurrence relation ; For each queries ; To store the count of distinct character ; Find occurrence of i + ' a ' ; Half the distinct Count ; If half the distinct count is less than equals to K then palindromic String can be made ; Given a String str ; Given Queries ; Function call
function canMakePaliQueries ( str , Q ) { let n = str . length ; let dp = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { dp [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = 0 ; } for ( let i = 0 ; i < 26 ; i ++ ) { let currentChar = String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; for ( let j = 0 ; j < n ; j ++ ) { if ( j == 0 ) { dp [ i ] [ j ] = ( str [ j ] == currentChar ) ? 1 : 0 ; } else { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + ( ( str [ j ] == currentChar ) ? 1 : 0 ) ; } } } for ( let query of Q . values ( ) ) { let left = query [ 0 ] ; let right = query [ 1 ] ; let k = query [ 2 ] ; let unMatchedCount = 0 ; for ( let i = 0 ; i < 26 ; i ++ ) { let occurrence = dp [ i ] [ right ] - dp [ i ] [ left ] + ( str [ left ] == ( i + ' ' . charCodeAt ( 0 ) ) ? 1 : 0 ) ; if ( occurrence % 2 == 1 ) unMatchedCount ++ ; } let ans = unMatchedCount / 2 ; if ( ans <= k ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } let str = " " ; let Q = [ [ 1 , 5 , 3 ] , [ 5 , 7 , 0 ] , [ 8 , 11 , 3 ] , [ 3 , 10 , 5 ] , [ 0 , 9 , 5 ] ] ; canMakePaliQueries ( str , Q ) ;
Maximize product of digit sum of consecutive pairs in a subsequence of length K | Javascript implementation to find the maximum product of the digit sum of the consecutive pairs of the subsequence of the length K ; Function to find the product of two numbers digit sum in the pair ; Loop to find the digits of the number ; Loop to find the digits of other number ; Function to find the subsequence of the length K ; Base Case ; Condition when we didn 't reach  the length K, but ran out of  elements of the array ; Condition if already calculated ; If length upto this point is odd ; If length is odd , it means we need second element of this current pair , calculate the product of digit sum of current and previous element and recur by moving towards next index ; If length upto this point is even ; Exclude this current element and recur for next elements . ; return by memoizing it , by selecting the maximum among two choices . ; Driver Code
const MAX = 100 ; let dp = [ ] for ( let i = 0 ; i < 1000 ; i ++ ) { let temp1 = [ ] ; for ( let j = 0 ; j < MAX ; j ++ ) { let temp2 = [ ] ; for ( let k = 0 ; k < MAX ; k ++ ) { temp2 . push ( 0 ) } temp1 . push ( temp2 ) } dp . push ( temp1 ) } function productDigitSum ( x , y ) { let sumx = 0 ; while ( x ) { sumx += ( x % 10 ) ; x = Math . floor ( x / 10 ) ; } let sumy = 0 ; while ( y ) { sumy += ( y % 10 ) ; y = Math . floor ( y / 10 ) ; } return ( sumx * sumy ) ; } function solve ( arr , i , len , prev , n , k ) { if ( len == k ) return 0 ; if ( i == n ) return Number . MIN_SAFE_INTEGER ; if ( dp [ i ] [ len ] [ prev ] ) return dp [ i ] [ len ] [ prev ] ; let inc = 0 , exc = 0 ; if ( len & 1 ) { inc = productDigitSum ( arr [ prev ] , arr [ i ] ) + solve ( arr , i + 1 , len + 1 , 0 , n , k ) ; } else { inc = solve ( arr , i + 1 , len + 1 , i , n , k ) ; } exc = solve ( arr , i + 1 , len , prev , n , k ) ; return dp [ i ] [ len ] [ prev ] = Math . max ( inc , exc ) ; } let arr = [ 10 , 5 , 9 , 101 , 24 , 2 , 20 , 14 ] ; let n = arr . length ; let k = 6 ; document . write ( solve ( arr , 0 , 0 , 0 , n , k ) ) ;
Count minimum factor jumps required to reach the end of an Array | vector to store factors of each integer ; dp array ; Precomputing all factors of integers from 1 to 100000 ; Function to count the minimum jumps ; If we reach the end of array , no more jumps are required ; If the jump results in out of index , return INT_MAX ; If the answer has been already computed , return it directly ; Else compute the answer using the recurrence relation ; Iterating over all choices of jumps ; Considering current factor as a jump ; Jump leads to the destination ; Return ans and memorize it ; pre - calculating the factors
let factors = new Array ( ) ; let dp = new Array ( 100005 ) ; for ( let i = 0 ; i < 100005 ; i ++ ) { factors . push ( new Array ( ) ) ; } function precompute ( ) { for ( let i = 1 ; i <= 100000 ; i ++ ) { for ( let j = i ; j <= 100000 ; j += i ) { factors [ j ] . push ( i ) ; } } } function solve ( arr , k , n ) { if ( k == n - 1 ) { return 0 ; } if ( k >= n ) { return Number . MAX_SAFE_INTEGER ; } if ( dp [ k ] ) { return dp [ k ] ; } let ans = Number . MAX_SAFE_INTEGER ; for ( let j of factors [ arr [ k ] ] ) { let res = solve ( arr , k + j , n ) ; if ( res != Number . MAX_SAFE_INTEGER ) { ans = Math . min ( ans , res + 1 ) ; } } return dp [ k ] = ans ; } precompute ( ) ; let arr = [ 2 , 8 , 16 , 55 , 99 , 100 ] ; let n = arr . length ; document . write ( solve ( arr , 0 , n ) ) ;
Count the number of ways to give ranks for N students such that same ranks are possible | Javascript program to calculate the number of ways to give ranks for N students such that same ranks are possible ; Initializing a table in order to store the bell triangle ; Function to calculate the K - th bell number ; If we have already calculated the bell numbers until the required N ; Base case ; First Bell Number ; If the value of the bell triangle has already been calculated ; Fill the defined dp table ; Function to return the number of ways to give ranks for N students such that same ranks are possible ; Resizing the dp table for the given value of n ; Variables to store the answer and the factorial value ; Iterating till N ; Simultaneously calculate the k ! ; Computing the K - th bell number and multiplying it with K ! ; Driver code
var mod = parseInt ( 1e9 + 7 ) ; var dp ; function f ( n , k ) { if ( n < k ) return 0 ; if ( n == k ) return 1 ; if ( k == 1 ) return 1 ; if ( dp [ n ] [ k ] != - 1 ) return dp [ n ] [ k ] ; return dp [ n ] [ k ] = ( ( k * f ( n - 1 , k ) ) % mod + ( f ( n - 1 , k - 1 ) ) % mod ) % mod ; } function operation ( n ) { dp = Array ( n + 1 ) . fill ( ) . map ( ( ) => Array ( n + 1 ) . fill ( 0 ) ) ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < n + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } var ans = 0 , fac = 1 ; for ( k = 1 ; k <= n ; k ++ ) { fac *= k ; ans = ( ans + ( fac * f ( n , k ) ) % mod ) % mod ; } return ans ; } var n = 5 ; document . write ( operation ( n ) + " " ) ;
Count the number of ordered sets not containing consecutive numbers | Function to calculate the count of ordered set for a given size ; Base cases ; Function returns the count of all ordered sets ; Prestore the factorial value ; Iterate all ordered set sizes and find the count for each one maximum ordered set size will be smaller than N as all elements are distinct and non consecutive ; Multiply ny size ! for all the arrangements because sets are ordered ; Add to total answer ; Driver code
function CountSets ( x , pos ) { if ( x <= 0 ) { if ( pos == 0 ) return 1 ; else return 0 ; } if ( pos == 0 ) return 1 ; var answer = CountSets ( x - 1 , pos ) + CountSets ( x - 2 , pos - 1 ) ; return answer ; } function CountOrderedSets ( n ) { var factorial = Array ( 10000 ) . fill ( 0 ) ; factorial [ 0 ] = 1 ; for ( i = 1 ; i < 10000 ; i ++ ) factorial [ i ] = factorial [ i - 1 ] * i ; var answer = 0 ; for ( i = 1 ; i <= n ; i ++ ) { var sets = CountSets ( n , i ) * factorial [ i ] ; answer = answer + sets ; } return answer ; } var N = 3 ; document . write ( CountOrderedSets ( N ) ) ;
Maximum sum such that exactly half of the elements are selected and no two adjacent | Function return the maximum sum possible under given condition ; Intitialising the dp table ; Base case ; Condition to select the current element ; Condition to not select the current element if possible ; Driver code
function MaximumSum ( a , n ) { let dp = [ ] ; for ( let i = 0 ; i < n + 1 ; i ++ ) dp . push ( Array ( n + 1 ) ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) { for ( let j = 0 ; j < n + 1 ; j ++ ) dp [ i ] [ j ] = - 10000 ; } for ( let i = 0 ; i < n + 1 ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = 1 ; j <= i ; j ++ ) { let val = - 10000 ; if ( ( i - 2 >= 0 && dp [ i - 2 , j - 1 ] != - 10000 ) i - 2 < 0 ) { val = a [ i - 1 ] + ( i - 2 >= 0 ? dp [ i - 2 ] [ j - 1 ] : 0 ) ; } if ( i - 1 >= j ) { val = Math . max ( val , dp [ i - 1 ] [ j ] ) ; } dp [ i ] [ j ] = val ; } } return dp [ n ] [ n / 2 ] } let A = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let N = A . length ; document . write ( MaximumSum ( A , N ) ) ;
Check if N can be converted to the form K power K by the given operation | Javascript implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if N can be converted to K power K ; Check if n is of the form k ^ k ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Return the result ; Function to check the above method ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
var kPowKform = new Set ( ) ; function func ( n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . has ( n ) ) return 1 ; var answer = 0 ; var x = n ; while ( x > 0 ) { var d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) ) { answer = 1 ; break ; } } x = parseInt ( x / 10 ) ; } return answer ; } function canBeConverted ( n ) { if ( func ( n ) ) document . write ( " " ) ; else document . write ( " " ) ; } var N = 90 ; for ( var i = 1 ; i <= 8 ; i ++ ) { var val = 1 ; for ( var j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . add ( val ) ; } canBeConverted ( N ) ;
Count of subarrays of an Array having all unique digits | Function to check whether the subarray has all unique digits ; Storing all digits occurred ; Traversing all the numbers of v ; Storing all digits of v [ i ] ; Checking whether digits of v [ i ] have already occurred ; Inserting digits of v [ i ] in the set ; Function to count the number subarray with all digits unique ; Traverse through all the subarrays ; To store elements of this subarray ; Generate all subarray and store it in vector ; Check whether this subarray has all digits unique ; Increase the count ; Return the count ; Driver code
function check ( v ) { let digits = new Set ( ) ; for ( let i = 0 ; i < v . length ; i ++ ) { let d = new Set ( ) ; while ( v [ i ] ) { d . add ( v [ i ] % 10 ) ; v [ i ] = Math . floor ( v [ i ] / 10 ) ; } for ( let it of d ) { if ( digits . has ( it ) ) return false ; } for ( let it of d ) digits . add ( it ) ; } return true ; } function numberOfSubarrays ( a , n ) { let answer = 0 ; for ( let i = 1 ; i < ( 1 << n ) ; i ++ ) { let temp = new Array ( ) ; for ( let j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) temp . push ( a [ j ] ) ; } if ( check ( temp ) ) answer ++ ; } return answer ; } let N = 4 ; let A = [ 1 , 12 , 23 , 34 ] ; document . write ( numberOfSubarrays ( A , N ) ) ;