This is my solution to LeetCode Problem 1342 - Number of Steps to Reduce a Number to Zero - in C# and a brief explanation
Link to problem on LeetCode -> https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/
I have created a new project in Visual Studio 2022 Community Edition, selected the Console App project template, named the project NumberOfStepsToReduceANumberToZero, selected .NET Core 3.1 (Out of support) Framework.
I have two classes, Program class and Solution class.
I am using the automatically generated Program class and Main function.
internal class Program
{
static void Main(string[] args)
{
Firstly, the num numeric input (int) is initialized by using basic int initialization. This part of code is commented out currently.
int num = 14;
Currently, the num numeric input (int) is initialized by using next structure:
- Console Write -> where it displays that the value of num needs to be inputted.
- int input = int Pars Console ReadLine -> where user input is stored into the numeric input (int).
Console.Write("Enter value of number num: ");
int num = int.Parse(Console.ReadLine());
CODE EXPLAINED IN GREATER DETAIL
- Console.ReadLine always takes user input in a text format (string) . To convert the text input (string) into a numeric input (int) -> int Parse needs to be used.
Next, the NumberOfSteps function from Solution class is called. It takes an int (int num) and returns an int (int output). The returned int is stored into a new int named numberOfSteps. The returned int is then printed out to the console.
int numberOfSteps = Solution.NumberOfSteps(num);
Console.WriteLine(numberOfSteps);
At the end of the Program class there is next structure:
- Console WriteLine -> where it displays that the code is done processing.
- Console ReadLine -> where it waits for user input.
WHY DOES CONSOLE READLINE WAIT FOR USER INPUT?
To stop the code from automatically completing and exiting and us not being able to check the result of the code.
Console.WriteLine("Done processing");
Console.ReadLine();
I have created a new class and named it Solution. I have made it public. I have created the NumberOfSteps function and mirrored the example provided in the LeetCode problem. I have made it static.
public class Solution
{
public static int NumberOfSteps(int num)
{
Firstly, the output numeric input (int) is initialized by using basic int initialization. It is set to 0. This numeric input (int) will be used for counting the number of times the operation needs to run.
int output = 0;
Secondly, the while loop is initialized that iterates until the value of num is equal to 0.
while (num != 0)
{
Inside the while loop, there is an if and else statement.
- The if statement, checks if the result of modulus 2 of a number is equal to 0. If the statement is true, the value of num is divided by 2.
- If the if statement is false, i.e. the result of modulus 2 of a number is not equal to 0, the else statement runs. The value of num is subtracted by 1.
if (num % 2 == 0) { num /= 2; }
else { num--; }
The value of output is incremented by 1.
WHY IS THE VALUE OF OUTPUT INCREMENTED BY 1 AFTER THE IF-ELSE STATEMENTS, AND NOT INSIDE OF THEM?
Because output needs to be incremented by 1 regardless of the operation that ran. The number of operations increases by 1 be it the division by 2 operation, or subtraction by 1 operation.
output ++;
At the end of the Solution class, the output numeric input (int) is returned.
return output;
In Solution class, instead of doing a while loop, I did a do-while loop.
I have made this error based on the assumption that the code needs to run at least once.
The difference between a while and a do-while loop is that a while loop first checks the condition, and then runs the code inside. The result can be such that, if the condition is false, the code inside the while loop does not run at all. The do-while loop on the other hand, runs the code once inside no matter the condition, and then checks if the condition is true.
WHY IS THE DO-WHILE LOOP THE WRONG CHOICE HERE, AND THE WHILE LOOP THE RIGHT CHOICE?
Because 0 can be provided as num value.
WHAT WILL BE THE OUTPUT OF DO-WHILE LOOP IF 0 IS PROVIDED AS NUM VALUE?
The output of the do-while loop will be 1, because the code inside will run once, no matter the condition.
Neither the if and the else statement will mark as true, so the value of num will remain as 0, but the output value will get increased by 1, even though none of the operations ran successfully.
This is why the do-while is a wrong choice here. It is incorrect that number 0 needs 1 operation to be reduced to 0.
WHAT WILL BE THE OUTPUT OF WHILE LOOP IF 0 IS PROVIDED AS NUM VALUE?
The output of the while loop will be 0, because first the condition will be cheked and it will be marked as false.
The code inside the while loop will not run at all. This is why the while loop is a right choice here.
It is correct that numer 0 needs 0 operations to be reduced to 0.
In the RUNNING THE CODE - RESULT part of this README, Additional, to the example testcases, I have included the testcase of value of num as 0, so that the correct result can be observed.
do{
...
}while (num != 0);
Any advice regarding making the code MORE TIME AND SPACE EFFICIENT is MORE THAN WELCOME.