In order to sort list of numbers using radix sort algorithm, we need to get…
2017
In order to sort list of numbers using radix sort algorithm, we need to get the individual digits of each number ‘n’ of the list. If n is a positive decimal integer, then ith digit, from right, of the number n is:
Answer: A. ⌊ n / 10i − 1 ⌋ % 10 — To extract the i-th digit of a positive decimal integer n, counted from the right and starting at i = 1, first remove the rightmost (i − 1) digits with an…
- A.
⌊ n / 10i − 1 ⌋ % 10
- B.
⌈ n / 10i ⌉ % 10
- C.
⌈ n / 10i − 1 ⌉ % 10
- D.
⌊ n / 10i ⌋ % 10
Attempted by 322 students.
Show answer & explanation
Correct answer: A
To extract the i-th digit of a positive decimal integer n, counted from the right and starting at i = 1, first remove the rightmost (i − 1) digits with an integer (floor) division by 10 raised to the power (i − 1), then isolate the resulting units digit with modulo 10. Formally: digiti(n) = ⌊n / 10i − 1⌋ % 10. Using a ceiling function in place of the floor, or dividing by the wrong power of 10, generally returns a different digit — though not on every input, since ceiling and floor coincide when the quotient is already a whole number, and a shifted position can occasionally coincide with the target digit by chance.
Compute the divisor: 10 raised to the power (i − 1) = 10 raised to the power (3 − 1) = 102 = 100.
Divide n by the divisor: 250 / 100 = 2.5.
Apply the floor function: ⌊2.5⌋ = 2.
Apply modulo 10: 2 % 10 = 2 — this is the 3rd digit of 250 counted from the right (250 read right-to-left gives digits 0, 5, 2 at positions 1, 2, 3).
Cross-check with i = 1, the units digit (which is 0 for n = 250): ⌊250 / 100⌋ % 10 = ⌊250 / 1⌋ % 10 = 250 % 10 = 0 — correct, confirming the formula generalizes to every position.
⌈n / 10i − 1⌉ % 10 rounds the quotient up before the modulo instead of down, so at i = 3 it gives ⌈2.5⌉ % 10 = 3 % 10 = 3, not 2 — because the ceiling function rounds any non-integer quotient up to the next whole number, it returns a different digit than the floor-based formula whenever the true quotient is not already an integer.
⌊n / 10i⌋ % 10 divides by one extra power of 10, so at i = 3 it gives ⌊250 / 1000⌋ % 10 = 0 % 10 = 0 — the value that belongs to the (i + 1)-th position, not the i-th.
⌈n / 10i⌉ % 10 combines both deviations — the extra power of 10 and the upward rounding — giving ⌈250 / 1000⌉ % 10 = 1 % 10 = 1 at i = 3, which matches neither the i-th nor the (i + 1)-th digit.
So ⌊n / 10i − 1⌋ % 10 is the expression that returns the i-th digit from the right for every valid i.
Explore the full course: Iocl Engineers Officers Grade A Paper 2