এই সমস্যাতে, একটি পূর্ণসংখ্যা n দেওয়া থাকে। n-এর বাইনারি রিপ্রেজেন্টেশনে একটি সেট বিট (1) কে ০ করে সম্ভব সর্বোচ্চ ছোট সংখ্যা বের করতে হবে।
উদাহরণ
n = 3 (011₂) → সেট বিট ফ্লিপ: 001 (1) বা 010 (2) → সর্বোচ্চ = 2
লজিক
সর্বোচ্চ ছোট সংখ্যা পাওয়ার জন্য ডানপাশের সেট বিট (rightmost set bit) কে ০ করুন।
Rightmost set bit পাওয়ার ফর্মুলা: log2(n & -n) + 1
C++ প্রোগ্রাম উদাহরণ
#include <iostream>
#include <math.h>
using namespace std;
int returnRightSetBit(int n) {
return log2(n & -n) + 1;
}
void previousSmallerInteger(int n) {
int rightBit = returnRightSetBit(n);
cout << (n & ~(1 << (rightBit - 1)));
}
int main() {
int n = 3452;
cout << "The number is " << n << "\nThe greatest integer smaller than the number is: ";
previousSmallerInteger(n);
return 0;
}
আউটপুট
The number is 3452 The greatest integer smaller than the number is: 3448