इस समस्या में, हमें एक संख्या n दी गई है। हमारा कार्य n से छोटी सबसे बड़ी संख्या प्रिंट करना है जिसके सभी डिजिट अलग-अलग हों।
उदाहरण
Input: n = 2332 Output: 2319
इस समस्या को हल करने के लिए, हम संख्याओं की रिवर्स काउंटिंग करेंगे यानी n से 0 तक। और अलग-अलग डिजिट वाली संख्या की जांच करेंगे। यदि वर्तमान काउंट मान शर्त को पूरा करता है तो इसे प्रिंट करें और लूप समाप्त करें। अन्यथा लूप जारी रखें। लूप अधिकतम n बार चलेगा।
उदाहरण
हमारे समाधान को लागू करने वाला प्रोग्राम:
#include <bits/stdc++.h>
using namespace std;
int findDistinctDigitNumber(int n) {
for (int i = n - 1; i >= 0; i--) {
int count[10] = { 0 };
int x = i;
int count1 = 0, count2 = 0;
while (x) {
count[x % 10]++;
x /= 10;
count1++;
}
for (int j = 0; j < 10; j++) {
if (count[j] == 1)
count2++;
}
if (count1 == count2)
return i;
}
}
int main() {
int n = 44324;
cout << "Number less than " << n << " with all digits distinct are : " << findDistinctDigitNumber(n);
return 0;
}
आउटपुट
Number less than 44324 with all digits distinct are : 43987