আমাদের n বিট দেওয়া আছে। ২n দৈর্ঘ্যের বাইনারি সিকোয়েন্স বানাতে হবে যেন এর প্রথম n বিট ও দ্বিতীয় n বিটের যোগফল (১-এর সংখ্যা) সমান হয়।
প্রথম অর্ধে k সংখ্যক ১ থাকার উপায় = nCk। দ্বিতীয় অর্ধেও k সংখ্যক ১ থাকার উপায় = nCk। মোট কম্বিনেশন = Σ (nCk)², k=0 থেকে n।
ইনপুট
n=1
আউটপুট
Sequences with same sum of first and second half bits: 2
ব্যাখ্যা: ২-বিট সিকোয়েন্স: 00,01,10,11। 01 ও 10-এর সম ১।
ইনপুট
n=2
আউটপুট
Sequences with same sum of first and second half bits: 6
ব্যাখ্যা: ৪-বিট সিকোয়েন্স ১৬টি। সম সম হলে: 0000, 0101, 0110, 1001, 1010, 1111 (৬টি)۔
প্রোগ্রামে ব্যবহৃত পদ্ধতি
- findSeq(int n) ফাংশন।
- nCi = 1 (nC0)। ans = 1।
- i=1 থেকে n: nCi = nCi * (n+1-i) / i; ans += nCi * nCi।
- ans রিটার্ন।
উদাহরণ কোড
#include <iostream>
using namespace std;
int findSeq(int n){
int nCi = 1;
int ans = 1;
for (int i = 1; i <= n; i++){
nCi = (nCi * (n + 1 - i)) / i;
ans += nCi * nCi;
}
return ans;
}
int main(){
int bits = 2;
cout << "Count of binary sequences such that sum of first and second half bits is same: " << findSeq(bits);
return 0;
}
আউটপুট
Count of binary sequences such that sum of first and second half bits is same: 6