codeparrot/xlcost-text-to-code huggingface dataset:
Minimum deletions in Array to make difference of adjacent elements non | C # program for the above approach ; The maximum value of A [ i ] ; Function for finding minimum deletions so that the array becomes non - decreasing and the difference between adjacent elements is also non - decreasing ; Initializing the dp table and setting all values to 0 ; Find the maximum size valid set that can be taken and then subtract its size from N to get minimum number of deletions ; When selecting only current element and deleting all elements from 0 to i - 1 inclusive ; If this is true moving from index j to i is possible ; Iterate over all elements from 0 to diff and find the max ; Take the max set size from dp [ N - 1 ] [ 0 ] to dp [ N - 1 ] [ MAX ] ; Driver Code ; Function call
using System ; class GFG { static int MAX = 100001 ; static int minimumDeletions ( int [ ] A , int N ) { int [ , ] dp = new int [ N , MAX ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i , 0 ] = 1 ; for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( A [ i ] >= A [ j ] ) { int diff = A [ i ] - A [ j ] ; for ( int k = 0 ; k <= diff ; k ++ ) { dp [ i , diff ] = Math . Max ( dp [ i , diff ] , dp [ j , k ] + 1 ) ; } } } } int maxSetSize = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) maxSetSize = Math . Max ( maxSetSize , dp [ N - 1 , i ] ) ; return N - maxSetSize ; } public static void Main ( ) { int [ ] A = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . Length ; Console . Write ( minimumDeletions ( A , N ) ) ; } }
Count N | C # program for the above approach ; Stores the value of overlapping states ; Function to find the count of numbers that are formed by digits X and Y and whose sum of digit also have digit X or Y ; Initialize dp array ; Base Case ; Check if sum of digits formed by only X or Y ; Return the already computed ; Place the digit X at the current position ; Place the digit Y at the current position ; Update current state result ; Function to check whether a number have only digits X or Y or not ; Until sum is positive ; If any digit is not X or Y then return 0 ; Return 1 ; Driver Code ; Function Call
using System ; class GFG { static int [ , ] dp = new int [ 100 + 5 , 900 + 5 ] ; static int mod = 10000007 ; public static int countNumbers ( int n , int x , int y , int sum ) { for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) { dp [ i , j ] = - 1 ; } } if ( n == 0 ) { return check ( sum , x , y ) ; } if ( dp [ n , sum ] != - 1 ) { return dp [ n , sum ] % mod ; } int option1 = countNumbers ( n - 1 , x , y , sum + x ) % mod ; int option2 = countNumbers ( n - 1 , x , y , sum + y ) % mod ; return dp [ n , sum ] = ( option1 + option2 ) % mod ; } public static int check ( int sum , int x , int y ) { while ( sum > 0 ) { int ln = sum % 10 ; if ( ln != x && ln != y ) { return 0 ; } sum /= 10 ; } return 1 ; } public static void Main ( String [ ] args ) { int N = 3 , X = 1 , Y = 5 ; Console . WriteLine ( countNumbers ( N , X , Y , 0 ) % mod ) ; } }
Minimum cost to generate any permutation of the given string | C # program for the above approach ; Function to find the minimum cost to form any permutation of string s ; Base Case ; Return the precomputed state ; Iterate over the string and check all possible characters available for current position ; Check if character can be placed at current position ; As there is no previous character so the cost for 1 st character is 0 ; Find the cost of current character and move to next position ; Store the answer for each current state ; Function that returns true if the current bit is set ; Function that generates any permutation of the given string with minimum cost ; Initialize dp table ; Set all the bits of the current character id ; Minimum cost of generating the permutation ; Driver Code ; Function Call
using System ; class GFG { public static int solve ( int [ , ] a , String s , int n , int prev , int mask , int [ , ] dp ) { if ( mask == 0 ) return 0 ; if ( dp [ mask , prev + 1 ] != - 1 ) return dp [ mask , prev + 1 ] ; int ans = 10000 ; for ( int i = 0 ; i < s . Length ; i ++ ) { int id = s [ i ] - ' a ' ; if ( check ( mask , id ) ) { if ( prev == - 1 ) { ans = Math . Min ( ans , solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } else { ans = Math . Min ( ans , a [ prev , id ] + solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } } } dp [ mask , prev + 1 ] = ans ; return ans ; } public static bool check ( int mask , int i ) { int c = ( mask & ( 1 << i ) ) ; return c != 0 ; } static void generatePermutation ( int mask , int n , int [ , ] a , String s ) { int [ , ] dp = new int [ ( 1 << n ) + 5 , n + 5 ] ; for ( int i = 0 ; i < ( 1 << n ) + 5 ; i ++ ) for ( int j = 0 ; j < n + 5 ; j ++ ) dp [ i , j ] = - 1 ; for ( int i = 0 ; i < s . Length ; i ++ ) { int id = s [ i ] - ' a ' ; mask |= ( 1 << id ) ; } Console . WriteLine ( solve ( a , s , n , - 1 , mask , dp ) ) ; } public static void Main ( String [ ] args ) { int N = 5 ; String str = " abcde " ; int [ , ] mat = { { 0 , 5 , 1 , 5 , 3 } , { 4 , 0 , 9 , 4 , 2 } , { 7 , 9 , 0 , 10 , 7 } , { 1 , 2 , 8 , 0 , 2 } , { 3 , 9 , 7 , 7 , 0 } } ; generatePermutation ( 0 , N , mat , str ) ; } }
Minimize Cost to reduce the Array to a single element by given operations | C # Program for the above approach ; Function to minimize the cost to add the array elements to a single element ; Check if the value is already stored in the array ; Compute left subproblem ; Compute left subproblem ; Calculate minimum cost ; Store the answer to avoid recalculation ; Function to generate the cost using Prefix Sum Array technique ; Function to combine the sum of the two subproblems ; Driver Code ; Initialise dp array ; Preprocessing the array
using System ; class GFG { static int inf = 10000000 ; public static int minCost ( int [ ] a , int i , int j , int k , int [ ] prefix , int [ , ] dp ) { if ( i >= j ) return 0 ; if ( dp [ i , j ] != - 1 ) return dp [ i , j ] ; int best_cost = inf ; for ( int pos = i ; pos < j ; pos ++ ) { int left = minCost ( a , i , pos , k , prefix , dp ) ; int right = minCost ( a , pos + 1 , j , k , prefix , dp ) ; best_cost = Math . Min ( best_cost , left + right + ( k * Combine ( prefix , i , j ) ) ) ; } return dp [ i , j ] = best_cost ; } public static int [ ] preprocess ( int [ ] a , int n ) { int [ ] p = new int [ n ] ; p [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) p [ i ] = p [ i - 1 ] + a [ i ] ; return p ; } public static int Combine ( int [ ] p , int i , int j ) { if ( i == 0 ) return p [ j ] ; else return p [ j ] - p [ i - 1 ] ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] a = { 4 , 5 , 6 , 7 } ; int k = 3 ; int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } int [ ] prefix = preprocess ( a , n ) ; Console . WriteLine ( minCost ( a , 0 , n - 1 , k , prefix , dp ) ) ; } }
Minimize operations required to obtain N | Function to find the minimum number of operations ; Storing the minimum operations to obtain all numbers up to N ; Initilal state ; Iterate for the remaining numbers ; If the number can be obtained by multiplication by 2 ; If the number can be obtained by multiplication by 3 ; Obtaining the number by adding 1 ; Return the minm operations to obtain n ; Driver Code
using System ; class GFG { static int minOperations ( int n ) { int [ ] dp = new int [ n + 1 ] ; int x ; dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = int . MaxValue ; if ( i % 2 == 0 ) { x = dp [ i / 2 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } if ( i % 3 == 0 ) { x = dp [ i / 3 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } x = dp [ i - 1 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } return dp [ n ] ; } public static void Main ( string [ ] args ) { int n = 15 ; Console . Write ( minOperations ( n ) ) ; } }
Longest Reverse Bitonic Sequence | C # program to find the longest Reverse bitonic Subsequence ; Function to return the length of the Longest Reverse Bitonic Subsequence in the array ; Allocate memory for LIS [ ] and initialize LIS values as 1 for all indexes ; Compute LIS values from left to right for every index ; Initialize LDS for all indexes as 1 ; Compute LDS values for every index from right to left ; Find the maximum value of lis [ i ] + lds [ i ] - 1 in the array ; Return the maximum ; Driver code
using System ; class GFG { static int ReverseBitonic ( int [ ] arr , int N ) { int i , j ; int [ ] lds = new int [ N ] ; for ( i = 0 ; i < N ; i ++ ) { lds [ i ] = 1 ; } for ( i = 1 ; i < N ; i ++ ) { for ( j = 0 ; j < i ; j ++ ) { if ( arr [ i ] < arr [ j ] && lds [ i ] < lds [ j ] + 1 ) { lds [ i ] = lds [ j ] + 1 ; } } } int [ ] lis = new int [ N ] ; for ( i = 0 ; i < N ; i ++ ) { lis [ i ] = 1 ; } for ( i = N - 2 ; i >= 0 ; i -- ) { for ( j = N - 1 ; j > i ; j -- ) { if ( arr [ i ] < arr [ j ] && lis [ i ] < lis [ j ] + 1 ) { lis [ i ] = lis [ j ] + 1 ; } } } int max = lis [ 0 ] + lds [ 0 ] - 1 ; for ( i = 1 ; i < N ; i ++ ) { if ( lis [ i ] + lds [ i ] - 1 > max ) { max = lis [ i ] + lds [ i ] - 1 ; } } return max ; } public static void Main ( ) { int [ ] arr = new int [ ] { 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 } ; int N = arr . Length ; Console . WriteLine ( " Length ▁ of ▁ LBS ▁ is ▁ " + ReverseBitonic ( arr , N ) ) ; } }
Maze With N doors and 1 Key | C # implementation of the approach ; Recursive function to check whether there is a path from the top left cell to the bottom right cell of the maze ; Check whether the current cell is within the maze ; If key is required to move further ; If the key hasn 't been used before ; If current cell is the destination ; Either go down or right ; If current cell is the destination ; Either go down or right ; Driver code ; We are making these { { '0' , '0' , '1' } , { '1' , '0' , '1' } , { '1' , '1' , '0' } } ; ; If there is a path from the cell ( 0 , 0 )
using System ; using System . Collections . Generic ; class GFG { static bool findPath ( List < List < int > > maze , int xpos , int ypos , bool key ) { if ( xpos < 0 xpos >= maze . Count ypos < 0 ypos >= maze . Count ) return false ; if ( maze [ xpos ] [ ypos ] == '1' ) { if ( key == true ) if ( xpos == maze . Count - 1 && ypos == maze . Count - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , false ) || findPath ( maze , xpos , ypos + 1 , false ) ; } if ( xpos == maze . Count - 1 && ypos == maze . Count - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , key ) || findPath ( maze , xpos , ypos + 1 , key ) ; } static bool mazeProb ( List < List < int > > maze , int xpos , int ypos ) { bool key = true ; if ( findPath ( maze , xpos , ypos , key ) ) return true ; return false ; } public static void Main ( String [ ] args ) { int size = 3 ; List < List < int > > maze = new List < List < int > > ( size ) ; for ( int i = 0 ; i < size ; i ++ ) { maze . Add ( new List < int > ( ) ) ; } maze [ 0 ] . Add ( 0 ) ; maze [ 0 ] . Add ( 0 ) ; maze [ 0 ] . Add ( 1 ) ; maze [ 1 ] . Add ( 1 ) ; maze [ 1 ] . Add ( 0 ) ; maze [ 1 ] . Add ( 1 ) ; maze [ 2 ] . Add ( 1 ) ; maze [ 2 ] . Add ( 1 ) ; maze [ 2 ] . Add ( 0 ) ; if ( mazeProb ( maze , 0 , 0 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Minimum cost to reach end of array array when a maximum jump of K index is allowed | C # implementation of the above approach ; Function to return the minimum cost to reach the last index ; If we reach the last index ; Already visited state ; Initially maximum ; Visit all possible reachable index ; If inside range ; We cannot move any further ; Memoize ; Driver Code
using System ; class GfG { static int FindMinimumCost ( int ind , int [ ] a , int n , int k , int [ ] dp ) { if ( ind == ( n - 1 ) ) return 0 ; else if ( dp [ ind ] != - 1 ) return dp [ ind ] ; else { int ans = int . MaxValue ; for ( int i = 1 ; i <= k ; i ++ ) { if ( ind + i < n ) ans = Math . Min ( ans , Math . Abs ( a [ ind + i ] - a [ ind ] ) + FindMinimumCost ( ind + i , a , n , k , dp ) ) ; else break ; } return dp [ ind ] = ans ; } } public static void Main ( ) { int [ ] a = { 10 , 30 , 40 , 50 , 20 } ; int k = 3 ; int n = a . Length ; int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = - 1 ; Console . WriteLine ( FindMinimumCost ( 0 , a , n , k , dp ) ) ; } }
Sum of all even factors of numbers in the range [ l , r ] | C # implementation of the approach ; Function to calculate the prefix sum of all the even factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the even factors of the numbers in the given range ; Driver code
using System ; using System ; class GFG { public const int MAX = 100000 ; public static long [ ] prefix = new long [ MAX ] ; public static void sieve_modified ( ) { for ( int i = 2 ; i < MAX ; i += 2 ) { for ( int j = i ; j < MAX ; j += i ) { prefix [ j ] += i ; } } for ( int i = 1 ; i < MAX ; i ++ ) { prefix [ i ] += prefix [ i - 1 ] ; } } public static long sumEvenFactors ( int L , int R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } public static void Main ( string [ ] args ) { sieve_modified ( ) ; int l = 6 , r = 10 ; Console . Write ( sumEvenFactors ( l , r ) ) ; } }
Count of numbers between range having only non | C # implementation of the approach ; states - position , sum , rem , tight sum can have values upto 162 , if we are dealing with numbers upto 10 ^ 18 when all 18 digits are 9 , then sum is 18 * 9 = 162 ; n is the sum of digits and number should be divisible by m ; Function to return the count of required numbers from 0 to num ; Last position ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current digit is zero and nonz is 1 , we can 't place it ; Function to convert x into its digit vector and uses count ( ) function to return the required count ; Initialize dp ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int M = 20 ; static int [ , , , ] dp = new int [ M , 165 , M , 2 ] ; static int n , m ; static int count ( int pos , int sum , int rem , int tight , int nonz , List < int > num ) { if ( pos == num . Count ) { if ( rem == 0 && sum == n ) return 1 ; return 0 ; } if ( dp [ pos , sum , rem , tight ] != - 1 ) return dp [ pos , sum , rem , tight ] ; int ans = 0 ; int limit = ( tight != 0 ? 9 : num [ pos ] ) ; for ( int d = 0 ; d <= limit ; d ++ ) { if ( d == 0 && nonz != 0 ) continue ; int currSum = sum + d ; int currRem = ( rem * 10 + d ) % m ; int currF = ( tight != 0 || ( d < num [ pos ] ) ) ? 1 : 0 ; ans += count ( pos + 1 , currSum , currRem , currF , ( nonz != 0 d != 0 ) ? 1 : 0 , num ) ; } return dp [ pos , sum , rem , tight ] = ans ; } static int solve ( int x ) { List < int > num = new List < int > ( ) ; while ( x != 0 ) { num . Add ( x % 10 ) ; x /= 10 ; } num . Reverse ( ) ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < 165 ; j ++ ) for ( int k = 0 ; k < M ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i , j , k , l ] = - 1 ; return count ( 0 , 0 , 0 , 0 , 0 , num ) ; } public static void Main ( String [ ] args ) { int L = 1 , R = 100 ; n = 8 ; m = 2 ; Console . Write ( solve ( R ) - solve ( L ) ) ; } }
Count of Numbers in Range where the number does not contain more than K non zero digits | C # Program to find the count of numbers in a range where the number does not contain more than K non zero digits ; states - position , count , tight ; K is the number of non zero digits ; This function returns the count of required numbers from 0 to num ; Last position ; If count of non zero digits is less than or equal to K ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current digit is nonzero increment currCnt ; At this position , number becomes smaller ; Next recursive call ; This function converts a number into its digit vector and uses above function to compute the answer ; Initialize dp ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int M = 20 ; static int [ , , ] dp = new int [ M , M , 2 ] ; static int K ; static List < int > num ; static int countInRangeUtil ( int pos , int cnt , int tight ) { if ( pos == num . Count ) { if ( cnt <= K ) return 1 ; return 0 ; } if ( dp [ pos , cnt , tight ] != - 1 ) return dp [ pos , cnt , tight ] ; int ans = 0 ; int limit = ( tight != 0 ? 9 : num [ pos ] ) ; for ( int dig = 0 ; dig <= limit ; dig ++ ) { int currCnt = cnt ; if ( dig != 0 ) currCnt ++ ; int currTight = tight ; if ( dig < num [ pos ] ) currTight = 1 ; ans += countInRangeUtil ( pos + 1 , currCnt , currTight ) ; } return dp [ pos , cnt , tight ] = ans ; } static int countInRange ( int x ) { num = new List < int > ( ) ; while ( x != 0 ) { num . Add ( x % 10 ) ; x /= 10 ; } num . Reverse ( ) ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) for ( int k = 0 ; k < 2 ; k ++ ) dp [ i , j , k ] = - 1 ; return countInRangeUtil ( 0 , 0 , 0 ) ; } public static void Main ( ) { int L = 1 , R = 1000 ; K = 3 ; Console . WriteLine ( countInRange ( R ) - countInRange ( L - 1 ) ) ; L = 9995 ; R = 10005 ; K = 2 ; Console . WriteLine ( countInRange ( R ) - countInRange ( L - 1 ) ) ; } }
Balanced expressions such that given positions have opening brackets | Set 2 | C # implementation of above approach using memoization ; Function to find Number of proper bracket expressions ; If open - closed brackets < 0 ; If index reaches the end of expression ; If brackets are balanced ; If already stored in dp ; If the current index has assigned open bracket ; Move forward increasing the length of open brackets ; Move forward by inserting open as well as closed brackets on that index ; return the answer ; Driver code ; DP array to precompute the answer ; Open brackets at position 1 ; Calling the find function to calculate the answer
using System ; class GFG { static readonly int N = 1000 ; static int find ( int index , int openbrk , int n , int [ , ] dp , int [ ] adj ) { if ( openbrk < 0 ) { return 0 ; } if ( index == n ) { if ( openbrk == 0 ) { return 1 ; } else { return 0 ; } } if ( dp [ index , openbrk ] != - 1 ) { return dp [ index , openbrk ] ; } if ( adj [ index ] == 1 ) { dp [ index , openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) ; } else { dp [ index , openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) + find ( index + 1 , openbrk - 1 , n , dp , adj ) ; } return dp [ index , openbrk ] ; } public static void Main ( ) { int [ , ] dp = new int [ N , N ] ; int n = 2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i , j ] = - 1 ; } } int [ ] adj = { 1 , 0 , 0 , 0 } ; Console . WriteLine ( find ( 0 , 0 , 2 * n , dp , adj ) ) ; } }
Maximize array elements upto given number | C # code to find maximum value of number obtained by using array elements recursively . ; Utility function to find maximum possible value ; If entire array is traversed , then compare current value in num to overall maximum obtained so far . ; Case 1 : Subtract current element from value so far if result is greater than or equal to zero . ; Case 2 : Add current element to value so far if result is less than or equal to maxLimit . ; Function to find maximum possible value that can be obtained using array elements and given number . ; variable to store maximum value that can be obtained . ; variable to store current index position . ; call to utility function to find maximum possible value that can be obtained . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findMaxValUtil ( int [ ] arr , int n , int num , int maxLimit , int ind , ref int ans ) { if ( ind == n ) { ans = Math . Max ( ans , num ) ; return ; } if ( num - arr [ ind ] >= 0 ) { findMaxValUtil ( arr , n , num - arr [ ind ] , maxLimit , ind + 1 , ref ans ) ; } if ( num + arr [ ind ] <= maxLimit ) { findMaxValUtil ( arr , n , num + arr [ ind ] , maxLimit , ind + 1 , ref ans ) ; } } static int findMaxVal ( int [ ] arr , int n , int num , int maxLimit ) { int ans = 0 ; int ind = 0 ; findMaxValUtil ( arr , n , num , maxLimit , ind , ref ans ) ; return ans ; } public static void Main ( ) { int num = 1 ; int [ ] arr = { 3 , 10 , 6 , 4 , 5 } ; int n = arr . Length ; int maxLimit = 15 ; Console . Write ( findMaxVal ( arr , n , num , maxLimit ) ) ; } }
Print equal sum sets of array ( Partition problem ) | Set 1 | C # program to print equal sum two subsets of an array if it can be partitioned into subsets . ; / Function to print the equal sum sets of the array . ; / Print set 1. ; / Print set 2. ; Utility function to find the sets of the array which have equal sum . ; If entire array is traversed , compare both the sums . ; If sums are equal print both sets and return true to show sets are found . ; If sums are not equal then return sets are not found . ; Add current element to set 1. ; Recursive call after adding current element to set 1. ; If this inclusion results in equal sum sets partition then return true to show desired sets are found . ; If not then backtrack by removing current element from set1 and include it in set 2. ; Recursive call after including current element to set 2. ; Return true if array arr can be partitioned into two equal sum sets or not . ; Calculate sum of elements in array . ; If sum is odd then array cannot be partitioned . ; Declare Lists to store both the sets . ; / Find both the sets . ; Driver code
using System ; using System . Collections . Generic ; using System . Linq ; using System . Collections ; class GFG { static void printSets ( List < int > set1 , List < int > set2 ) { int i ; for ( i = 0 ; i < set1 . Count ; i ++ ) { Console . Write ( set1 [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; for ( i = 0 ; i < set2 . Count ; i ++ ) { Console . Write ( set2 [ i ] + " ▁ " ) ; } } static bool findSets ( int [ ] arr , int n , ref List < int > set1 , ref List < int > set2 , int sum1 , int sum2 , int pos ) { if ( pos == n ) { if ( sum1 == sum2 ) { printSets ( set1 , set2 ) ; return true ; } else return false ; } set1 . Add ( arr [ pos ] ) ; bool res = findSets ( arr , n , ref set1 , ref set2 , sum1 + arr [ pos ] , sum2 , pos + 1 ) ; if ( res == true ) return res ; set1 . RemoveAt ( set1 . Count - 1 ) ; set2 . Add ( arr [ pos ] ) ; res = findSets ( arr , n , ref set1 , ref set2 , sum1 , sum2 + arr [ pos ] , pos + 1 ) ; if ( res == false ) if ( set2 . Count > 0 ) set2 . RemoveAt ( set2 . Count - 1 ) ; return res ; } static bool isPartitionPoss ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; List < int > set1 = new List < int > ( ) ; List < int > set2 = new List < int > ( ) ; return findSets ( arr , n , ref set1 , ref set2 , 0 , 0 , 0 ) ; } public static void Main ( ) { int [ ] arr = { 5 , 5 , 1 , 11 } ; int n = arr . Length ; if ( isPartitionPoss ( arr , n ) == false ) { Console . Write ( " - 1" ) ; } } }