Algorithm Atlas · worked example
See the algorithm, step by step
Compare the end-pair sum with the target and discard one impossible endpoint.
This call is starting. Local values appear as they are initialized.
Data & variables
Changed Source indexExample input
[1,8,9]Follow the code. Data appears here when it enters scope.
Call stack · 1 frame
maincall 1
Rust source
Line 17fn sorted_pair(values: &[i32], target: i32) -> Option<(usize, usize)> {
if values.len() < 2 {
return None;
}
let (mut left, mut right) = (0, values.len() - 1);
while left < right {
match (values[left] + values[right]).cmp(&target) {
std::cmp::Ordering::Equal => return Some((left + 1, right + 1)),
std::cmp::Ordering::Less => left += 1,
std::cmp::Ordering::Greater => right -= 1,
}
}
None
}
fn main() {
let result = sorted_pair(&[1, 8, 9], 17).map_or("-1,-1".into(), |(a, b)| format!("{a},{b}"));
println!("{result}");
}
Try your own version
Change the independent fixture `[1,8,9]` in a legal way, then compare the result with your prediction. Explain how the same invariant applies to the separate visible challenge `[2,3,7,11,15]`.
Open personal copy in Playground ↗Start with this exact source and input, change a value, then use Run or Debug locally. This replay stays attached to the original example.
Recordings use a native Rust target. Playground runs on wasm32-wasip1; target-sized types such as usize can have different limits.

Understand it
The output contract uses one-based indexes. The worked program demonstrates this invariant on an independent fixture, so you can study the implementation before predicting the challenge result.
For teaching input [1,8,9], the worked program returns 2,3. Its functions are complete and use only std. Adapt the same state transitions to the visible challenge input [2,3,7,11,15]; predict that result before opening the answer reveal. The precise contract is: For sorted values, return one-based indexes of any two distinct positions summing to 17. Return -1,-1 if no pair exists.
Return canonical output: lowercase booleans, decimal integers without leading zeros, comma-separated values and semicolon-separated groups, with no extra whitespace or final newline. Whole medians use integer form and half medians use .5. Textual sentinels such as none and # retain their stated meaning. String and character-grid fixtures use ASCII unless the lesson explicitly specifies Unicode scalar values.
Solve the exact task, not just the visible example: For sorted values, return one-based indexes of any two distinct positions summing to 17. Return -1,-1 if no pair exists. On input [2,3,7,11,15], predict the output before opening the answer reveal. The revealed output is a test oracle for this case, while private checks exercise other legal cases. Start by stating the input grammar and output format in your own words. Parse the complete input, including surrounding whitespace, before relying on any convenient positions in the example.
Build the solution around one maintained claim: Compare the end-pair sum with the target and discard one impossible endpoint. Decide what state represents that claim, how it is initialized, what a single update does, and when the algorithm stops. Two positions move according to a monotone fact: after one comparison, some candidates can be ruled out permanently. Name that fact. Trace the visible input by hand and record at least two intermediate states, not only the final answer. If the trace cannot justify your predicted output, there is a gap between the idea and the implementation.
Check both correctness and cost. The intended bound is O(n) time · O(1) space; explain which operation dominates it and how many times the input can trigger that operation. Test pointers meeting, crossing, duplicate values, and the smallest valid input; many off-by-one errors hide there. Add one small case that should succeed, one boundary case, and one case chosen to challenge the invariant. Compare your hand result with the program's result, then use a failing case to fix the earliest incorrect state transition rather than patching the final output.
The pattern
Compare the end-pair sum with the target and discard one impossible endpoint.
When to use it: one pair in sorted numeric data
Complexity: O(n) time · O(1) space
Input: [2,3,7,11,15]
Read the code
fn sorted_pair(values: &[i32], target: i32) -> Option<(usize, usize)> {
if values.len() < 2 {
return None;
}
let (mut left, mut right) = (0, values.len() - 1);
while left < right {
match (values[left] + values[right]).cmp(&target) {
std::cmp::Ordering::Equal => return Some((left + 1, right + 1)),
std::cmp::Ordering::Less => left += 1,
std::cmp::Ordering::Greater => right -= 1,
}
}
None
}
fn main() {
let result = sorted_pair(&[1, 8, 9], 17).map_or("-1,-1".into(), |(a, b)| format!("{a},{b}"));
println!("{result}");
}
Complete std-only sorted-two-sum program for its typed teaching fixture. Parsing and output construction are separate from the core algorithm.
Practice starter
pub fn solve(input: &str) -> String {
// For sorted values, return one-based indexes of any two distinct positions summing to 17. Return -1,-1 if no pair exists.
// Trim surrounding whitespace; implement parsing and the algorithm.
let _ = input;
todo!("implement sorted-two-sum")
}
Try it
For sorted values, return one-based indexes of any two distinct positions summing to 17. Return -1,-1 if no pair exists.
What success looks like: The private harness prints PASS sorted-two-sum. It checks at least five distinct semantic inputs plus surrounding-whitespace variants. Explain complexity separately; this harness does not time the algorithm.
Check your understanding
Boundary check for sorted-two-sum: with input [2,5,12,20], what result should solve return?
Reveal the answer
2,333,3
For this input the result is 2,3. The output contract uses one-based indexes. Compare this independent fixture with the worked trace: For [2,3,7,11,15], the outer values sum to 17, so zero-based (0,4) becomes output (1,5). On [1,8,9], the valid result is (2,3). Ordering enables pointer elimination without a map.
Go deeper
Common misconception: “Sorted arrays are internally one-based in Rust”
The output contract uses one-based indexes. The independent teaching fixture demonstrates this rule; predict the separate visible challenge before revealing its answer.
- Pattern-specific invariant: The output contract uses one-based indexes. This is the property the named implementation maintains; explain which comparison, state update or recursive return in the worked program establishes it.
- Independent typed fixture: The worked call uses
[1,8,9]and computes2,3. Keep parsing separate, then identify the core state before and after this call. - Different legal case: For
[2,5,12,20], the independently calculated result is2,3. The complexity discussion uses O(n) time · O(1) space. Verify the state invariant before reasoning about the operation count.
Transfer challenge: Change the independent fixture [1,8,9] in a legal way, then compare the result with your prediction. Explain how the same invariant applies to the separate visible challenge [2,3,7,11,15].
Connections
- Sorted Two-Sum: When to Use It: one pair in sorted numeric data
- Palindrome Pointers: Mental Model: Compare normalized symbols from the outside inward and skip irrelevant input.
Reveal the Algorithm Atlas answer
1,5
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Challenge: Sorted Two-Sum (courseId "algorithms", lessonId "algorithm.sorted-two-sum.challenge") Link: https://crabrix.com/learn/algorithms/algorithm.sorted-two-sum.challenge Goal: The output contract uses one-based indexes. For sorted values, return one-based indexes of any two distinct positions summing to 17. Return -1,-1 if no pair exists. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=algorithms/algorithm.sorted-two-sum.challenge) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.