+ 1

I need answer anyone knows please tell me

Compute nearest larger number by interchanging digits updated. Given 2 numbers a and b find the smallest number greater than b by interchanging the digits of a and if not possible print -1. Constraints 1 <= a,b <= 10000000 Explanation Example 1 Input 459 500 Output 549

29th Jun 2019, 4:21 AM
Kishore Kumar.B
Kishore Kumar.B - avatar
1 Antwort
0
https://code.sololearn.com/c02exV7xZC14 #include <iostream> #include <algorithm> #include <string> std::string nearestLargerNumber (std::string a, const std::string &b); int main() { std::string strA {}, strB {}; std::cout << "Enter a and b seperated by a space: "; std::cin >> strA >> strB; std::cout << std::endl; long long NLN = std::stoll (nearestLargerNumber(strA, strB)); if (NLN > std::stoll(strB)) std::cout << NLN << std::endl; else std::cout << -1 << std::endl; return 0; } std::string nearestLargerNumber (std::string a, const std::string &b) { if (a.size() < b.size()) return a; sort(a.begin(), a.end()); for (size_t i {}; i < b.size(); i++) for (size_t j {i}; j < a.size(); j++) if (a.at(j) >= b.at(i)) { std::swap(a.at(j), a.at(i)); break; } return a; }
29th Jun 2019, 11:27 AM
Mina Yossry
Mina Yossry - avatar