codeparrot/xlcost-text-to-code huggingface dataset:
Check if a large number is divisible by a number which is a power of 2 | C # program for the above approach ; Function to check divisibility ; Calculate the number of digits in num ; Check if the length of the string is less than the powerOf2 then return false ; Check if the powerOf2 is 0 that means the given number is 1 and as every number is divisible by 1 so return true ; Find the number which is formed by the last n digits of the string where n = powerOf2 ; Check if the number formed is divisible by input num or not ; Driver Code ; Given number ; Function call
using System ; class GFG { static bool checkIfDivisible ( String str , long num ) { long powerOf2 = ( int ) ( Math . Log ( num ) / Math . Log ( 2 ) ) ; if ( str . Length < powerOf2 ) return false ; if ( powerOf2 == 0 ) return true ; long i , number = 0 ; int len = str . Length ; for ( i = len - powerOf2 ; i < len ; i ++ ) { number += ( long ) ( ( str [ ( int ) i ] - '0' ) * Math . Pow ( 10 , powerOf2 - 1 ) ) ; powerOf2 -- ; } if ( number % num != 0 ) return false ; else return true ; } public static void Main ( String [ ] args ) { String str = "213467756564" ; long num = 4 ; if ( checkIfDivisible ( str , num ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction | C # program to check clockwise or counterclockwise order in an array ; Comparing the first and last value of array ; If the Count is greater than 1 then it can 't be  represented in required order ; Driver code
using System ; class GFG { static bool check_order ( int [ ] arr ) { int cnt = 0 ; for ( int i = 0 ; i < arr . Length - 1 ; i ++ ) { if ( Math . Abs ( arr [ i + 1 ] - arr [ i ] ) > 1 ) cnt ++ ; } if ( Math . Abs ( arr [ 0 ] - arr [ arr . Length - 1 ] ) > 1 ) cnt ++ ; if ( cnt > 1 ) return false ; return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 1 } ; if ( check_order ( arr ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
Largest number M less than N such that XOR of M and N is even | C # program for the above problem . ; Function to find the maximum possible value of M ; Edge case ; M = N - 2 is maximum possible value ; Driver code
using System ; class GFG { static int getM ( int n ) { if ( n == 1 ) return - 1 ; else return n - 2 ; } static void Main ( ) { int n = 10 ; Console . Write ( getM ( n ) ) ; } }
Maximize profit in buying and selling stocks with Rest condition | C # program for the above problem ; If there is only one day for buying and selling no profit can be made ; Array to store Maxprofit by resting on given day ; Array to store Maxprofit by buying or resting on the given day ; Array to store Maxprofit by selling on given day ; Initially there will 0 profit ; Buying on 1 st day results in negative profit ; Zero profit since selling before buying isn 't possible ; max of profit on ( i - 1 ) th day by resting and profit on ( i - 1 ) th day by selling . ; max of profit by resting on ith day and buying on ith day . ; max of profit by selling on ith day ; maxprofit ; Driver code
using System ; class GFG { static int maxProfit ( int [ ] prices , int n ) { if ( n <= 1 ) return 0 ; int [ ] rest = new int [ n ] ; int [ ] hold = new int [ 9 ] ; int [ ] sold = new int [ 9 ] ; rest [ 0 ] = 0 ; hold [ 0 ] = - prices [ 0 ] ; sold [ 0 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { rest [ i ] = Math . Max ( rest [ i - 1 ] , sold [ i - 1 ] ) ; hold [ i ] = Math . Max ( hold [ i - 1 ] , rest [ i - 1 ] - prices [ i ] ) ; sold [ i ] = hold [ i - 1 ] + prices [ i ] ; } return Math . Max ( rest [ n - 1 ] , sold [ n - 1 ] ) ; } static void Main ( ) { int [ ] price = { 2 , 4 , 5 , 0 , 2 } ; int n = price . Length ; Console . WriteLine ( maxProfit ( price , n ) ) ; } }
Count of subarrays of size K having at least one pair with absolute difference divisible by K | C # implementation of the above approach ; Function to return the required number of subarrays ; Return number of possible subarrays of length K ; Driver Code
using System ; class GFG { static int findSubarrays ( int [ ] arr , int N , int K ) { return N - K + 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 2 , 17 , 18 } ; int K = 4 ; int N = arr . Length ; Console . Write ( findSubarrays ( arr , N , K ) ) ; } }
Maximum length of subarray such that all elements are equal in the subarray | C # program for the above approach ; Function to find the longest subarray with same element ; Check if the elements are same then we can increment the length ; Reinitialize j ; Compare the maximum length e with j ; Return max length ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static int longest_subarray ( int [ ] arr , int d ) { int i = 0 , j = 1 , e = 0 ; for ( i = 0 ; i < d - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { j = j + 1 ; } else { j = 1 ; } if ( e < j ) { e = j ; } } return e ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . Write ( longest_subarray ( arr , N ) ) ; } }
Print all Strong numbers less than or equal to N | C # program for the above approach ; Store the factorial of all the digits from [ 0 , 9 ] ; Function to return true if number is strong or not ; Converting N to String so that can easily access all it 's digit ; sum will store summation of factorial of all digits of a number N ; Returns true of N is strong number ; Function to print all strong number till N ; Iterating from 1 to N ; Checking if a number is strong then print it ; Driver Code ; Given Number ; Function Call
using System ; class GFG { static int [ ] factorial = { 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 } ; public static bool isStrong ( int N ) { String num = N . ToString ( ) ; int sum = 0 ; for ( int i = 0 ; i < num . Length ; i ++ ) { sum += factorial [ int . Parse ( num [ i ] + " " ) ] ; } return sum == N ; } public static void printStrongNumbers ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( isStrong ( i ) ) { Console . Write ( i + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int N = 1000 ; printStrongNumbers ( N ) ; } }
Count of pairs in a given range with sum of their product and sum equal to their concatenated number | C # program to count all the possible pairs with X * Y + ( X + Y ) equal to number formed by concatenating X and Y ; Function for counting pairs ; Count possible values of Y ; Driver Code
using System ; class GFG { static int countPairs ( int A , int B ) { int countY = 0 , countX = ( B - A ) + 1 , next_val = 9 ; while ( next_val <= B ) { if ( next_val >= A ) { countY += 1 ; } next_val = next_val * 10 + 9 ; } return ( countX * countY ) ; } public static void Main ( ) { int A = 1 ; int B = 16 ; Console . Write ( countPairs ( A , B ) ) ; } }
Minimum sprinklers required to water a rectangular park | C # program to find minimum number sprinklers required to cover the park ; Function to find the minimum number sprinklers required to water the park . ; General requirements of sprinklers ; If M is odd then add one additional sprinklers ; Driver code
using System ; class GFG { public static int solve ( int n , int m ) { int ans = n * ( m / 2 ) ; if ( m % 2 == 1 ) { ans += ( n + 1 ) / 2 ; } return ans ; } public static void Main ( String [ ] args ) { int N = 5 ; int M = 3 ; Console . WriteLine ( solve ( N , M ) ) ; } }
Queries to find frequencies of a string within specified substrings | C # program to find frequency of a string K in a substring [ L , R ] in S ; Store the frequency of string for each index ; Compute and store frequencies for every index ; Driver code
using System . IO ; using System ; class GFG { static int max_len = 100005 ; static int [ ] cnt = new int [ max_len ] ; static void precompute ( string s , string K ) { int n = s . Length ; for ( int i = 0 ; i < n - 2 ; i ++ ) { cnt [ i + 1 ] = cnt [ i ] ; if ( s . Substring ( i , K . Length ) . Equals ( K ) ) { cnt [ i + 1 ] += 1 ; } } cnt [ n - 2 + 1 ] = cnt [ n - 2 ] ; } static void Main ( ) { string s = " ABCABCABABC " ; string K = " ABC " ; precompute ( s , K ) ; int [ , ] Q = { { 1 , 6 } , { 5 , 11 } } ; for ( int it = 0 ; it < Q . GetLength ( 0 ) ; it ++ ) { Console . WriteLine ( cnt [ Q [ it , 1 ] - 1 ] - cnt [ Q [ it , 0 ] - 1 ] ) ; } } }
XOR of elements in a given range with updates using Fenwick Tree | C # program to find XOR of elements in given range [ L , R ] . ; Returns XOR of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial XORs of array elements are stored in BITree [ ] . ; Traverse ancestors of BITree [ index ] ; XOR current element of BIT to ans ; Update index to that of the parent node in getXor ( ) view by subtracting LSB ( Least Significant Bit ) ; Updates the Binary Index Tree by replacing all ancestors of index by their respective XOR with val ; Traverse all ancestors and XOR with ' val ' . ; XOR ' val ' to current node of BIT ; Update index to that of the parent node in updateBit ( ) view by adding LSB ( Least Significant Bit ) ; Constructs and returns a Binary Indexed Tree for the given array ; Create and initialize the Binary Indexed Tree ; Store the actual values in BITree [ ] using update ( ) ; Driver Code ; Create the Binary Indexed Tree ; Solve each query in Q ; Update the values of all ancestors of idx
using System ; class GFG { static int getXOR ( int [ ] BITree , int index ) { int ans = 0 ; index += 1 ; while ( index > 0 ) { ans ^= BITree [ index ] ; index -= index & ( - index ) ; } return ans ; } static void updateBIT ( int [ ] BITree , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] ^= val ; index += index & ( - index ) ; } } static int [ ] constructBITree ( int [ ] arr , int n ) { int [ ] BITree = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) updateBIT ( BITree , n , i , arr [ i ] ) ; return BITree ; } static int rangeXor ( int [ ] BITree , int l , int r ) { return getXOR ( BITree , r ) ^ getXOR ( BITree , l - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 1 , 1 , 3 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = A . Length ; int [ , ] q = { { 1 , 0 , 9 } , { 2 , 3 , 6 } , { 2 , 5 , 5 } , { 2 , 8 , 1 } , { 1 , 0 , 9 } } ; int [ ] BITree = constructBITree ( A , n ) ; for ( int i = 0 ; i < q . GetLength ( 0 ) ; i ++ ) { int id = q [ i , 0 ] ; if ( id == 1 ) { int L = q [ i , 1 ] ; int R = q [ i , 2 ] ; Console . Write ( " XOR ▁ of ▁ elements ▁ " + " in ▁ given ▁ range ▁ is ▁ " + rangeXor ( BITree , L , R ) + " STRNEWLINE " ) ; } else { int idx = q [ i , 1 ] ; int val = q [ i , 2 ] ; A [ idx ] ^= val ; updateBIT ( BITree , n , idx , val ) ; } } } }
Count of Double Prime numbers in a given range L to R | C # program to find the count of Double Prime numbers in the range L to R ; Array to make Sieve where arr [ i ] = 0 indicates non prime and arr [ i ] = 1 indicates prime ; Array to find double prime ; Function to find the number double prime numbers in range ; Assume all numbers as prime ; Check if the number is prime ; check for multiples of i ; Make all multiples of ith prime as non - prime ; Check if number at ith position is prime then increment count ; Indicates count of numbers from 1 to i that are also prime and hence double prime ; If number is not a double prime ; finding cumulative sum ; Driver code
using System ; class GFG { static int [ ] arr = new int [ 1000001 ] ; static int [ ] dp = new int [ 1000001 ] ; static void count ( ) { int maxN = 1000000 , i , j ; for ( i = 0 ; i < maxN ; i ++ ) arr [ i ] = 1 ; arr [ 0 ] = 0 ; arr [ 1 ] = 0 ; for ( i = 2 ; i * i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) { for ( j = 2 * i ; j <= maxN ; j += i ) { arr [ j ] = 0 ; } } } int cnt = 0 ; for ( i = 0 ; i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) cnt ++ ; if ( arr [ cnt ] == 1 ) dp [ i ] = 1 ; else dp [ i ] = 0 ; } for ( i = 1 ; i <= maxN ; i ++ ) dp [ i ] += dp [ i - 1 ] ; } public static void Main ( ) { int L = 4 , R = 12 ; count ( ) ; Console . Write ( dp [ R ] - dp [ L - 1 ] ) ; } }
Count of ungrouped characters after dividing a string into K groups of distinct characters | C # code to implement the above approach ; Create array where index represents alphabets ; Fill count of every alphabet to corresponding array index ; Count for every element how much is exceeding from no . of groups then sum them ; Print answer ; Driver code
using System ; class GFG { static void findUngroupedElement ( String s , int k ) { int n = s . Length ; int [ ] b = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) b [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char p = s [ i ] ; b [ p - ' a ' ] += 1 ; } int sum = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( b [ i ] > k ) sum += b [ i ] - k ; } Console . WriteLine ( sum ) ; } public static void Main ( String [ ] srgs ) { String s = " stayinghomesaveslife " ; int k = 1 ; findUngroupedElement ( s , k ) ; } }
Generate an array of given size with equal count and sum of odd and even numbers | C # implementation to find the array containing same count of even and odd elements with equal sum of even and odd elements ; Function to find the array such that the array contains the same count of even and odd elements with equal sum of even and odd elements ; Length of array which is not divisible by 4 is unable to form such array ; Loop to find the resulted array containing the same count of even and odd elements ; Find the total sum of even elements ; Find the total sum of odd elements ; Find the difference between the total sum of even and odd elements ; The difference will be added in the last odd element ; Driver Code
using System ; class GFG { static void findSolution ( int N ) { if ( N % 4 != 0 ) Console . Write ( - 1 + " STRNEWLINE " ) ; else { int temp = 0 , sum_odd = 0 ; int sum_even = 0 ; int [ ] result = new int [ N ] ; for ( int i = 0 ; i < N ; i += 2 ) { temp += 2 ; result [ i + 1 ] = temp ; sum_even += result [ i + 1 ] ; result [ i ] = temp - 1 ; sum_odd += result [ i ] ; } int diff = sum_even - sum_odd ; result [ N - 2 ] += diff ; for ( int i = 0 ; i < N ; i ++ ) Console . Write ( result [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int N = 8 ; findSolution ( N ) ; } }
Number of ways to place two queens on a N * N chess | C # implementation to find the number of ways to place two queens on the N * N chess board ; Function to find number of valid positions for two queens in the N * N chess board ; Driver Code ; Function Call
using System ; class GFG { static double possiblePositions ( double n ) { double term1 = Math . Pow ( n , 4 ) ; double term2 = Math . Pow ( n , 3 ) ; double term3 = Math . Pow ( n , 2 ) ; double term4 = n / 3 ; double ans = ( Math . Ceiling ( term1 / 2 ) ) - ( Math . Ceiling ( 5 * term2 ) / 3 ) + ( Math . Ceiling ( 3 * term3 ) / 2 ) - term4 ; return ( long ) ans ; } public static void Main ( String [ ] args ) { double n ; n = 3 ; double ans = possiblePositions ( n ) ; Console . Write ( ans + " STRNEWLINE " ) ; } }
Group all co | C # implementation to group mutually coprime numbers into one group with minimum group possible ; Function to group the mutually co - prime numbers into one group ; Loop for the numbers less than the 4 ; ints 1 , 2 and 3 can be grouped into one group ; Consecutive even and odd numbers ; Driver Code ; Function Call
using System ; class GFG { static void mutually_coprime ( int n ) { if ( n <= 3 ) { for ( int j = 1 ; j < n + 1 ; j ++ ) Console . Write ( j + " ▁ " ) ; Console . WriteLine ( ) ; } else { Console . WriteLine ( "1 ▁ 2 ▁ 3" ) ; for ( int j = 4 ; j < n ; j += 2 ) { Console . WriteLine ( j + " ▁ " + ( j + 1 ) ) ; if ( n % 2 == 0 ) Console . WriteLine ( n ) ; } } } public static void Main ( String [ ] args ) { int n = 9 ; mutually_coprime ( n ) ; } }
Maximum sum subset having equal number of positive and negative elements | C # implementation to find the maximum sum subset having equal number of positive and negative elements in the subset ; Function to find maximum sum subset with equal number of positive and negative elements ; Loop to store the positive and negative elements in two different array ; Sort both the array ; Pointers starting from the highest elements ; Find pairs having sum greater than zero ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findMaxSum ( int [ ] arr , int n ) { List < int > a = new List < int > ( ) ; List < int > b = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { a . Add ( arr [ i ] ) ; } else if ( arr [ i ] < 0 ) { b . Add ( arr [ i ] ) ; } } a . Sort ( ) ; b . Sort ( ) ; int p = a . Count - 1 ; int q = b . Count - 1 ; int s = 0 ; while ( p >= 0 && q >= 0 ) { if ( a [ p ] + b [ q ] > 0 ) { s = s + a [ p ] + b [ q ] ; } else { break ; } p = p - 1 ; q = q - 1 ; } return s ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , - 2 , 3 , 4 , - 5 , 8 } ; int n1 = arr1 . Length ; Console . Write ( findMaxSum ( arr1 , n1 ) + " STRNEWLINE " ) ; } }
Print the nodes with a prime degree in given Prufer sequence of a Tree | C # implementation to print the nodes with prime degree from the given prufer sequence ; Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to print the nodes with prime degree in the tree whose Prufer sequence is given ; Hash - table to mark the degree of every node ; Initially let all the degrees be 1 ; Increase the count of the degree ; Print the nodes with prime degree ; Driver Code
using System ; class GFG { static void SieveOfEratosthenes ( bool [ ] prime , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static void PrimeDegreeNodes ( int [ ] prufer , int n ) { int nodes = n + 2 ; bool [ ] prime = new bool [ nodes + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , nodes + 1 ) ; int [ ] degree = new int [ n + 2 + 1 ] ; for ( int i = 1 ; i <= nodes ; i ++ ) degree [ i ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) degree [ prufer [ i ] ] ++ ; for ( int i = 1 ; i <= nodes ; i ++ ) { if ( prime [ degree [ i ] ] ) { Console . Write ( i + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] a = { 4 , 1 , 3 , 4 } ; int n = a . Length ; PrimeDegreeNodes ( a , n ) ; } }
Nodes with prime degree in an undirected Graph | C # implementation of the approach ; To store Prime Numbers ; Function to find the prime numbers till 10 ^ 5 ; Traverse all multiple of i and make it false ; Function to print the nodes having prime degree ; To store Adjacency List of a Graph ; Make Adjacency List ; To precompute prime numbers till 10 ^ 5 ; Traverse each vertex ; Find size of Adjacency List ; If length of Adj [ i ] is Prime then print it ; Driver code ; Vertices and Edges ; Edges ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int n = 10005 ; static bool [ ] Prime = new bool [ n + 1 ] ; static void SieveOfEratosthenes ( ) { int i , j ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( i = 2 ; i * i <= 10005 ; i ++ ) { if ( Prime [ i ] ) { for ( j = 2 * i ; j < 10005 ; j += i ) { Prime [ j ] = false ; } } } } static void primeDegreeNodes ( int N , int M , int [ , ] edges ) { List < int > [ ] Adj = new List < int > [ N + 1 ] ; for ( int i = 0 ; i < Adj . Length ; i ++ ) Adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int x = edges [ i , 0 ] ; int y = edges [ i , 1 ] ; Adj [ x ] . Add ( y ) ; Adj [ y ] . Add ( x ) ; } SieveOfEratosthenes ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { int x = Adj [ i ] . Count ; if ( Prime [ x ] ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 4 , M = 6 ; int [ , ] edges = { { 1 , 2 } , { 1 , 3 } , { 1 , 4 } , { 2 , 3 } , { 2 , 4 } , { 3 , 4 } } ; for ( int i = 0 ; i < Prime . Length ; i ++ ) Prime [ i ] = true ; primeDegreeNodes ( N , M , edges ) ; } }
Number of ways to color N | C # program for the above approach ; Recursive function to count the ways ; Base case ; Initialise answer to 0 ; Color each uncolored block according to the given condition ; If any block is uncolored ; Check if adjacent blocks are colored or not ; Color the block ; recursively iterate for next uncolored block ; Uncolored for the next recursive call ; Return the final count ; Function to count the ways to color block ; Mark which blocks are colored in each recursive step ; Function call to count the ways ; Driver Code ; Number of blocks ; Number of colored blocks ; Function call
using System ; class GFG { static int mod = 1000000007 ; static int countWays ( int [ ] colored , int count , int n ) { if ( count == n ) { return 1 ; } int answer = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { if ( colored [ i ] == 0 ) { if ( colored [ i - 1 ] == 1 colored [ i + 1 ] == 1 ) { colored [ i ] = 1 ; answer = ( answer + countWays ( colored , count + 1 , n ) ) % mod ; colored [ i ] = 0 ; } } } return answer ; } static int waysToColor ( int [ ] arr , int n , int k ) { int [ ] colored = new int [ n + 2 ] ; for ( int i = 0 ; i < k ; i ++ ) { colored [ arr [ i ] ] = 1 ; } return countWays ( colored , k , n ) ; } public static void Main ( ) { int N = 6 ; int K = 3 ; int [ ] arr = { 1 , 2 , 6 } ; Console . Write ( waysToColor ( arr , N , K ) ) ; } }
Shortest Palindromic Substring | C # program to find the shortest palindromic subString ; Function return the shortest palindromic subString ; Finding the smallest character present in the String ; Driver code
using System ; class GFG { static char ShortestPalindrome ( String s ) { int n = s . Length ; char ans = s [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( char ) Math . Min ( ans , s [ i ] ) ; } return ans ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; Console . Write ( ShortestPalindrome ( s ) ) ; } }
Maximum length Subsequence with alternating sign and maximum Sum | C # implementation to find the subsequence with alternating sign having maximum size and maximum sum . ; Function to find the subsequence with alternating sign having maximum size and maximum sum . ; Find whether each element is positive or negative ; Find the required subsequence ; Find the maximum element in the specified range ; print the result ; Driver code ; array declaration ; size of array
using System ; public class GFG { static void findSubsequence ( int [ ] arr , int n ) { int [ ] sign = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) sign [ i ] = 1 ; else sign [ i ] = - 1 ; } int k = 0 ; int [ ] result = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = arr [ i ] ; int j = i ; while ( j < n && sign [ i ] == sign [ j ] ) { cur = Math . Max ( cur , arr [ j ] ) ; ++ j ; } result [ k ++ ] = cur ; i = j - 1 ; } for ( int i = 0 ; i < k ; i ++ ) Console . Write ( result [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 4 , 9 , 4 , 11 , - 5 , - 17 , 9 , - 3 , - 5 , 2 } ; int n = arr . Length ; findSubsequence ( arr , n ) ; } }