Introduction
Numbers are one of the first things that appear in DSA, and they are much friendlier than they look. A number can be checked, divided, broken into digits, counted, and compared. These small actions become the base for many beginner problems.
Sometimes a task may ask whether a number is odd or even. Sometimes it may ask how many digits are present in a number. Sometimes each digit may need to be checked separately. All these tasks are based on a few simple ideas: finding remainders, looking at the last digit, and removing digits one by one.
This article starts from the smallest useful idea, the modulo operator, and then slowly builds up to odd/even checking, digit extraction, counting digits, counting odd digits, and finding the largest digit.
1. Modulo Operator
Suppose a number is divided by another number, and the remainder is to be found. This remainder value can be found using the modulo operator, written as %. If a % b is calculated, it means dividing a by b and returning the remainder.
The idea comes from a simple question: “After equal sharing, what is still left?” If 17 candies are shared in groups of 5, three full groups can be made, but 2 candies cannot form another full group. That leftover part is the remainder. If the remainder is 0, the division is clean. If the remainder is not 0, something is left behind.
Understanding Modulo Operator
Key Points
If a is completely divisible by b, then a % b becomes 0. For example, 20 % 4 = 0.
If a is smaller than b, then a % b becomes a itself. For example, 3 % 5 = 3.
The divisor b should not be 0 because division by 0 is not valid.
Example 1
Input: a = 17, b = 5
Output: 2
Explanation: 17 can make three complete groups of 5, which uses 15. After that, 2 is left, so 17 % 5 = 2.
Example 2
Input: a = 3, b = 5
Output: 3
Explanation: 5 cannot be taken even once from 3, so 3 remains as the remainder.
Algorithm
Take the dividend and divisor. The dividend is the number being divided, and the divisor is the size of each group.
Form as many complete groups of the divisor as possible because the remainder can only be found after full groups are removed.
Find the value that could not fit into another full group. This leftover value is the modulo result.
Return the leftover value. If the dividend is smaller than the divisor, no full group can be formed, so the dividend itself becomes the remainder.
Complexity Analysis
Time Complexity: O(1), because the remainder is found using one direct arithmetic operation.
Space Complexity: O(1), because no extra storage is required.
2. Checking if a Number is Odd or Even
Suppose a number is given, and it needs to be checked whether it can be split perfectly into pairs, meaning finding is it even or odd. If every item gets a partner, the number is even. If one item is left without a partner, the number is odd. Since pairs are made in groups of 2, this can be checked using n % 2.
The logic comes from the idea of pairing. If division by 2 leaves nothing behind, all pairs are complete. If something is left behind, one value did not get paired. So a remainder of 0 means even, and any non-zero remainder means odd.
Key Points
0 is even because 0 % 2 = 0.
Negative integers can also be checked using the same divisibility idea.
Odd/even classification is generally used for integers, not decimal values.
Example 1
Input: n = 24
Output: Even
Explanation: 24 is completely divisible by 2, so it is an even number.
Example 2
Input: n = 17
Output: Odd
Explanation: 17 leaves a remainder of 1 when divided by 2, so it is an odd number.
Algorithm
Check the remainder when the number is divided by 2 because odd and even numbers are based on making pairs.
If the remainder is 0, mark the number as even because every unit fits into a pair.
If the remainder is not 0, mark the number as odd because one unit is left after pairing.
Handle 0 as even because it follows the same remainder rule.
Dry Run
Odd Even Check Dry Run
Complexity Analysis
Time Complexity: O(1), because only one modulo operation is needed.
Space Complexity: O(1), because no extra data structure is used.
3. Extracting Digits from a Number
Suppose a number is given, and each digit needs to be looked at separately. For example, in 5834, the digits are 5, 8, 3, and 4. The easiest digit to access is the last digit. In the decimal number system, n % 10 gives the last digit.
The logic comes from the way decimal numbers are built around 10. The last digit always sits in the ones place. When a number is divided by 10, the ones-place digit is left as the remainder. After that digit is extracted, integer division by 10 removes it, so the next digit becomes available.
Extracting Digits of a Number
Key Points
For negative numbers, the absolute value is usually taken first , as this does not impact the digits.
0 itself is a digit and should be handled carefully.
Example 1
Input: n = 5834
Output: 4 3 8 5
Explanation: Digits are extracted from the right side, so 4 is extracted first, followed by 3, 8, and 5.
Example 2
Input: n = 907
Output: 7 0 9
Explanation: Digits are extracted from the right side, so 7 is extracted first, followed by 0, and 9.
Algorithm
Extract the last digit using n % 10 because the last digit is the remainder when a decimal number is divided by 10.
Process the extracted digit according to the need of the problem. The digit may be counted, checked, compared, or added.
Remove the last digit using integer division by 10 so that the next digit becomes available.
Continue until the number becomes 0 because no digits are left after that point.
Handle negative numbers using their absolute value because the minus sign is not a digit.
Complexity Analysis
Time Complexity: O(d), where d is the number of digits, because each digit is extracted once.
Space Complexity: O(1), because only a few variables are needed.
4. Counting Digits in a Number
Suppose the task is to find how many digits are present in a number. For example, 5834 has 4 digits, while 90 has 2 digits. The number 0 also has 1 digit because 0 itself is a digit.
The logic comes from digit extraction. Every time the last digit is removed, one digit has definitely been found. So instead of looking at the whole number at once, the number is shortened step by step while a counter keeps track of how many digits were removed.
Key Points
0 has one digit.
Negative numbers should be converted to positive before counting digits.
Numbers ending with 0, such as 1200, still count those zeros as digits.
Example 1
Input: n = 5834
Output: 4
Explanation: The digits are 5, 8, 3, and 4, so the total count is 4.
Example 2
Input: n = 0
Output: 1
Explanation: 0 itself is a digit, so the digit count is 1.
Algorithm
Handle 0 separately by returning 1 because 0 is a valid digit, but the usual digit-removal loop would not run for it.
Convert the number to its absolute value if it is negative because the minus sign is not counted.
Initialize a digit counter as 0 to store how many digits have been removed so far.
Remove the last digit using integer division by 10 because each removal confirms that one digit existed.
Increase the counter after every removal so the count stays matched with the number of processed digits.
Continue until the number becomes 0. At that point, all digits have been removed.
Dry Run
Counts Digits Dry Run
Complexity Analysis
Time Complexity: O(d), where d is the number of digits, because one digit is removed in every step.
Space Complexity: O(1), because only a counter and the current number are maintained.
5. Counting Odd Digits in a Number
Suppose a number is given, and only the odd digits inside it need to be counted. For example, in 5834, the odd digits are 5 and 3, so the answer is 2.
The logic combines two earlier ideas. First, each digit is extracted using n % 10. Then the extracted digit is checked using digit % 2. If the digit is odd, the count is increased. If it is even, it is simply skipped.
Key Points
Odd digits are 1, 3, 5, 7, and 9.
0 is not an odd digit because it is even.
For negative numbers, only the digits are checked.
Repeated odd digits are counted each time they appear.
Algorithm
Convert the number to its absolute value if it is negative because only digits matter.
Initialize an odd digit counter as 0 to store how many odd digits have been found.
Extract the last digit using n % 10 so each digit can be checked separately.
Check the extracted digit using digit % 2. If the remainder is not 0, the digit is odd.
Increase the counter only when the digit is odd so even digits do not affect the answer.
Remove the processed digit using integer division by 10 so the next digit becomes available.
Continue until the number becomes 0. Once all digits are processed, the counter gives the answer.
Dry Run
Counts Odd Digits Dry Run
Complexity Analysis
Time Complexity: O(d), where d is the number of digits, because every digit is checked once.
Space Complexity: O(1), because only the current digit and a counter are maintained.
6. Finding the Largest Digit in a Number
Suppose a number is given, and the biggest digit inside it needs to be found. In 5834, the digits are 5, 8, 3, and 4. The largest digit is 8.
The logic is based on comparison. Each digit is extracted one by one and compared with the largest digit found so far. If a bigger digit appears, the answer is updated. Since decimal digits range only from 0 to 9, finding 9 means the best possible answer has already been found.
Key Points
Decimal digits range from 0 to 9, so the largest possible digit is 9.
If 9 is found, the search can stop early.
If the number is 0, the largest digit is 0.
For negative numbers, the sign is ignored.
Example 1
Input: n = 5834
Output: 8
Explanation: The digits are 5, 8, 3, and 4. The largest digit is 8.
Example 2
Input: n = 90715
Output: 9
Explanation: Among all digits, 9 is the largest.
Algorithm
Convert the number to its absolute value if it is negative because the sign does not affect the largest digit.
Handle 0 separately by returning 0 because the only digit present is 0.
Initialize the largest digit as 0 because no decimal digit is smaller than 0.
Extract the last digit using n % 10 so one digit can be compared at a time.
Compare the extracted digit with the current largest digit. If the extracted digit is bigger, update the largest digit.
Stop early if the largest digit becomes 9 because no decimal digit can be greater than 9.
Remove the processed digit using integer division by 10 and continue until the number becomes 0.
Dry Run
Find Largest Digit in a Number
Complexity Analysis
Time Complexity: O(d), where d is the number of digits, because each digit may need to be checked once.
Space Complexity: O(1), because only the current digit and largest digit are maintained.
Key Takeaways
The modulo operator gives the leftover value after division.
n % 2 helps check whether a number is odd or even.
n % 10 extracts the last digit of a number.
Integer division by 10 removes the last digit.
Counting digits means removing digits one by one and increasing a counter.
Counting odd digits means extracting each digit and applying the odd/even rule.
Finding the largest digit means comparing each digit with the best digit found so far.
0 should be handled carefully because it is a valid one-digit number.
Interview follow-up Questions
The modulo operator is designed to return the leftover value after a is divided by b. It ignores the quotient and focuses only on what remains.
Be the first to add a comment.