Find all amicable number pairs within a range

Quick Overview

Find every amicable pair of integers inside a given range, where the proper divisors of each number add up to the other number. It tests computing divisor sums for many numbers at once, reasoning about running time, and handling range boundaries without reporting a pair twice.

Find all amicable number pairs within a range

Company: Yahoo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given two integers `low` and `high`, return all amicable pairs `(a, b)` with `low <= a < b <= high`, such that the sum of the proper divisors of `a` equals `b` and the sum of the proper divisors of `b` equals `a`. A proper divisor of a positive integer `n` is a positive divisor of `n` other than `n` itself. Return the pairs in ascending order of the first element, with each pair represented as `[a, b]`. ### Function Signature ```python def amicable_pairs(low: int, high: int) -> list[list[int]]: ... ``` ### Rules - Both numbers of a pair must lie in `[low, high]`. A pair whose smaller number is below `low`, or whose larger number is above `high`, is left out. - A perfect number (one whose proper divisors sum to itself, such as 6) does not form a pair with itself, because a pair needs `a < b`. - Each pair is `[a, b]` with `a < b`, and the list is sorted by `a` in ascending order. Return an empty list when the range holds no pair. ### Constraints - `1 <= low <= high <= 10^5` ### Examples ```text Input: low = 1, high = 300 Output: [[220, 284]] ``` The proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, which sum to 284. The proper divisors of 284 are 1, 2, 4, 71 and 142, which sum to 220. ```text Input: low = 1, high = 1500 Output: [[220, 284], [1184, 1210]] ``` The proper divisors of 1184 sum to 1210 and the proper divisors of 1210 sum to 1184. The next amicable pair, (2620, 2924), lies above 1500. ```text Input: low = 250, high = 1200 Output: [] ``` 284 is in the range but its partner 220 is below `low`, and 1184 is in the range but its partner 1210 is above `high`. Neither pair has both numbers inside `[250, 1200]`.

Overview: Find every amicable pair of integers inside a given range, where the proper divisors of each number add up to the other number. It tests computing divisor sums for many numbers at once, reasoning about running time, and handling range boundaries without reporting a pair twice.

Yahoo logo
Yahoo
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given two integers low and high, return all amicable pairs (a, b) with low <= a < b <= high, such that the sum of the proper divisors of a equals b and the sum of the proper divisors of b equals a.

A proper divisor of a positive integer n is a positive divisor of n other than n itself. Return the pairs in ascending order of the first element, with each pair represented as [a, b].

Function Signature

def amicable_pairs(low: int, high: int) -> list[list[int]]:
    ...

Rules

  • Both numbers of a pair must lie in [low, high] . A pair whose smaller number is below low , or whose larger number is above high , is left out.
  • A perfect number (one whose proper divisors sum to itself, such as 6) does not form a pair with itself, because a pair needs a < b .
  • Each pair is [a, b] with a < b , and the list is sorted by a in ascending order. Return an empty list when the range holds no pair.

Constraints

  • 1 <= low <= high <= 10^5

Examples

Input: low = 1, high = 300
Output: [[220, 284]]

The proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, which sum to 284. The proper divisors of 284 are 1, 2, 4, 71 and 142, which sum to 220.

Input: low = 1, high = 1500
Output: [[220, 284], [1184, 1210]]

The proper divisors of 1184 sum to 1210 and the proper divisors of 1210 sum to 1184. The next amicable pair, (2620, 2924), lies above 1500.

Input: low = 250, high = 1200
Output: []

284 is in the range but its partner 220 is below low, and 1184 is in the range but its partner 1210 is above high. Neither pair has both numbers inside [250, 1200].