```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 ``` ```// Q: Find minimum and maximum number made out of all 9 digits which is a perfect square bool isPerfectSquare(const char *s) { int num; { stringstream ss(s); if( !(ss >> num) ) return false; static const bool possible_sq[] = {true, true, false, false, true, true, true, false, false, true}; if( !possible_sq[num % 10] ) return false; } const int c = num; int res = 0; int bit = 1 << 30; while (bit > num) bit >>= 2; while (bit != 0) { if (num >= res + bit) { num -= res + bit; res = (res >> 1) + bit; } else res >>= 1; bit >>= 2; } return (res * res == c); } int main() { char num[] = "123456789"; char *const f = &num[0]; char *const t = &num[9]; bool found = false; do { if( isPerfectSquare(num) ) { cout << "Smallest is: " << num << endl; found = true; break; } } while( next_permutation(f, t) ); if( !found ) { cout << "None found" << endl; return 0; } sort( f, t, std::greater() ); do { if( isPerfectSquare(num) ) { cout << "Largest is: " << num << endl; break; } } while( next_permutation( f, t, std::greater() ) ); } ```
 ```1 2 ``` ```Smallest is: 139854276 Largest is: 923187456 ```