इस समस्या में, हमें एक इंटीजर n दिया गया है। हमारा कार्य n से कम सबसे बड़ी संख्या प्रिंट करना है जिसे संख्या के बाइनरी प्रतिनिधित्व के एक सेट बिट को बदलकर बनाया जा सकता है।
समस्या को समझने के लिए एक उदाहरण लेते हैं
Input: n = 3 Output: 2 Explanation: (3)10 = (011)2 एक सेट बिट फ्लिप करने पर 001 और 010 मिलते हैं। 010 बड़ा है यानी 2।
इस समस्या को हल करने के लिए, हमें राइटमोस्ट सेट बिट को फ्लिप करके शून्य बनाना होगा जो उस संख्या से कम सबसे बड़ी संभव संख्या बनाएगा जो संख्या के एक बिट को फ्लिप करके पाई जाती है।
हमारे समाधान के कार्यान्वयन को दिखाने वाला प्रोग्राम,
उदाहरण
#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