for c++:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <limits> // For numeric_limits
int findSmallestGreaterPermutation(int a, int b) {
std::string s = std::to_string(a);
std::sort(s.begin(), s.end()); // Sort to get the smallest permutation first for next_permutation
long long smallest_greater_num = std::numeric_limits<long long>::max();
bool found = false;
do {
long long current_num = std::stoll(s); // Convert string to long long
if (current_num > b) {
found = true;
if (current_num < smallest_greater_num) {
smallest_greater_num = current_num;
}
}
} while (std::next_permutation(s.begin(), s.end()));
if (found) {
return static_cast<int>(smallest_greater_num); // Cast back to int if within range
} else {
return -1;
}
}
int main() {
int a, b;
std::cout << "Enter two numbers (a and b): ";
std::cin >> a >> b;
int result = findSmallestGreaterPermutation(a, b);
if (result != -1) {
std::cout << "Smallest number greater than " << b << " by interchanging digits of " << a << " is: " << result << std::endl;
} else {
std::cout << "No such number found." << std::endl;
}
return 0;
}