কম্পিউটার টিউটোরিয়াল

C# ব্যবহার করে ব্যাকট্র্যাকিংয়ের মাধ্যমে দেওয়া অ্যারেতে থেকেISTINCT সাবসেট খুঁজে বের করার পদ্ধতি


ডিস্টিংクト সাবসেট সমস্যাটি আমাদের দেওয়া অ্যারেতে থেকে বিভিন্ন সংমিশ্রণ দেয়।

যখন টার্গেট 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
  1. C#-এ ল্যাম্বডা এক্সপ্রেশন দিয়ে অ্যারে থেকে সর্বনিম্ন মান পাওয়া

  2. C#-এ অ্যারের র‍্যাঙ্ক (Rank) বের করা

  3. C#-এ জ্যাগড অ্যারে (Jagged Array) এর দৈর্ঘ্য বের করা

  4. C#-এ অ্যারের দৈর্ঘ্য কীভাবে জানবেন?