codeparrot/xlcost-text-to-code huggingface dataset:
Find the row whose product has maximum count of prime factors | JavaScript implementation to find the row whose product has maximum number of prime factors ; function for SieveOfEratosthenes ; Create a boolean array " isPrime [ 0 . . N ] " and initialize all entries it as true . A value in isPrime [ i ] will finally be false if i is not a prime , else true . ; check if isPrime [ p ] is not changed ; Update all multiples of p ; Print all isPrime numbers ; function to display the answer ; function to Count the row number of divisors in particular row multiplication ; Find count of occurrences of each prime factor ; Compute count of all divisors ; Update row number if factors of this row is max ; Clearing map to store prime factors for next row ; Driver code
let N = 3 let M = 5 let Large = 1e6 ; let prime = new Array ( ) ; function SieveOfEratosthenes ( ) { let isPrime = new Array ( ) ; for ( let i = 0 ; i < Large + 1 ; i ++ ) { isPrime . push ( [ ] ) } isPrime . fill ( true ) ; for ( let p = 2 ; p * p <= Large ; p ++ ) { if ( isPrime [ p ] == true ) { for ( let i = p * 2 ; i <= Large ; i += p ) isPrime [ i ] = false ; } } for ( let p = 2 ; p <= Large ; p ++ ) if ( isPrime [ p ] ) prime . push ( p ) ; } function Display ( arr , row ) { for ( let i = 0 ; i < M ; i ++ ) document . write ( arr [ row ] [ i ] + " " ) ; } function countDivisorsMult ( arr ) { let mp = new Map ( ) ; let row_no = 0 ; let max_factor = 0 ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { let no = arr [ i ] [ j ] ; for ( let k = 0 ; k < prime . length ; k ++ ) { while ( no > 1 && no % prime [ k ] == 0 ) { no /= prime [ k ] ; if ( mp . has ( prime [ k ] ) ) { mp . set ( prime [ k ] , mp . get ( prime [ k ] ) + 1 ) } else { mp . set ( prime [ k ] , 1 ) } } if ( no == 1 ) break ; } } let res = 1 ; for ( let it of mp ) { res *= ( it [ 1 ] + 1 ) ; } if ( max_factor < res ) { row_no = i ; max_factor = res ; } mp . clear ( ) ; } Display ( arr , row_no ) ; } let arr = [ [ 1 , 2 , 3 , 10 , 23 ] , [ 4 , 5 , 6 , 7 , 8 ] , [ 7 , 8 , 9 , 15 , 45 ] ] ; SieveOfEratosthenes ( ) ; countDivisorsMult ( arr ) ;
Smallest N digit number whose sum of square of digits is a Perfect Square | function to check if number is a perfect square ; Function to calculate the smallest N digit number ; Place digits greater than equal to prev ; Check if placing this digit leads to a solution then return it ; Else backtrack ; Create a string representing the N digit number ; Initialise N
function isSquare ( n ) { let k = Math . floor ( Math . sqrt ( n ) ) ; return k * k == n ? 1 : 0 ; } function calculate ( pos , prev , sum , v ) { if ( pos == v . length ) return isSquare ( sum ) ; for ( let i = prev ; i <= 9 ; i ++ ) { v [ pos ] = i ; sum += i * i ; if ( calculate ( pos + 1 , i , sum , v ) != 0 ) return 1 ; sum -= ( i * i ) ; } return 0 ; } function minValue ( n ) { let v = Array . from ( { length : n } , ( _ , i ) => 0 ) ; if ( calculate ( 0 , 1 , 0 , v ) != 0 ) { let answer = " " ; for ( let i = 0 ; i < v . length ; i ++ ) answer += ( v [ i ] + 0 ) ; return answer ; } else return " " ; } let N = 2 ; document . write ( minValue ( N ) ) ;
Sum of GCD of all possible sequences | Javascript implementation of the above approach ; A recursive function that generates all the sequence and find GCD ; If we reach the sequence of length N g is the GCD of the sequence ; Initialise answer to 0 ; Placing all possible values at this position and recursively find the GCD of the sequence ; Take GCD of GCD calculated uptill now i . e . g with current element ; Take modulo to avoid overflow ; Return the final answer ; Function that finds the sum of GCD of all the subsequence of N length ; Recursive function that generates the sequence and return the GCD ; Driver Code ; Function Call
var MOD = 1000000007 ; function calculate ( pos , g , n , k ) { if ( pos == n ) { return g ; } var answer = 0 ; for ( var i = 1 ; i <= k ; i ++ ) { answer = ( answer % MOD + calculate ( pos + 1 , __gcd ( g , i ) , n , k ) % MOD ) ; answer %= MOD ; } return answer ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function sumofGCD ( n , k ) { return calculate ( 0 , 0 , n , k ) ; } var N = 3 , K = 2 ; document . write ( sumofGCD ( N , K ) ) ;
Sum of GCD of all possible sequences | JavaScript implementation of the above approach ; Function to find a ^ b in log ( b ) ; Function that finds the sum of GCD of all the subsequence of N length ; To stores the number of sequences with gcd i ; Find contribution of each gcd to happen ; To count multiples ; possible sequences with overcounting ; to avoid overflow ; Find extra element which will not form gcd = i ; Find overcounting ; Remove the overcounting ; To store the final answer ; Return Final answer ; Driver Code ; Function call
var MOD = 1000000007 ; function fastexpo ( a , b ) { var res = 1 ; a %= MOD ; while ( b ) { if ( b & 1 ) res = ( res * a ) % MOD ; a *= a ; a %= MOD ; b >>= 1 ; } return res ; } function sumofGCD ( n , k ) { var count = Array ( k + 1 ) . fill ( 0 ) ; for ( var g = k ; g >= 1 ; g -- ) { var count_multiples = k / g ; var temp ; temp = fastexpo ( count_multiples , n ) ; temp %= MOD ; var extra = 0 ; for ( var j = g * 2 ; j <= k ; j += g ) { extra = ( extra + count [ j ] ) ; extra %= MOD ; } count [ g ] = ( temp - extra + MOD ) ; count [ g ] %= MOD ; } var sum = 0 ; var add ; for ( var i = 1 ; i <= k ; ++ i ) { add = ( count [ i ] % MOD * i % MOD ) ; add %= MOD ; sum += add ; sum %= MOD ; } return sum ; } var N = 3 , K = 2 ; document . write ( sumofGCD ( N , K ) ) ;
Split the given string into Odds : Digit DP | Function to check whether a string is an odd number or not ; A function to find the minimum number of segments the given string can be divided such that every segment is a odd number ; Declare a splitdp [ ] array and initialize to - 1 ; Build the DP table in a bottom - up manner ; Initially Check if the entire prefix is odd ; If the Given Prefix can be split leto Odds then for the remaining string from i to j Check if Odd . If yes calculate the minimum split till j ; To check if the substring from i to j is a odd number or not ; If it is an odd number , then update the dp array ; Return the minimum number of splits for the entire string ; Driver code
function checkOdd ( number ) { let n = number . length ; let num = number [ n - 1 ] - ' ' ; return ( num & 1 ) ; } function splitLetoOdds ( number ) { let numLen = number . length ; let splitDP = Array . from ( { length : numLen + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < numLen + 1 ; i ++ ) splitDP [ i ] = - 1 ; for ( let i = 1 ; i <= numLen ; i ++ ) { if ( i <= 9 && ( checkOdd ( number . substr ( 0 , i ) ) == 1 ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( let j = 1 ; j <= 9 && i + j <= numLen ; j ++ ) { if ( checkOdd ( number . substr ( i , i + j ) ) == 1 ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } document . write ( splitLetoOdds ( " " ) ) ;
Split the given string into Primes : Digit DP | Function to check whether a string is a prime number or not ; A recursive function to find the minimum number of segments the given string can be divided such that every segment is a prime ; If the number is null ; checkPrime function is called to check if the number is a prime or not . ; A very large number denoting maximum ; Consider a minimum of 6 and length since the primes are less than 10 ^ 6 ; Recursively call the function to check for the remaining string ; Evaluating minimum splits into Primes for the suffix ; Checks if no combination found ; Driver code
function checkPrime ( number ) { let num = String ( number ) ; for ( let i = 2 ; i * i <= num ; i ++ ) if ( ( num % i ) == 0 ) return false ; return true ; } function splitIntoPrimes ( number ) { if ( number . length == 0 ) return 0 ; if ( number . length <= 6 && checkPrime ( number ) ) return 1 ; else { let numLen = number . length ; let ans = 1000000 ; for ( let i = 1 ; i <= 6 && i <= numLen ; i ++ ) { if ( checkPrime ( number . substr ( 0 , i ) ) ) { let val = splitIntoPrimes ( number . substr ( i ) ) ; if ( val != - 1 ) { ans = Math . min ( ans , 1 + val ) ; } } } if ( ans == 1000000 ) return - 1 ; return ans ; } } document . write ( splitIntoPrimes ( " " ) + " " ) ; document . write ( splitIntoPrimes ( " " ) + " " ) ;
Find the numbers from 1 to N that contains exactly k non | Function to find number less than N having k non - zero digits ; Store the memorised values ; Initialise ; Base ; Calculate all states For every state , from numbers 1 to N , the count of numbers which contain exactly j non zero digits is being computed and updated in the dp array . ; Return the required answer ; Driver code ; Function call
function k_nonzero_numbers ( s , n , k ) { let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] = new Array ( 2 ) ; for ( let j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = new Array ( k + 1 ) ; for ( let x = 0 ; x <= k ; x ++ ) dp [ i ] [ j ] [ x ] = 0 ; } } dp [ 0 ] [ 0 ] [ 0 ] = 1 ; for ( let i = 0 ; i < n ; ++ i ) { let sm = 0 ; while ( sm < 2 ) { for ( let j = 0 ; j < k + 1 ; ++ j ) { let x = 0 ; while ( x <= ( sm != 0 ? 9 : s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) { if ( j + ( x > 0 ? 1 : 0 ) < k + 1 ) { dp [ i + 1 ] [ ( sm != 0 || x < ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ? 1 : 0 ] [ j + ( x > 0 ? 1 : 0 ) ] += dp [ i ] [ sm ] [ j ] ; } ++ x ; } } ++ sm ; } } return dp [ n ] [ 0 ] [ k ] + dp [ n ] [ 1 ] [ k ] ; } let s = " " ; let k = 2 ; let n = s . length ; document . write ( k_nonzero_numbers ( s , n , k ) ) ;
Count maximum occurrence of subsequence in string such that indices in subsequence is in A . P . | Function to find the maximum occurrence of the subsequence such that the indices of characters are in arithmetic progression ; Frequencies of subsequence ; Loop to find the frequencies of subsequence of length 1 ; Loop to find the frequencies subsequence of length 2 ; Finding maximum frequency ; Driver Code
function maximumOccurrence ( s ) { var n = s . length ; var freq = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { var temp = " " ; temp += s [ i ] ; if ( freq . has ( temp ) ) freq . set ( temp , freq . get ( temp ) + 1 ) else freq . set ( temp , 1 ) } for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { var temp = " " ; temp += s [ i ] ; temp += s [ j ] ; if ( freq . has ( temp ) ) freq . set ( temp , freq . get ( temp ) + 1 ) else freq . set ( temp , 1 ) } } var answer = - 1000000000 ; freq . forEach ( ( value , key ) => { answer = Math . max ( answer , value ) ; } ) ; return answer ; } var s = " " ; document . write ( maximumOccurrence ( s ) ) ;
Count ways to partition a string such that both parts have equal distinct characters | Function to count the distinct characters in the string ; Frequency of each character ; Loop to count the frequency of each character of the string ; If frequency is greater than 0 then the character occured ; Function to count the number of ways to partition the string such that each partition have same number of distinct character ; Loop to choose the partition index for the string ; Divide in two parts ; Check whether number of distinct characters are equal ; Driver Code
function distinctChars ( s ) { let freq = [ ] ; for ( let i = 0 ; i < 26 ; i ++ ) freq . push ( 0 ) ; let count = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) freq [ s . charCodeAt ( i ) - 97 ] ++ ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] > 0 ) count ++ ; } return count ; } function waysToSplit ( s ) { let n = s . length ; let answer = 0 ; for ( let i = 1 ; i < n ; i ++ ) { let left = s . substr ( 0 , i ) ; let right = s . substr ( i , n - i ) ; if ( distinctChars ( left ) == distinctChars ( right ) ) answer ++ ; } return answer ; } let s = " " ; document . write ( waysToSplit ( s ) ) ;
Count ways to partition a string such that both parts have equal distinct characters | Function to count the number of ways to partition the string such that each partition have same number of distinct character ; Prefix and suffix array for distinct character from start and end ; To check whether a character has appeared till ith index ; Calculating prefix array ; Character appears for the first time in string ; Character is visited ; Resetting seen for suffix calculation ; Calculating the suffix array ; Character appears for the first time ; This character has now appeared ; Loop to calculate the number partition points in the string ; Check whether number of distinct characters are equal ; Driver Code
function waysToSplit ( s ) { var n = s . length ; var answer = 0 ; var prefix = Array ( n ) . fill ( 0 ) ; var suffix = Array ( n ) . fill ( 0 ) ; var seen = Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { var prev = ( i - 1 >= 0 ? prefix [ i - 1 ] : 0 ) ; if ( seen [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) { prefix [ i ] += ( prev + 1 ) ; } else prefix [ i ] = prev ; seen [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 1 ; } seen = Array ( 26 ) . fill ( 0 ) ; suffix [ n - 1 ] = 0 ; for ( var i = n - 1 ; i >= 1 ; i -- ) { var prev = suffix [ i ] ; if ( seen [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) { suffix [ i - 1 ] += ( prev + 1 ) ; } else suffix [ i - 1 ] = prev ; seen [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 1 ; } for ( var i = 0 ; i < n ; i ++ ) { if ( prefix [ i ] == suffix [ i ] ) answer ++ ; } return answer ; } var s = " " ; document . write ( waysToSplit ( s ) ) ;
Find the numbers present at Kth level of a Fibonacci Binary Tree | Initializing the max value ; Array to store all the fibonacci numbers ; Function to generate fibonacci numbers using Dynamic Programming ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous two numbers in the series and store it ; Function to print the Fibonacci numbers present at Kth level of a Binary Tree ; Finding the left and right index ; Iterating and printing the numbers ; Precomputing Fibonacci numbers
var MAX_SIZE = 100005 ; var fib = Array ( MAX_SIZE + 1 ) . fill ( 0 ) ; function fibonacci ( ) { var i ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( i = 2 ; i <= MAX_SIZE ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } function printLevel ( level ) { var left_index = parseInt ( Math . pow ( 2 , level - 1 ) ) ; var right_index = parseInt ( ( Math . pow ( 2 , level ) - 1 ) ) ; for ( i = left_index ; i <= right_index ; i ++ ) { document . write ( fib [ i - 1 ] + " " ) ; } document . write ( ) ; } fibonacci ( ) ; var K = 4 ; printLevel ( K ) ;
Count the number of ways to divide N in k groups incrementally | Function to count the number of ways to divide the number N in groups such that each group has K number of elements ; 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
function calculate ( pos , prev , left , k ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; let answer = 0 ; for ( let i = prev ; i <= left ; i ++ ) { answer += calculate ( pos + 1 , i , left - i , k ) ; } return answer ; } function countWaystoDivide ( n , k ) { return calculate ( 0 , 1 , n , k ) ; } let N = 8 ; let K = 4 ; document . write ( countWaystoDivide ( N , K ) ) ;
Count the number of ways to divide N in k groups incrementally | DP Table ; Function to count the number of ways to divide the number N in groups such that each group has K number of elements ; Base Case ; if N is divides completely into less than k groups ; If the subproblem has been solved , use the value ; put all possible values greater equal to prev ; Function to count the number of ways to divide the number N in groups ; Initialize DP Table as - 1 ; Driver Code
let dp = new Array ( 500 ) ; for ( let i = 0 ; i < 500 ; i ++ ) { dp [ i ] = new Array ( 500 ) ; for ( let j = 0 ; j < 500 ; j ++ ) { dp [ i ] [ j ] = new Array ( 500 ) ; for ( let k = 0 ; k < 500 ; k ++ ) dp [ i ] [ j ] [ k ] = 0 ; } } function calculate ( pos , prev , left , k ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; if ( dp [ pos ] [ prev ] [ left ] != - 1 ) return dp [ pos ] [ prev ] [ left ] ; let answer = 0 ; for ( let i = prev ; i <= left ; i ++ ) { answer += calculate ( pos + 1 , i , left - i , k ) ; } return dp [ pos ] [ prev ] [ left ] = answer ; } function countWaystoDivide ( n , k ) { for ( let i = 0 ; i < 500 ; i ++ ) { for ( let j = 0 ; j < 500 ; j ++ ) { for ( let l = 0 ; l < 500 ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } return calculate ( 0 , 1 , n , k ) ; } let N = 8 ; let K = 4 ; document . write ( countWaystoDivide ( N , K ) ) ;
Maximum score possible after performing given operations on an Array | Function to calculate maximum score recursively ; Base case ; Sum of array in range ( l , r ) ; If the operation is even - numbered the score is decremented ; Exploring all paths , by removing leftmost and rightmost element and selecting the maximum value ; Function to find the max score ; Prefix sum array ; Calculating prefix_sum ; Driver code
function maxScore ( l , r , prefix_sum , num ) { if ( l > r ) return 0 ; let current_sum = prefix_sum [ r ] - ( l - 1 >= 0 ? prefix_sum [ l - 1 ] : 0 ) ; if ( num % 2 == 0 ) current_sum *= - 1 ; return current_sum + Math . max ( maxScore ( l + 1 , r , prefix_sum , num + 1 ) , maxScore ( l , r - 1 , prefix_sum , num + 1 ) ) ; } function findMaxScore ( a , n ) { let prefix_sum = new Uint8Array ( n ) ; prefix_sum [ 0 ] = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ; } return maxScore ( 0 , n - 1 , prefix_sum , 1 ) ; } let n = 6 ; let A = [ 1 , 2 , 3 , 4 , 2 , 6 ] ; document . write ( findMaxScore ( A , n ) ) ;
Longest common subarray in the given two arrays | Function to find the maximum length of equal subarray ; Auxiliary dp [ ] [ ] array ; Updating the dp [ ] [ ] table in Bottom Up approach ; If A [ i ] is equal to B [ i ] then dp [ j ] [ i ] = dp [ j + 1 ] [ i + 1 ] + 1 ; Find maximum of all the values in dp [ ] [ ] array to get the maximum length ; Update the length ; Return the maximum length ; Driver Code ; Function call to find maximum length of subarray
function FindMaxLength ( A , B , n , m ) { let dp = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) { dp [ i ] = new Array ( m + 1 ) ; for ( let j = 0 ; j <= m ; j ++ ) dp [ i ] [ j ] = 0 ; } for ( let i = n - 1 ; i >= 0 ; i -- ) { for ( let j = m - 1 ; j >= 0 ; j -- ) { if ( A [ i ] == B [ j ] ) dp [ j ] [ i ] = dp [ j + 1 ] [ i + 1 ] + 1 ; } } let maxm = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { maxm = Math . max ( maxm , dp [ i ] [ j ] ) ; } } return maxm ; } let A = [ 1 , 2 , 8 , 2 , 1 ] ; let B = [ 8 , 2 , 1 , 4 , 7 ] ; let n = A . length ; let m = B . length ; document . write ( FindMaxLength ( A , B , n , m ) ) ;
Maximum contiguous decreasing sequence obtained by removing any one element | Function to find the maximum length ; Initialise maximum length to 1 ; Initialise left [ ] to find the length of decreasing sequence from left to right ; Initialise right [ ] to find the length of decreasing sequence from right to left ; Initially store 1 at each index of left and right array ; Iterate over the array arr [ ] to store length of decreasing sequence that can be obtained at every index in the right array ; Store the length of longest continuous decreasing sequence in maximum ; Iterate over the array arr [ ] to store length of decreasing sequence that can be obtained at every index in the left array ; Check if we can obtain a longer decreasing sequence after removal of any element from the array arr [ ] with the help of left [ ] & right [ ] ; Return maximum length of sequence ; Driver code ; Function calling
function maxLength ( a , n ) { let maximum = 1 ; let left = new Array ( n ) ; let right = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { left [ i ] = 1 ; right [ i ] = 1 ; } for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] > a [ i + 1 ] ) { right [ i ] = right [ i + 1 ] + 1 ; } maximum = Math . max ( maximum , right [ i ] ) ; } for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] < a [ i - 1 ] ) { left [ i ] = left [ i - 1 ] + 1 ; } } if ( n > 2 ) { for ( let i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i - 1 ] > a [ i + 1 ] ) { maximum = Math . max ( maximum , left [ i - 1 ] + right [ i + 1 ] ) ; } } } return maximum ; } let arr = [ 8 , 7 , 3 , 5 , 2 , 9 ] ; let n = arr . length ; document . write ( maxLength ( arr , n ) + " " ) ;
Longest alternating subsequence in terms of positive and negative integers | LAS [ i ] [ pos ] array to find the length of LAS till index i by including or excluding element arr [ i ] on the basis of value of pos ; Base Case ; If current element is positive and pos is true Include the current element and change pos to false ; Recurr for the next iteration ; If current element is negative and pos is false Include the current element and change pos to true ; Recurr for the next iteration ; If current element is excluded , reccur for next iteration ; Driver 's Code ; Print LAS
let LAS = new Array ( ) ; for ( let i = 0 ; i < 1000 ; i ++ ) { let temp = new Array ( ) for ( let j = 0 ; j < 2 ; j ++ ) { temp . push ( [ ] ) } LAS . push ( temp ) ; } for ( let i = 0 ; i < 1000 ; i ++ ) { for ( let j = 0 ; j < 2 ; j ++ ) { LAS [ i ] [ j ] = false } } function solve ( arr , n , i , pos ) { if ( i == n ) return 0 ; if ( LAS [ i ] [ pos ] ) return LAS [ i ] [ pos ] ; let inc = 0 , exc = 0 ; if ( arr [ i ] > 0 && pos == true ) { pos = false ; inc = 1 + solve ( arr , n , i + 1 , pos ) ; } else if ( arr [ i ] < 0 && pos == false ) { pos = true ; inc = 1 + solve ( arr , n , i + 1 , pos ) ; } exc = solve ( arr , n , i + 1 , pos ) ; LAS [ i ] [ pos ] = Math . max ( inc , exc ) ; return LAS [ i ] [ pos ] ; } let arr = [ - 1 , 2 , 3 , 4 , 5 , - 6 , 8 , - 99 ] ; let n = arr . length ; document . write ( Math . max ( solve ( arr , n , 0 , 0 ) , solve ( arr , n , 0 , 1 ) ) ) ;
Number of subsequences with negative product | Function to return the count of all the subsequences with negative product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; Driver code
function cntSubSeq ( arr , n ) { var pos_count = 0 ; var neg_count = 0 ; var result ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = Math . pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= Math . pow ( 2 , neg_count - 1 ) ; else result = 0 ; return result ; } var arr = [ 3 , - 4 , - 1 , 6 ] ; var n = arr . length ; document . write ( cntSubSeq ( arr , n ) ) ;