তিনটি সংখ্যা R, G, B এবং অক্ষর 'R', 'G', 'B' মাত্র দেওয়া আছে। লক্ষ্য হলো সম্ভাব্য স্ট্রিংয়ের সংখ্যা বের করা যাতে সেটিতে ন্যূনতম R সংখ্যক 'R', ন্যূনতম G সংখ্যক 'G' এবং ন্যূনতম B সংখ্যক 'B' থাকে। R, G, B-এর যোগফল স্ট্রিং দৈর্ঘ্যের চেয়ে কম বা সমান হবে।
ইনপুট
R = 1, G = 1, B = 1, length=3
আউটপুট
Count of number of strings (made of R, G and B) using given combination are: 6
ব্যাখ্যা
সম্ভাব্য স্ট্রিংগুলো: "RGB", "RBG", "BRG", "BGR", "GRB", "GBR"। অর্থাৎ RGB-এর পারমুটেশন।
ইনপুট
R = 2, G = 0, B = 2, length=4
আউটপুট
Count of number of strings (made of R, G and B) using given combination are: 6
ব্যাখ্যা
সম্ভাব্য স্ট্রিংগুলো: "RRBB", "BBRR", "RBRB", "BRBR", "RBBR", "BRRB"।
প্রোগ্রামে ব্যবহৃত পদ্ধতি:
এই পদ্ধতিতে আমরা প্রথমে R, B, G সংখ্যক অক্ষর নিই। এখন বাকি দৈর্ঘ্য - (R+G+B) অক্ষর দিয়ে সব পারমুটেশন ও কম্বিনেশন বের করে কাউন্টে যোগ করা হয়।
- ইন্টিজার ভ্যালু R, G, B ইনপুট নেওয়া হয়।
sizeস্ট্রিং দৈর্ঘ্য হিসেবে নেওয়া হয়।combination(int R, int G, int B, int size)ফাংশন সব ইনপুট নিয়ে স্ট্রিং কাউন্ট রিটার্ন করে।- ইনিশিয়াল কাউন্ট ০। বাকি কাউন্ট
temp = size - (R + G + B)। size+1দৈর্ঘ্যেরarrঅ্যারে পারমুটেশন ভ্যালু রাখতে।- ইনিশিয়ালি
arr[i]-এ ফ্যাক্টোরিয়াল স্টোর করা হয়।arr[0]=1,arr[i]=arr[i-1]*i। - কম্বিনেশন ক্যালকুলেশনের জন্য আবার দুটি ফর লুপ।
i=0থেকেtemp,j=0থেকেtemp-i।temp_2 = temp - (i + j)।temp_3 = arr[i+R] * arr[j+B] * arr[temp_2+G]।arr[size] / temp_3কাউন্টে যোগ করা হয়।- সব লুপ শেষে
countরিটার্ন।
উদাহরণ
#include <bits/stdc++.h>
using namespace std;
int combination(int R, int G, int B, int size) {
int count = 0;
int temp = size - (R + G + B);
int arr[size+1];
arr[0] = 1;
for (int i = 1; i <= size; i++) {
arr[i] = arr[i - 1] * i;
}
for (int i = 0; i <= temp; i++) {
for (int j = 0; j <= temp-i; j++) {
int temp_2 = temp - (i + j);
int temp_3 = arr[i + R] * arr[j + B] * arr[temp_2 + G];
count += arr[size] / temp_3;
}
}
return count;
}
int main() {
int R = 2, G = 1, B = 1;
int size = 4;
cout << "Count of number of strings (made of R, G and B) using given combination are: " << combination(R, G, B, size);
return 0;
}
আউটপুট
Count of number of strings (made of R, G and B) using given combination are: 12