Difference Between Nondivisible and Divisible Sums

Quick Overview

Return the sum of integers from one through n that are not divisible by m minus the sum of those that are divisible, preserving negative results.

Difference Between Nondivisible and Divisible Sums

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Difference Between Nondivisible and Divisible Sums You are given positive integers `n` and `m`. Consider every integer in the inclusive range `[1,n]`. Let `num1` be the sum of the integers in that range that are not divisible by `m`. Let `num2` be the sum of the integers in that range that are divisible by `m`. Return `num1 - num2`. ### Examples **Example 1** ```text Input: n = 8, m = 3 Output: 18 ``` The nondivisible integers are `[1,2,4,5,7,8]`, whose sum is `27`. The divisible integers are `[3,6]`, whose sum is `9`. Their difference is `27 - 9 = 18`. **Example 2** ```text Input: n = 4, m = 1 Output: -10 ``` Every integer in `[1,4]` is divisible by `1`. Thus `num1 = 0`, `num2 = 1 + 2 + 3 + 4 = 10`, and the result is `-10`. ### Constraints - `1 <= n, m <= 1000`. ### Clarifying Questions - Are both endpoints included? Yes; include every integer from `1` through `n`. - What is the sum if one of the two groups is empty? Zero. - Can the answer be negative? Yes; return the signed difference in the specified order. - If `m > n`, is any integer in the range divisible by `m`? No; in that case `num2` is zero. ### C++ Interface ```cpp class Solution { public: int differenceOfSums(int n, int m) { } }; ``` ### Reasoning Checkpoints - Partition the inclusive range according to divisibility without losing an endpoint. - Keep the subtraction order and the empty-group cases explicit. - Justify the arithmetic and its range under the given bounds. ```hint Relate the two sums The sum of all integers from one through n is known. The divisible group consists of m times the integers from one through floor(n / m). How can these sums give the requested difference? ```

Overview: Return the sum of integers from one through n that are not divisible by m minus the sum of those that are divisible, preserving negative results.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Difference Between Nondivisible and Divisible Sums

You are given positive integers n and m. Consider every integer in the inclusive range [1,n].

Let num1 be the sum of the integers in that range that are not divisible by m. Let num2 be the sum of the integers in that range that are divisible by m.

Return num1 - num2.

Examples

Example 1

Input: n = 8, m = 3
Output: 18

The nondivisible integers are [1,2,4,5,7,8], whose sum is 27. The divisible integers are [3,6], whose sum is 9. Their difference is 27 - 9 = 18.

Example 2

Input: n = 4, m = 1
Output: -10

Every integer in [1,4] is divisible by 1. Thus num1 = 0, num2 = 1 + 2 + 3 + 4 = 10, and the result is -10.

Constraints

  • 1 <= n, m <= 1000 .

Clarifying Questions Guidance

  • Are both endpoints included? Yes; include every integer from 1 through n .
  • What is the sum if one of the two groups is empty? Zero.
  • Can the answer be negative? Yes; return the signed difference in the specified order.
  • If m > n , is any integer in the range divisible by m ? No; in that case num2 is zero.

C++ Interface

class Solution {
public:
    int differenceOfSums(int n, int m) {
        
    }
};

Reasoning Checkpoints

  • Partition the inclusive range according to divisibility without losing an endpoint.
  • Keep the subtraction order and the empty-group cases explicit.
  • Justify the arithmetic and its range under the given bounds.