ডিস্টিংクト সাবসেট সমস্যাটি আমাদের দেওয়া অ্যারেতে থেকে বিভিন্ন সংমিশ্রণ দেয়।
যখন টার্গেট 2, তখন অ্যারেতে থেকে আমরা সংখ্যা 2-এর সাথে মিলে যাওয়া সব সংমিশ্রণ নিই। যখন টার্গেট 3, তখন সংখ্যা 3-এর সাথে মিলে যাওয়া সব সংমিশ্রণ নিই। নিচের উদাহরণে অ্যারে [1,2,3] এবং টার্গেট 2। তাই আমরা সংখ্যা 2-এর সাথে মিলে যাওয়া সব সংমিশ্রণ নিই: "1,2", "2,3", "1,3"।
উদাহরণ
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ConsoleApplication
{
public class BackTracking
{
public void Subsets(int[] array)
{
List<int> currentList = new List<int>();
List<string> results = new List<string>();
BackTrackkingCombination(array, 2, 0, currentList, results);
foreach (var item in results)
{
StringBuilder s = new StringBuilder();
foreach (var item1 in item)
{
s.Append(item1.ToString());
}
Console.WriteLine(s);
s = null;
}
}
public void BackTrackkingCombination(int[] array, int size, int startIndex, List<int> currentList, List<string> results)
{
if (currentList.Count == size)
{
StringBuilder s = new StringBuilder();
foreach (var item in currentList)
{
s.Append(item);
}
results.Add(s.ToString());
return;
}
for (int i = startIndex; i < array.Length; i++)
{
currentList.Add(array[i]);
BackTrackkingCombination(array, size, i + 1, currentList, results);
currentList.Remove(array[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
BackTracking b = new BackTracking();
int[] arrs = { 1, 2, 3 };
b.Subsets(arrs);
}
}
}
আউটপুট
12 13 23