Print inorder combinations of an array

Take an array and print non over lapping in order pairs.
Example:
# [1,2,3,4] => input

# output below is in order combination

# (1234)
# (1)(234)
# (1)(23)(4)
# (1)(2)(34)
# (12)(34)
# (12)(3)(4)
# (123)(4)
# (1)(2)(3)(4)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        public static char[] charArray = { '1', '2', '3', '4', '5', '6' };

        public static void Main(string[] args)
        {
            int[] stepArray = new int[charArray.Length];
            for(int i = 0; i < stepArray.Length; i++)
            {
                stepArray[i] = i + 1;
            }
            PrintInorderCombination(charArray.Length, stepArray, 0, "");
        }

        public static void PrintInorderCombination(int sum, int[] stepArray, int tempSum, string str)
        {
            if (tempSum > sum)
            {
                return;
            }
            else if (tempSum == sum && str.Length != 0)
            {
                Regex reg = new Regex(" ");
                string[] combinationString = reg.Split(str);
                int counter = 0;
                for(int i = 0; i < combinationString.Length; i++)
                {
                    Console.Write("(");
                    for (int j = 0; j < int.Parse(combinationString[i]); j++)
                    {
                        Console.Write(charArray[counter++]);
                    }
                    Console.Write(")");
                }
                Console.WriteLine();
            }
            else
            {
                for (int i = 0; i < stepArray.Length; i++)
                {
                    if (str == "")
                    {
                        PrintInorderCombination(sum, stepArray, tempSum + stepArray[i], str + stepArray[i].ToString());
                    }
                    else
                    {
                        PrintInorderCombination(sum, stepArray, tempSum + stepArray[i], str + " " + stepArray[i].ToString());
                    }
                }
            }
        }
    }
}

Output
=======
(1)(2)(3)(4)(5)(6)
(1)(2)(3)(4)(56)
(1)(2)(3)(45)(6)
(1)(2)(3)(456)
(1)(2)(34)(5)(6)
(1)(2)(34)(56)
(1)(2)(345)(6)
(1)(2)(3456)
(1)(23)(4)(5)(6)
(1)(23)(4)(56)
(1)(23)(45)(6)
(1)(23)(456)
(1)(234)(5)(6)
(1)(234)(56)
(1)(2345)(6)
(1)(23456)
(12)(3)(4)(5)(6)
(12)(3)(4)(56)
(12)(3)(45)(6)
(12)(3)(456)
(12)(34)(5)(6)
(12)(34)(56)
(12)(345)(6)
(12)(3456)
(123)(4)(5)(6)
(123)(4)(56)
(123)(45)(6)
(123)(456)
(1234)(5)(6)
(1234)(56)
(12345)(6)
(123456)
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s