codeparrot/xlcost-text-to-code huggingface dataset:
Find the count of mountains in a given Matrix | C # program find the count of mountains in a given Matrix ; Function to count number of mountains in a given matrix of size n ; form another matrix with one extra layer of border elements . Border elements will contain Integer . MIN_VALUE value . ; For border elements , set value as Integer . MIN_VALUE ; For rest elements , just copy it into new matrix ; Check for mountains in the modified matrix ; check for all directions ; Driver code
using System ; class GFG { static int MAX = 100 ; static int countMountains ( int [ , ] a , int n ) { int [ , ] A = new int [ n + 2 , n + 2 ] ; int count = 0 ; for ( int i = 0 ; i < n + 2 ; i ++ ) { for ( int j = 0 ; j < n + 2 ; j ++ ) { if ( ( i == 0 ) || ( j == 0 ) || ( i == n + 1 ) || ( j == n + 1 ) ) { A [ i , j ] = int . MinValue ; } else { A [ i , j ] = a [ i - 1 , j - 1 ] ; } } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( ( A [ i , j ] > A [ i - 1 , j ] ) && ( A [ i , j ] > A [ i + 1 , j ] ) && ( A [ i , j ] > A [ i , j - 1 ] ) && ( A [ i , j ] > A [ i , j + 1 ] ) && ( A [ i , j ] > A [ i - 1 , j - 1 ] ) && ( A [ i , j ] > A [ i + 1 , j + 1 ] ) && ( A [ i , j ] > A [ i - 1 , j + 1 ] ) && ( A [ i , j ] > A [ i + 1 , j - 1 ] ) ) { count ++ ; } } } return count ; } public static void Main ( string [ ] args ) { int [ , ] a = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int n = 3 ; Console . WriteLine ( countMountains ( a , n ) ) ; } }
Minimum length of the reduced Array formed using given operations | C # implementation to find the minimum length of the array ; Function to find the length of minimized array ; Creating the required dp tables ; Initialising the dp table by - 1 ; base case ; Check if the two subarray can be combined ; Initialising dp1 table with max value ; Check if the subarray can be reduced to a single element ; Minimal partition of [ 1 : j - 1 ] + 1 ; Driver code
using System ; class GFG { static int minimalLength ( int [ ] a , int n ) { int [ , ] dp = new int [ n + 1 , n + 1 ] ; int [ ] dp1 = new int [ n ] ; int i , j , k ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < n + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } for ( int size = 1 ; size <= n ; size ++ ) { for ( i = 0 ; i < n - size + 1 ; i ++ ) { j = i + size - 1 ; if ( i == j ) dp [ i , j ] = a [ i ] ; else { for ( k = i ; k < j ; k ++ ) { if ( dp [ i , k ] != - 1 && dp [ i , k ] == dp [ k + 1 , j ] ) dp [ i , j ] = dp [ i , k ] + 1 ; } } } } for ( i = 0 ; i < n ; i ++ ) dp1 [ i ] = ( int ) 1e7 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( dp [ j , i ] != - 1 ) { if ( j == 0 ) dp1 [ i ] = 1 ; else dp1 [ i ] = Math . Min ( dp1 [ i ] , dp1 [ j - 1 ] + 1 ) ; } } } return dp1 [ n - 1 ] ; } public static void Main ( string [ ] args ) { int n = 7 ; int [ ] a = { 3 , 3 , 4 , 4 , 4 , 3 , 3 } ; Console . Write ( minimalLength ( a , n ) ) ; } }
Maximum score possible after performing given operations on an Array | C # program to find the maximum Score after given operations ; Memoizing by the use of a table ; Function to calculate maximum score ; Bse case ; If the same state has already been computed ; Sum of array in range ( l , r ) ; If the operation is even - numbered the score is decremented ; Exploring all paths , and storing maximum value in DP table to avoid further repetitive recursive calls ; Function to find the max score ; Prefix sum array ; Calculating prefix_sum ; Initialising the DP table , - 1 represents the subproblem hasn 't been solved yet ; Driver code
using System ; public class GFG { static int [ , , ] dp = new int [ 100 , 100 , 100 ] ; static int MaximumScoreDP ( int l , int r , int [ ] prefix_sum , int num ) { if ( l > r ) return 0 ; if ( dp [ l , r , num ] != - 1 ) return dp [ l , r , num ] ; int current_sum = prefix_sum [ r ] - ( l - 1 >= 0 ? prefix_sum [ l - 1 ] : 0 ) ; if ( num % 2 == 0 ) current_sum *= - 1 ; dp [ l , r , num ] = current_sum + Math . Max ( MaximumScoreDP ( l + 1 , r , prefix_sum , num + 1 ) , MaximumScoreDP ( l , r - 1 , prefix_sum , num + 1 ) ) ; return dp [ l , r , num ] ; } 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 ] ; } for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 100 ; j ++ ) { for ( int l = 0 ; l < 100 ; l ++ ) dp [ i , j , l ] = - 1 ; } } return MaximumScoreDP ( 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 ) ) ; } }
Count number of binary strings without consecutive 1 â €™ s : Set 2 | C # program to count number of binary Strings without consecutive 1 aTMs ; Table to store the solution of every sub problem ; Here , pos : keeps track of current position . f1 : is the flag to check if current number is less than N or not . pr : represents the previous digit ; Base case ; Check if this subproblem has already been solved ; Placing 0 at the current position as it does not violate the condition ; Here flag will be 1 for the next recursive call ; Placing 1 at this position only if the previously inserted number is 0 ; If the number is smaller than N ; If the digit at current position is 1 ; Storing the solution to this subproblem ; Function to find the number of integers less than or equal to N with no consecutive 1 aTMs in binary representation ; Convert N to binary form ; Loop to convert N from Decimal to binary ; Initialising the table with - 1. ; Calling the function ; Utility reverse function ; Driver code
using System ; public class GFG { static int [ , , ] memo = new int [ 32 , 2 , 2 ] ; static int dp ( int pos , int fl , int pr , String bin ) { if ( pos == bin . Length ) return 1 ; if ( memo [ pos , fl , pr ] != - 1 ) return memo [ pos , fl , pr ] ; int val = 0 ; if ( bin [ pos ] == '0' ) val = val + dp ( pos + 1 , fl , 0 , bin ) ; else if ( bin [ pos ] == '1' ) val = val + dp ( pos + 1 , 1 , 0 , bin ) ; if ( pr == 0 ) { if ( fl == 1 ) { val += dp ( pos + 1 , fl , 1 , bin ) ; } else if ( bin [ pos ] == '1' ) { val += dp ( pos + 1 , fl , 1 , bin ) ; } } return memo [ pos , fl , pr ] = val ; } static int findints ( int num ) { String bin = " " ; while ( num > 0 ) { if ( num % 2 == 1 ) bin += "1" ; else bin += "0" ; num /= 2 ; } bin = reverse ( bin ) ; for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { for ( int l = 0 ; l < 2 ; l ++ ) memo [ i , j , l ] = - 1 ; } } return dp ( 0 , 0 , 0 , bin ) ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { int N = 12 ; Console . Write ( findints ( N ) ) ; } }
Count ways to reach end from start stone with at most K jumps at each step | C # program to find total no . of ways to reach nth step ; Function which returns total no . of ways to reach nth step from sth steps ; Initialize dp array ; Initialize ( s - 1 ) th index to 1 ; Iterate a loop from s to n ; starting range for counting ranges ; Calculate Maximum moves to Reach ith step ; For nth step return dp [ n - 1 ] ; Driver Code ; no of steps ; Atmost steps allowed ; starting range
using System ; class GFG { static int TotalWays ( int n , int s , int k ) { int [ ] dp = new int [ n ] ; dp [ s - 1 ] = 1 ; for ( int i = s ; i < n ; i ++ ) { int idx = Math . Max ( s - 1 , i - k ) ; for ( int j = idx ; j < i ; j ++ ) { dp [ i ] += dp [ j ] ; } } return dp [ n - 1 ] ; } public static void Main ( string [ ] args ) { int n = 5 ; int k = 2 ; int s = 2 ; Console . Write ( " Total ▁ Ways ▁ = ▁ " + TotalWays ( n , s , k ) ) ; } }
Maximum subsequence sum with adjacent elements having atleast K difference in index | C # implementation to find the maximum sum subsequence such that two adjacent element have atleast difference of K in their indices ; Function to find the maximum sum subsequence such that two adjacent element have atleast difference of K in their indices ; DP Array to store the maximum sum obtained till now ; Either select the first element or Nothing ; Either Select the ( i - 1 ) element or let the previous best answer be the current best answer ; Either select the best sum till previous_index or select the current element + best_sum till index - k ; Driver Code
using System ; class GFG { static int max_sum ( int [ ] arr , int n , int k ) { int [ ] dp = new int [ n ] ; dp [ 0 ] = Math . Max ( 0 , arr [ 0 ] ) ; int i = 1 ; while ( i < k ) { dp [ i ] = Math . Max ( dp [ i - 1 ] , arr [ i ] ) ; i ++ ; } i = k ; while ( i < n ) { dp [ i ] = Math . Max ( dp [ i - 1 ] , arr [ i ] + dp [ i - k ] ) ; i ++ ; } return dp [ n - 1 ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , - 2 , 4 , 3 , 1 } ; int n = arr . Length ; int k = 4 ; Console . WriteLine ( max_sum ( arr , n , k ) ) ; } }
Number of binary strings such that there is no substring of length â ‰¥ 3 | C # implementation of the approach ; Function to return the count of all possible binary strings ; Base cases ; dp [ i , j ] is the number of possible strings such that '1' just appeared consecutively j times upto ith index ; Taking previously calculated value ; Taking all the possible cases that can appear at the Nth position ; Driver code
using System ; class GFG { static long MOD = 1000000007 ; static long countStr ( int N ) { long [ , ] dp = new long [ N + 1 , 3 ] ; dp [ 1 , 0 ] = 1 ; dp [ 1 , 1 ] = 1 ; dp [ 1 , 2 ] = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i , 0 ] = ( dp [ i - 1 , 0 ] + dp [ i - 1 , 1 ] + dp [ i - 1 , 2 ] ) % MOD ; dp [ i , 1 ] = dp [ i - 1 , 0 ] % MOD ; dp [ i , 2 ] = dp [ i - 1 , 1 ] % MOD ; } long ans = ( dp [ N , 0 ] + dp [ N , 1 ] + dp [ N , 2 ] ) % MOD ; return ans ; } public static void Main ( ) { int N = 8 ; Console . WriteLine ( countStr ( N ) ) ; } }
Count number of ways to get Odd Sum | C # implementation of above approach ; Count the ways to sum up with odd by choosing one element form each pair ; Initialize two array with 0 ; if element is even ; store count of even number in i 'th pair ; if the element is odd ; store count of odd number in i 'th pair ; Initial state of dp array ; dp [ i , 0 ] = total number of ways to get even sum upto i 'th pair ; dp [ i , 1 ] = total number of ways to odd even sum upto i 'th pair ; dp [ n - 1 , 1 ] = total number of ways to get odd sum upto n 'th pair ; Driver code
using System ; class GFG { static int CountOfOddSum ( int [ , ] a , int n ) { int [ , ] dp = new int [ n , 2 ] ; int [ , ] cnt = new int [ n , 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( a [ i , j ] % 2 == 0 ) { cnt [ i , 0 ] ++ ; } else { cnt [ i , 1 ] ++ ; } } } dp [ 0 , 0 ] = cnt [ 0 , 0 ] ; dp [ 0 , 1 ] = cnt [ 0 , 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i , 0 ] = ( dp [ i - 1 , 0 ] * cnt [ i , 0 ] + dp [ i - 1 , 1 ] * cnt [ i , 1 ] ) ; dp [ i , 1 ] = ( dp [ i - 1 , 0 ] * cnt [ i , 1 ] + dp [ i - 1 , 1 ] * cnt [ i , 0 ] ) ; } return dp [ n - 1 , 1 ] ; } public static void Main ( ) { int [ , ] a = { { 1 , 2 } , { 3 , 6 } } ; int n = a . GetLength ( 1 ) ; int ans = CountOfOddSum ( a , n ) ; Console . WriteLine ( ans ) ; } }
Longest Consecuetive Subsequence when only one insert operation is allowed | C # implementation of above approach ; Function to return the length of longest consecuetive subsequence after inserting an element ; Variable to find maximum value of the array ; Calculating maximum value of the array ; Declaring the DP table ; Variable to store the maximum length ; Iterating for every value present in the array ; Recurrence for dp [ val ] [ 0 ] ; No value can be inserted before 1 , hence the element value should be greater than 1 for this recurrance relation ; Recurrence for dp [ val ] [ 1 ] ; Maximum length of consecutive sequence ending at 1 is equal to 1 ; Update the ans variable with the new maximum length possible ; Return the ans ; Driver code ; Input array
using System ; class GFG { static int LongestConsSeq ( int [ ] arr , int N ) { int maxval = 1 ; for ( int i = 0 ; i < N ; i += 1 ) { maxval = Math . Max ( maxval , arr [ i ] ) ; } int [ , ] dp = new int [ maxval + 1 , 2 ] ; int ans = 1 ; for ( int i = 0 ; i < N ; i += 1 ) { dp [ arr [ i ] , 0 ] = ( 1 + dp [ arr [ i ] - 1 , 0 ] ) ; if ( arr [ i ] >= 2 ) dp [ arr [ i ] , 1 ] = Math . Max ( 1 + dp [ arr [ i ] - 1 , 1 ] , 2 + dp [ arr [ i ] - 2 , 0 ] ) ; else dp [ arr [ i ] , 1 ] = 1 ; ans = Math . Max ( ans , dp [ arr [ i ] , 1 ] ) ; } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 2 , 1 , 4 , 5 } ; int N = arr . Length ; Console . WriteLine ( LongestConsSeq ( arr , N ) ) ; } }
Stepping Numbers | A C # program to find all the Stepping Number in [ n , m ] ; This Method checks if an integer n is a Stepping Number ; Initalize prevDigit with - 1 ; Iterate through all digits of n and compare difference between value of previous and current digits ; Get Current digit ; Single digit is considered as a Stepping Number ; Check if absolute difference between prev digit and current digit is 1 ; A brute force approach based function to find all stepping numbers . ; Iterate through all the numbers from [ N , M ] and check if it is a stepping number . ; Driver code ; Display Stepping Numbers in the range [ n , m ]
using System ; class GFG { public static bool isStepNum ( int n ) { int prevDigit = - 1 ; while ( n > 0 ) { int curDigit = n % 10 ; if ( prevDigit != - 1 ) { if ( Math . Abs ( curDigit - prevDigit ) != 1 ) return false ; } n /= 10 ; prevDigit = curDigit ; } return true ; } public static void displaySteppingNumbers ( int n , int m ) { for ( int i = n ; i <= m ; i ++ ) if ( isStepNum ( i ) ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ; } }
Count numbers in given range such that sum of even digits is greater than sum of odd digits | C # code to count number in the range having the sum of even digits greater than the sum of odd digits ; Base Case ; check if condition satisfied or not ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 0 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; if current digit is odd ; } if current digit is even ; Function to convert n into its digit vector and uses memo ( ) function to return the required count ; Initialize DP ; Driver Code
using System . Collections . Generic ; using System ; class GFG { static List < int > v = new List < int > ( ) ; static int [ , , , ] dp = new int [ 18 , 180 , 180 , 2 ] ; static int memo ( int index , int evenSum , int oddSum , int tight ) { if ( index == v . Count ) { if ( evenSum > oddSum ) { return 1 ; } else { return 0 ; } } if ( dp [ index , evenSum , oddSum , tight ] != - 1 ) { return dp [ index , evenSum , oddSum , tight ] ; } int limit = ( tight > 0 ) ? v [ index ] : 9 ; int ans = 0 ; for ( int d = 0 ; d <= limit ; d ++ ) { int currTight = 0 ; if ( d == v [ index ] ) { currTight = tight ; } if ( d % 2 != 0 ) { ans += memo ( index + 1 , evenSum , oddSum + d , currTight ) ; else { ans += memo ( index + 1 , evenSum + d , oddSum , currTight ) ; } } dp [ index , evenSum , oddSum , tight ] = ans ; return ans ; } static int CountNum ( int n ) { v . Clear ( ) ; while ( n > 0 ) { v . Add ( n % 10 ) ; n = n / 10 ; } v . Reverse ( ) ; for ( int i = 0 ; i < 18 ; i ++ ) { for ( int j = 0 ; j < 180 ; j ++ ) { for ( int k = 0 ; k < 180 ; k ++ ) { for ( int l = 0 ; l < 2 ; l ++ ) { dp [ i , j , k , l ] = - 1 ; } } } } return memo ( 0 , 0 , 0 , 1 ) ; } public static void Main ( String [ ] args ) { int L , R ; L = 2 ; R = 10 ; Console . WriteLine ( CountNum ( R ) - CountNum ( L - 1 ) ) ; } }
Maximum sub | C # implementation of the approach ; Function to return the maximum sum of the sub - sequence such that two consecutive elements have a difference of at least 3 in their indices in the given array ; If there is a single element in the array ; Either select it or don 't ; If there are two elements ; Either select the first element or don 't ; Either select the first or the second element or don 't select any element ; Either select the first element or don 't ; Either select the first or the second element or don 't select any element ; Either select first , second , third or nothing ; For the rest of the elements ; Either select the best sum till previous_index or select the current element + best_sum till index - 3 ; Driver code
using System ; class GFG { static int max_sum ( int [ ] a , int n ) { int [ ] dp = new int [ n ] ; if ( n == 1 ) { dp [ 0 ] = Math . Max ( 0 , a [ 0 ] ) ; } else if ( n == 2 ) { dp [ 0 ] = Math . Max ( 0 , a [ 0 ] ) ; dp [ 1 ] = Math . Max ( a [ 1 ] , dp [ 0 ] ) ; } else if ( n >= 3 ) { dp [ 0 ] = Math . Max ( 0 , a [ 0 ] ) ; dp [ 1 ] = Math . Max ( a [ 1 ] , Math . Max ( 0 , a [ 0 ] ) ) ; dp [ 2 ] = Math . Max ( a [ 2 ] , Math . Max ( a [ 1 ] , Math . Max ( 0 , a [ 0 ] ) ) ) ; int i = 3 ; while ( i < n ) { dp [ i ] = Math . Max ( dp [ i - 1 ] , a [ i ] + dp [ i - 3 ] ) ; i ++ ; } } return dp [ n - 1 ] ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , - 2 , 4 , 3 } ; int n = arr . Length ; Console . Write ( max_sum ( arr , n ) ) ; } }
Minimum count of elements that sums to a given number | C # implementation of the above approach ; we will only store min counts of sum upto 100 ; initialize with INT_MAX ; memo [ 0 ] = 0 as 0 is made from 0 elements ; fill memo array with min counts of elements that will constitute sum upto 100 ; min_count will store min count of elements chosen ; starting from end iterate over each 2 digits and add min count of elements to min_count ; Driver code
using System ; class GFG { static int minCount ( int K ) { int [ ] memo = new int [ 100 ] ; for ( int i = 0 ; i < 100 ; i ++ ) { memo [ i ] = int . MaxValue ; } memo [ 0 ] = 0 ; for ( int i = 1 ; i < 100 ; i ++ ) { memo [ i ] = Math . Min ( memo [ i - 1 ] + 1 , memo [ i ] ) ; } for ( int i = 10 ; i < 100 ; i ++ ) { memo [ i ] = Math . Min ( memo [ i - 10 ] + 1 , memo [ i ] ) ; } for ( int i = 25 ; i < 100 ; i ++ ) { memo [ i ] = Math . Min ( memo [ i - 25 ] + 1 , memo [ i ] ) ; } int min_count = 0 ; while ( K > 0 ) { min_count += memo [ K % 100 ] ; K /= 100 ; } return min_count ; } static public void Main ( ) { int K = 69 ; Console . WriteLine ( minCount ( K ) ) ; } }
Number of sub | C # implementation of above approach ; Function to return the number of subsequences which have at least one consecutive pair with difference less than or equal to 1 ; Not required sub - sequences which turn required on adding i ; Required sub - sequence till now will be required sequence plus sub - sequence which turns required ; Similarly for not required ; Also updating total required and not required sub - sequences ; Also , storing values in dp ; Driver code
using System ; public class GFG { static int N = 10000 ; static int count_required_sequence ( int n , int [ ] arr ) { int total_required_subsequence = 0 ; int total_n_required_subsequence = 0 ; int [ , ] dp = new int [ N , 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { int turn_required = 0 ; for ( int j = - 1 ; j <= 1 ; j ++ ) turn_required += dp [ arr [ i ] + j , 0 ] ; int required_end_i = ( total_required_subsequence + turn_required ) ; int n_required_end_i = ( 1 + total_n_required_subsequence - turn_required ) ; total_required_subsequence += required_end_i ; total_n_required_subsequence += n_required_end_i ; dp [ arr [ i ] , 1 ] += required_end_i ; dp [ arr [ i ] , 0 ] += n_required_end_i ; } return total_required_subsequence ; } static public void Main ( ) { int [ ] arr = { 1 , 6 , 2 , 1 , 9 } ; int n = arr . Length ; Console . WriteLine ( count_required_sequence ( n , arr ) ) ; } }
Iterative approach to print all combinations of an Array | C # implementation of the approach ; Data array for combination ; Number of elements in the combination ; The boolean array ; Starting index of the 1 st tract of trues ; Ending index of the 1 st tract of trues ; Constructor ; Set the 1 st r Booleans to true , initialize Start and End ; Generate the very first combination ; Update the starting ending indices of trues in the boolean array ; Function that returns true if another combination can still be generated ; Function to generate the next combination ; Only one true in the tract ; Move the End and reset the End ; Set all the values to false starting from index Start and ending at index End in the boolean array ; Set the beginning elements to true ; Reset the End ; Function to print the combination generated previouslt ; If current index is set to true in the boolean array then element at current index in the original array is part of the combination generated previously ; Driver code
using System ; namespace IterativeCombination { class Combination { private int [ ] Indices ; private int R ; private bool [ ] Flags ; private int Start ; private int End ; public Combination ( int [ ] arr , int r ) { this . Indices = arr ; this . R = r ; } public void GetFirst ( ) { Flags = new bool [ this . Indices . Length ] ; for ( int i = 0 ; i < this . R ; ++ i ) { Flags [ i ] = true ; } this . Start = 0 ; this . End = this . R - 1 ; this . Output ( ) ; } public bool HasNext ( ) { return End < ( this . Indices . Length - 1 ) ; } public void Next ( ) { if ( this . Start == this . End ) { this . Flags [ this . End ] = false ; this . Flags [ this . End + 1 ] = true ; this . Start += 1 ; this . End += 1 ; while ( this . End + 1 < this . Indices . Length && this . Flags [ this . End + 1 ] ) { ++ this . End ; } } else { if ( this . Start == 0 ) { Flags [ this . End ] = false ; Flags [ this . End + 1 ] = true ; this . End -= 1 ; } else { Flags [ this . End + 1 ] = true ; for ( int i = this . Start ; i <= this . End ; ++ i ) { Flags [ i ] = false ; } for ( int i = 0 ; i < this . End - this . Start ; ++ i ) { Flags [ i ] = true ; } this . End = this . End - this . Start - 1 ; this . Start = 0 ; } } this . Output ( ) ; } private void Output ( ) { for ( int i = 0 , count = 0 ; i < Indices . Length && count < this . R ; ++ i ) { if ( Flags [ i ] ) { Console . Write ( Indices [ i ] ) ; Console . Write ( " ▁ " ) ; ++ count ; } } Console . WriteLine ( ) ; } } class AppDriver { static void Main ( ) { int [ ] arr = { 0 , 1 , 2 , 3 } ; int r = 3 ; Combination com = new Combination ( arr , r ) ; com . GetFirst ( ) ; while ( com . HasNext ( ) ) { com . Next ( ) ; } } } }