The minimum number without a digit, solve in c++ with while loop
Hello! I have to solve this problem in c++ with while loop. Below I put a solution, but I receive 2/4 correct tests. Careful! I must remove only 1 digit from the number, even if the same digit appears 2 or more times. If we take the number 4832851, I have to eliminate the first digit 8 from left to right, thus resulting in the final number 432851. How could I eliminate only that one, and not both or not on the other digit 8? The problem sounds like that: Requirement A natural number N is read from the keyboard. Calculate the minimum number obtained by removing a single digit from the initial number. Input data Read the natural number N. from the keyboard. Output data The minimum number will be displayed after deleting a digit. Restrictions and specifications 0 <N <1,000,000,000 Example Input data Output data 5912 512 My solution: #include using namespace std; int main() { unsigned long int N, copyOfN, newN; int digitMax = 0, multiply = 1; int counter = 1; cin >> N; copyOfN = N; newN = 0; while (N != 0) { if (N % 10 > digitMax) { digitMax = N % 10; } N = N / 10; } while (copyOfN != 0) { if (copyOfN % 10 != digitMax || counter == 0) { newN = (copyOfN % 10) * multiply + newN; multiply = multiply * 10; } else if (copyOfN % 10 == digitMax && counter == 1) { counter = 0; } copyOfN = copyOfN / 10; } cout << newN; return 0; }