Sum Pairwise Remainders Across Two Integer Ranges

Quick Overview

Sum every remainder produced by pairing an integer in one inclusive range with each valid divisor in another. This algorithmic exercise emphasizes quotient-block aggregation, the undefined zero-divisor boundary, and 64-bit arithmetic without a quadratic loop.

Sum Pairwise Remainders Across Two Integer Ranges

Company: IBM

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Online Assessment

# Sum Pairwise Remainders Across Two Integer Ranges You are given two non-negative integers `upperX` and `upperY`. Return the sum of `x mod y` over every integer `x` in `[0, upperX]` and every valid divisor `y` in `[1, upperY]`. Implement `sumRangeRemainders(upperX, upperY)`. ## Input and Output - Return $\sum_{x=0}^{\text{upperX}}\sum_{y=1}^{\text{upperY}}(x\bmod y)$. - The original range description includes zero at both lower bounds. Because modulo by zero is undefined, this practice version excludes `y = 0` while retaining `x = 0`. ## Constraints - `0 <= upperX <= 1,000,000` - `1 <= upperY <= 1,000,000` - The result fits in a signed 64-bit integer. - Iterating over all `(upperX + 1) * upperY` pairs will not finish within the intended limits. ## Example 1 ```text Input: upperX = 2, upperY = 2 Output: 1 ``` For divisors `1` and `2`, the remainder totals are `0` and `1`. ## Example 2 ```text Input: upperX = 3, upperY = 3 Output: 5 ``` The totals for divisors `1`, `2`, and `3` are `0`, `2`, and `3`.

Quick Answer: Sum every remainder produced by pairing an integer in one inclusive range with each valid divisor in another. This algorithmic exercise emphasizes quotient-block aggregation, the undefined zero-divisor boundary, and 64-bit arithmetic without a quadratic loop.

|Home/Coding & Algorithms/IBM
IBM logo
IBM
Aug 18, 2026
easySoftware EngineerOnline AssessmentCoding & Algorithms
0
0

Sum Pairwise Remainders Across Two Integer Ranges

You are given two non-negative integers upperX and upperY. Return the sum of x mod y over every integer x in [0, upperX] and every valid divisor y in [1, upperY].

Implement sumRangeRemainders(upperX, upperY).

Input and Output

  • Return x=0upperXy=1upperY(xmody)\sum_{x=0}^{\text{upperX}}\sum_{y=1}^{\text{upperY}}(x\bmod y) .
  • The original range description includes zero at both lower bounds. Because modulo by zero is undefined, this practice version excludes y = 0 while retaining x = 0 .

Constraints

  • 0 <= upperX <= 1,000,000
  • 1 <= upperY <= 1,000,000
  • The result fits in a signed 64-bit integer.
  • Iterating over all (upperX + 1) * upperY pairs will not finish within the intended limits.

Example 1

Input: upperX = 2, upperY = 2
Output: 1

For divisors 1 and 2, the remainder totals are 0 and 1.

Example 2

Input: upperX = 3, upperY = 3
Output: 5

The totals for divisors 1, 2, and 3 are 0, 2, and 3.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...