Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › RECURSION BASICS
For the inclusive range 3 to 8, printParity examines one current number at a time. The parity test compares current % 2 with wantedParity. A wantedParity of 0 selects even numbers, while a wantedParity of 1 selects odd numbers. The recursive call stays outside that test, so every number gets a turn to be examined.
void printParity(int current, int end, int wantedParity) {
if (current > end) {
return;
}
if (current % 2 == wantedParity) {
cout << current << " ";
}
printParity(current + 1, end, wantedParity);
}Start with printParity(3, 8, 0). At current = 3, 3 % 2 is 1, so the even test fails and nothing is printed. The call outside the condition still creates printParity(4, 8, 0). That call prints 4, and the same process reaches 6 and 8. Changing wantedParity to 1 changes only the selected output: the same calls visit 3 through 8, but they print 3, 5, and 7.
Which version correctly allows printParity to reach current = 4 when starting at current = 3 with wantedParity = 0?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The stopping test is current > 8, not current == 8. When current is 8, the function must still test its parity and print 8 during the even pass. Only after the call advances to current = 9 does the stopping test succeed, so that final call returns without printing.
void printParity(int current, int end, int wantedParity) {
if (current > end) {
return;
}
if (current % 2 == wantedParity) {
cout << current << " ";
}
printParity(current + 1, end, wantedParity);
}A test such as current == 8 returns too early if it appears before the parity check. That prevents 8 from being printed, even though 8 belongs to the range. The call sequence for 3 to 8 is therefore 3, 4, 5, 6, 7, 8, and finally 9, where the stopping condition returns without examining a range value.
During the even pass, the function reaches current = 4 and prints 4 before creating the call for 5. It later prints 6 and then 8 during the same descent through the range. Because each selected number is printed before the next call, the output follows the order in which the numbers are encountered: 4 6 8.
if (current % 2 == wantedParity) {
cout << current << " ";
}
printParity(current + 1, 8, wantedParity);If you place the recursive call first, the function travels all the way to current = 9 before any selected number is printed. Output then happens while calls return from the call stack. The call for 8 prints first, followed by 6 and 4, so the result becomes 8 6 4.
printParity(current + 1, 8, wantedParity);
if (current % 2 == wantedParity) {
cout << current << " ";
}Place the print operation relative to printParity(current + 1, 8, 0) so the even pass outputs 4 6 8.
// Place these two operations in the correct order
cout << current << " ";
printParity(current + 1, 8, 0);Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Let k be the number of candidates from 3 through 8, so k = 6. Each call does a constant amount of work, performs one recursive call, and advances to the next number. That gives the recurrence T(k) = T(k - 1) + O(1), which resolves to O(k) time. The parity test may reject half the values, but rejected values still cost a call and a constant amount of work.
The call stack holds the chain 3, 4, 5, 6, 7, 8, and the stopping call at 9. That is seven active calls at the deepest point, which is k + 1 for this range and still O(k) space. Printing only 4, 6, and 8 does not reduce the stack space, because every candidate must be visited before the function can stop.
| QUANTITY | FOR THE RANGE 3 TO 8 |
|---|---|
| Candidates, k | 6 values: 3, 4, 5, 6, 7, 8 |
| Stopping call | current = 9 |
| Time recurrence | T(k) = T(k - 1) + O(1) |
| Time complexity | O(k) |
| Deepest stack chain | 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 |
| Space complexity | O(k) |