codeparrot/xlcost-text-to-code huggingface dataset:
Sum of GCD of all possible sequences | C # 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 astatic void overflow ; Return the readonly 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
using System ; public class GFG { static int MOD = ( int ) 1e9 + 7 ; static int calculate ( int pos , int g , int n , int k ) { if ( pos == n ) { return g ; } int answer = 0 ; for ( int i = 1 ; i <= k ; i ++ ) { answer = ( answer % MOD + calculate ( pos + 1 , __gcd ( g , i ) , n , k ) % MOD ) ; answer %= MOD ; } return answer ; } static int sumofGCD ( int n , int k ) { return calculate ( 0 , 0 , n , k ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int N = 3 , K = 2 ; Console . Write ( sumofGCD ( N , K ) ) ; } }
Sum of GCD of all possible sequences | C # 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 astatic void overflow ; Find extra element which will not form gcd = i ; Find overcounting ; Remove the overcounting ; To store the readonly answer ; Return Final answer ; Driver Code ; Function call
using System ; class GFG { static int MOD = ( int ) 1e9 + 7 ; static int fastexpo ( int a , int b ) { int res = 1 ; a %= MOD ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res * a ) % MOD ; a *= a ; a %= MOD ; b >>= 1 ; } return res ; } static int sumofGCD ( int n , int k ) { int [ ] count = new int [ k + 1 ] ; for ( int g = k ; g >= 1 ; g -- ) { int count_multiples = k / g ; int temp ; temp = fastexpo ( count_multiples , n ) ; temp %= MOD ; int extra = 0 ; for ( int j = g * 2 ; j <= k ; j += g ) { extra = ( extra + count [ j ] ) ; extra %= MOD ; } count [ g ] = ( temp - extra + MOD ) ; count [ g ] %= MOD ; } int sum = 0 ; int add ; for ( int i = 1 ; i <= k ; ++ i ) { add = ( count [ i ] % MOD * i % MOD ) ; add %= MOD ; sum += add ; sum %= MOD ; } return sum ; } public static void Main ( String [ ] args ) { int N = 3 , K = 2 ; Console . Write ( sumofGCD ( N , K ) ) ; } }
Split the given string into Odds : Digit DP | C # program to split the given string into odds ; 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 into 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
using System ; class GFG { static int checkOdd ( string number ) { int n = number . Length ; int num = number [ n - 1 ] - '0' ; return ( num & 1 ) ; } static int splitIntoOdds ( string number ) { int numLen = number . Length ; int [ ] splitDP = new int [ numLen + 1 ] ; for ( int i = 0 ; i < numLen + 1 ; i ++ ) splitDP [ i ] = - 1 ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 9 && ( checkOdd ( number . Substring ( 0 , i ) ) == 1 ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( int j = 1 ; j <= 9 && i + j <= numLen ; j ++ ) { if ( checkOdd ( number . Substring ( 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 ] ; } public static void Main ( string [ ] args ) { Console . WriteLine ( splitIntoOdds ( "123456789123456789123" ) ) ; } }
Split the given string into Primes : Digit DP | C # implementation of the above approach ; 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
using System ; class GFG { static bool checkPrime ( String number ) { int num = Int32 . Parse ( number ) ; for ( int i = 2 ; i * i <= num ; i ++ ) if ( ( num % i ) == 0 ) return false ; return true ; } static int splitIntoPrimes ( String number ) { if ( number . Length == 0 ) return 0 ; if ( number . Length <= 6 && checkPrime ( number ) ) return 1 ; else { int numLen = number . Length ; int ans = 1000000 ; for ( int i = 1 ; i <= 6 && i <= numLen ; i ++ ) { if ( checkPrime ( number . Substring ( 0 , i ) ) ) { int val = splitIntoPrimes ( number . Substring ( i ) ) ; if ( val != - 1 ) { ans = Math . Min ( ans , 1 + val ) ; } } } if ( ans == 1000000 ) return - 1 ; return ans ; } } public static void Main ( String [ ] args ) { Console . Write ( splitIntoPrimes ( "13499315" ) + " STRNEWLINE " ) ; Console . Write ( splitIntoPrimes ( "43" ) + " STRNEWLINE " ) ; } }
Find the numbers from 1 to N that contains exactly k non | C # implementation of the above approach ; 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
using System ; using System . Collections ; class GFG { static int k_nonzero_numbers ( string s , int n , int k ) { int [ , , ] dp = new int [ n + 1 , 2 , k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) for ( int x = 0 ; x <= k ; x ++ ) dp [ i , j , x ] = 0 ; dp [ 0 , 0 , 0 ] = 1 ; for ( int i = 0 ; i < n ; ++ i ) { int sm = 0 ; while ( sm < 2 ) { for ( int j = 0 ; j < k + 1 ; ++ j ) { int x = 0 ; while ( x <= ( sm != 0 ? 9 : s [ i ] - '0' ) ) { if ( j + ( x > 0 ? 1 : 0 ) < k + 1 ) { dp [ i + 1 , ( ( sm != 0 || x < ( s [ i ] - '0' ) ) ? 1 : 0 ) , j + ( x > 0 ? 1 : 0 ) ] += dp [ i , sm , j ] ; } ++ x ; } } ++ sm ; } } return dp [ n , 0 , k ] + dp [ n , 1 , k ] ; } public static void Main ( string [ ] args ) { string s = "25" ; int k = 2 ; int n = s . Length ; Console . Write ( k_nonzero_numbers ( s , n , k ) ) ; } }
Count maximum occurrence of subsequence in string such that indices in subsequence is in A . P . | C # implementation to find the maximum occurrence of the subsequence such that the indices of characters are in arithmetic progression ; 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
using System ; using System . Collections . Generic ; class GFG { static int maximumOccurrence ( string s ) { int n = s . Length ; Dictionary < string , int > freq = new Dictionary < string , int > ( ) ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { string temp = " " ; temp += s [ i ] ; if ( freq . ContainsKey ( temp ) ) { freq [ temp ] ++ ; } else { freq [ temp ] = 1 ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { string temp = " " ; temp += s [ i ] ; temp += s [ j ] ; if ( freq . ContainsKey ( temp ) ) freq [ temp ] ++ ; else freq [ temp ] = 1 ; } } int answer = int . MinValue ; foreach ( KeyValuePair < string , int > it in freq ) answer = Math . Max ( answer , it . Value ) ; return answer ; } public static void Main ( string [ ] args ) { string s = " xxxyy " ; Console . Write ( maximumOccurrence ( s ) ) ; } }
Count ways to partition a string such that both parts have equal distinct characters | C # implementation to count the number of ways to partition the String such that each partition have same number of distinct characters in the String ; 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
using System ; class GFG { static int distinctChars ( string s ) { int [ ] freq = new int [ 26 ] ; int count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) freq [ s [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] > 0 ) count ++ ; } return count ; } static int waysToSplit ( string s ) { int n = s . Length ; int answer = 0 ; for ( int i = 1 ; i < n ; i ++ ) { string left = s . Substring ( 0 , i ) ; string right = s . Substring ( i , n - i ) ; if ( distinctChars ( left ) == distinctChars ( right ) ) answer ++ ; } return answer ; } public static void Main ( string [ ] args ) { string s = " ababa " ; Console . WriteLine ( waysToSplit ( s ) ) ; } }
Count ways to partition a string such that both parts have equal distinct characters | C # implementation to count the number of ways to partition the string such that each partition have same number of distinct characters in the string ; 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
using System ; public class GFG { static int waysToSplit ( string s ) { int n = s . Length ; int answer = 0 ; int [ ] prefix = new int [ n ] ; int [ ] suffix = new int [ n ] ; int [ ] seen = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { int prev = ( i - 1 >= 0 ? prefix [ i - 1 ] : 0 ) ; if ( seen [ s [ i ] - ' a ' ] == 0 ) { prefix [ i ] += ( prev + 1 ) ; } else prefix [ i ] = prev ; seen [ s [ i ] - ' a ' ] = 1 ; } for ( int i = 0 ; i < 26 ; i ++ ) seen [ i ] = 0 ; suffix [ n - 1 ] = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int prev = suffix [ i ] ; if ( seen [ s [ i ] - ' a ' ] == 0 ) { suffix [ i - 1 ] += ( prev + 1 ) ; } else suffix [ i - 1 ] = prev ; seen [ s [ i ] - ' a ' ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( prefix [ i ] == suffix [ i ] ) answer ++ ; } return answer ; } public static void Main ( string [ ] args ) { string s = " ababa " ; Console . WriteLine ( waysToSplit ( s ) ) ; } }
Counts paths from a point to reach Origin : Memory Optimized | C # implementation to count the paths from a points to origin ; Function to count the paths from a point to the origin ; Base Case when the point is already at origin ; Base Case when the point is on the x or y - axis ; Loop to fill all the position as 1 in the array ; Loop to count the number of paths from a point to origin in bottom - up manner ; Driver Code ; Function Call
using System ; class GFG { static long Count_Paths ( int x , int y ) { if ( x == 0 && y == 0 ) return 0 ; if ( x == 0 y == 0 ) return 1 ; int [ ] dp = new int [ Math . Max ( x , y ) + 1 ] ; int p = Math . Max ( x , y ) , q = Math . Min ( x , y ) ; for ( int i = 0 ; i <= p ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i <= q ; i ++ ) for ( int j = 1 ; j <= p ; j ++ ) dp [ j ] += dp [ j - 1 ] ; return dp [ p ] ; } public static void Main ( String [ ] args ) { int x = 3 , y = 3 ; Console . Write ( " Number ▁ of ▁ Paths ▁ " + Count_Paths ( x , y ) ) ; } }
Find the numbers present at Kth level of a Fibonacci Binary Tree | C # program to print the Fibonacci numbers present at K - th level of a 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 ; Driver code ; Precomputing Fibonacci numbers
using System ; class GFG { static int MAX_SIZE = 100005 ; static int [ ] fib = new int [ MAX_SIZE + 1 ] ; static void fibonacci ( ) { int i ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( i = 2 ; i <= MAX_SIZE ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } static void printLevel ( int level ) { int left_index = ( int ) Math . Pow ( 2 , level - 1 ) ; int right_index = ( int ) ( Math . Pow ( 2 , level ) - 1 ) ; for ( int i = left_index ; i <= right_index ; i ++ ) { Console . Write ( fib [ i - 1 ] + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( string [ ] args ) { fibonacci ( ) ; int K = 4 ; printLevel ( K ) ; } }
Count the number of ways to divide N in k groups incrementally | C # implementation to count the number of ways to divide N in groups such that each group has K number of elements ; 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
using System ; class GFG { static int calculate ( int pos , int prev , int left , int k ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; int answer = 0 ; for ( int i = prev ; i <= left ; i ++ ) { answer += calculate ( pos + 1 , i , left - i , k ) ; } return answer ; } static int countWaystoDivide ( int n , int k ) { return calculate ( 0 , 1 , n , k ) ; } public static void Main ( String [ ] args ) { int N = 8 ; int K = 4 ; Console . Write ( countWaystoDivide ( N , K ) ) ; } }
Count the number of ways to divide N in k groups incrementally | C # implementation to count the number of ways to divide N in groups such that each group has K number of elements ; 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
using System ; class GFG { static int [ , , ] dp = new int [ 50 , 50 , 50 ] ; static int calculate ( int pos , int prev , int left , int 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 ] ; int answer = 0 ; for ( int i = prev ; i <= left ; i ++ ) { answer += calculate ( pos + 1 , i , left - i , k ) ; } return dp [ pos , prev , left ] = answer ; } static int countWaystoDivide ( int n , int k ) { for ( int i = 0 ; i < 50 ; i ++ ) { for ( int j = 0 ; j < 50 ; j ++ ) { for ( int l = 0 ; l < 50 ; l ++ ) dp [ i , j , l ] = - 1 ; } } return calculate ( 0 , 1 , n , k ) ; } public static void Main ( String [ ] args ) { int N = 8 ; int K = 4 ; Console . Write ( countWaystoDivide ( N , K ) ) ; } }
Maximum score possible after performing given operations on an Array | C # program to find the maximum score after given operations ; 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
using System ; class GFG { static int maxScore ( int l , int r , int [ ] prefix_sum , int num ) { if ( l > r ) return 0 ; int 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 ) ) ; } static int findMaxScore ( int [ ] a , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ; } return maxScore ( 0 , n - 1 , prefix_sum , 1 ) ; } public static void Main ( String [ ] args ) { int n = 6 ; int [ ] A = { 1 , 2 , 3 , 4 , 2 , 6 } ; Console . Write ( findMaxScore ( A , n ) ) ; } }
Finding powers of any number P in N ! | C # program to find the power of P in N ! ; Map to store all the prime factors of P ; Function to find the prime factors of N im Map ; Clear map ; Check for factors of 2 ; Find all the prime factors ; If i is a factors then increase the frequency of i ; Function to find the power of prime number P in N ! ; Loop until temp <= N ; Add the number of numbers divisible by N ; Each time multiply temp by P ; Returns ans ; Function that find the powers of any P in N ! ; Find all prime factors of number P ; To store the powers of all prime factors ; Traverse the map ; Prime factor and corres . powers ; Find power of prime factor primeFac ; Divide frequency by facPow ; Store the power of primeFac ^ facPow ; Return the minimum element in Power array ; Driver 's Code ; Function to find power of P in N !
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static Dictionary < int , int > Map = new Dictionary < int , int > ( ) ; static void findPrimeFactors ( int N ) { int i ; Map . Clear ( ) ; while ( N % 2 == 0 ) { if ( Map . ContainsKey ( 2 ) ) Map [ 2 ] = Map [ 2 ] + 1 ; else Map . Add ( 2 , 1 ) ; N /= 2 ; } for ( i = 3 ; i <= Math . Sqrt ( N ) ; i += 2 ) { while ( N % i == 0 ) { if ( Map . ContainsKey ( i ) ) Map [ i ] = Map [ i ] + 1 ; else Map . Add ( i , 1 ) ; N /= i ; } } if ( N > 2 ) { if ( Map . ContainsKey ( N ) ) Map [ N ] = Map [ N ] + 1 ; else Map . Add ( N , 1 ) ; } } static int PowInFactN ( int N , int P ) { int ans = 0 ; int temp = P ; while ( temp <= N ) { ans += N / temp ; temp = temp * P ; } return ans ; } static int findPowers ( int N , int P ) { findPrimeFactors ( P ) ; List < int > Powers = new List < int > ( ) ; foreach ( KeyValuePair < int , int > it in Map ) { int primeFac = it . Key ; int facPow = it . Value ; int p = PowInFactN ( N , primeFac ) ; p /= facPow ; Powers . Add ( p ) ; } return Powers . Min ( ) ; } public static void Main ( String [ ] args ) { int N = 24 , P = 4 ; Console . Write ( findPowers ( N , P ) ) ; } }
Longest common subarray in the given two arrays | C # program to DP approach to above solution ; 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
using System ; class GFG { static int FindMaxLength ( int [ ] A , int [ ] B , int n , int m ) { int [ , ] dp = new int [ n + 1 , m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= m ; j ++ ) dp [ i , j ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( A [ i ] == B [ j ] ) dp [ j , i ] = dp [ j + 1 , i + 1 ] + 1 ; } } int maxm = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { maxm = Math . Max ( maxm , dp [ i , j ] ) ; } } return maxm ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 8 , 2 , 1 } ; int [ ] B = { 8 , 2 , 1 , 4 , 7 } ; int n = A . Length ; int m = B . Length ; Console . Write ( FindMaxLength ( A , B , n , m ) ) ; } }
Maximum contiguous decreasing sequence obtained by removing any one element | C # program to find maximum length of decreasing sequence by removing at most 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
using System ; class GFG { static int maxLength ( int [ ] a , int n ) { int maximum = 1 ; int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = 1 ; right [ i ] = 1 ; } for ( int 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 ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] < a [ i - 1 ] ) { left [ i ] = left [ i - 1 ] + 1 ; } } if ( n > 2 ) { for ( int 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 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 7 , 3 , 5 , 2 , 9 } ; int n = arr . Length ; Console . WriteLine ( maxLength ( arr , n ) ) ; } }
Longest alternating subsequence in terms of positive and negative integers | C # program to find the length of longest alternate subsequence ; 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 1 Include the current element and change pos to 0 ; Recurr for the next iteration ; If current element is negative and pos is o Include the current element and change pos to 1 ; Recurr for the next iteration ; If current element is excluded , reccur for next iteration ; Driver 's Code ; Print LAS
using System ; public class GFG { static int [ , ] LAS = new int [ 1000 , 2 ] ; static int solve ( int [ ] arr , int n , int i , int pos ) { if ( i == n ) return 0 ; if ( LAS [ i , pos ] == 1 ) return LAS [ i , pos ] ; int inc = 0 , exc = 0 ; if ( arr [ i ] > 0 && pos == 1 ) { pos = 0 ; inc = 1 + solve ( arr , n , i + 1 , pos ) ; } else if ( arr [ i ] < 0 && pos == 0 ) { pos = 1 ; 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 ] ; } public static void Main ( ) { int [ ] arr = { - 1 , 2 , 3 , 4 , 5 , - 6 , 8 , - 99 } ; int n = arr . Length ; Console . WriteLine ( Math . Max ( solve ( arr , n , 0 , 0 ) , solve ( arr , n , 0 , 1 ) ) ) ; } }