+6

# Need help in a algorithm.

I can't create a algorithm for this problem : Take input of number consisting of four digits. generate the maximum and minimum numbers using the four digits entered. Try it for 2005.

7/22/2019 12:39:09 PM

LIOÑ

+3

LIOÑ In your code initialize min and max to 0 Then in the for loops, first multiply then add, instead of adding first and then multiplying.

+7

LIOÑ Take the no. convert to string, sort ascending, chop any leading 0, you have minimum num. Sort decending or read previous sorted string from back, you have the max no.

+5

~ swim ~ thanks man👍

+4

~ swim ~ can you help me modify this code. https://code.sololearn.com/c2n9dAgXADDo/?ref=app

+4

Take a look at these codes... https://code.sololearn.com/c9MED063I58H/?ref=app https://code.sololearn.com/cu27hs85Hzav/?ref=app Basic approach to find maximum number: extract the digits in number and sort them in descending order using any sorting algorithm. Similarly, to find the minimum number, sort the digits in ascending order and check if there are any leading zeros. If there is any zero at first digit, replace it with first non-zero digit.

+4

in c++ you can make a vector of these 4 digits . and then sort it for example/ vector<int> digits; //for minimum sort(digits.begin(), digits.end()); //for maximum sort(digits.end(), digits.begin());

+2

LIOÑ Ok so you are taking 4 ints separately. The logic is more or less same. If you want your code to be modified, i'll need to go through it first. And i assume all nos entered are between 0 to 9. Guve me sometime.

+2

Use array with 4 numbers. Sort the array. Get numbers for index 0 and 3. Use List in Python. 🤔

+2

Hope this helps.. .!! https://code.sololearn.com/cvWpXjXRyEG5/?ref=app

+1

LIOÑ 👍☺

+1

Kuri Probably it could, but the question has a C++ tag ;-)