2894. Divisible and Non-divisible Sums Difference LeetCode Solution

In this guide, you will get 2894. Divisible and Non-divisible Sums Difference LeetCode Solution with the best time and space complexity. The solution to Divisible and Non-divisible Sums Difference problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Divisible and Non-divisible Sums Difference solution in C++
  4. Divisible and Non-divisible Sums Difference solution in Java
  5. Divisible and Non-divisible Sums Difference solution in Python
  6. Additional Resources
2894. Divisible and Non-divisible Sums Difference LeetCode Solution image

Problem Statement of Divisible and Non-divisible Sums Difference

You are given positive integers n and m.
Define two integers as follows:

num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m.
num2: The sum of all integers in the range [1, n] (both inclusive) that are divisible by m.

Return the integer num1 – num2.

Example 1:

Input: n = 10, m = 3
Output: 19
Explanation: In the given example:
– Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37.
– Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18.
We return 37 – 18 = 19 as the answer.

Example 2:

Input: n = 5, m = 6
Output: 15
Explanation: In the given example:
– Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15.
– Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0.
We return 15 – 0 = 15 as the answer.

Example 3:

Input: n = 5, m = 1
Output: -15
Explanation: In the given example:
– Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0.
– Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15.
We return 0 – 15 = -15 as the answer.

Constraints:

1 <= n, m <= 1000

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

2894. Divisible and Non-divisible Sums Difference LeetCode Solution in C++

class Solution {
 public:
  int differenceOfSums(int n, int m) {
    const int sum = (1 + n) * n / 2;
    const int num2 = getDivisibleSum(n, m);
    const int num1 = sum - num2;
    return num1 - num2;
  }

 private:
  // Returns the sum of all the integers in [1, n] that are divisible by m.
  int getDivisibleSum(int n, int m) {
    const int last = n / m * m;
    if (last == 0)
      return 0;
    const int first = m;
    const int count = (last - first) / m + 1;
    return (first + last) * count / 2;
  }
};
/* code provided by PROGIEZ */

2894. Divisible and Non-divisible Sums Difference LeetCode Solution in Java

class Solution {
  public int differenceOfSums(int n, int m) {
    final int sum = (1 + n) * n / 2;
    final int num2 = getDivisibleSum(n, m);
    final int num1 = sum - num2;
    return num1 - num2;
  }

  // Returns the sum of all the integers in [1, n] that are divisible by m.
  private int getDivisibleSum(int n, int m) {
    final int last = n / m * m;
    if (last == 0)
      return 0;
    final int first = m;
    final int count = (last - first) / m + 1;
    return (first + last) * count / 2;
  }
}
// code provided by PROGIEZ

2894. Divisible and Non-divisible Sums Difference LeetCode Solution in Python

class Solution:
  def differenceOfSums(self, n: int, m: int) -> int:
    summ = (1 + n) * n // 2
    num2 = self._getDivisibleSum(n, m)
    num1 = summ - num2
    return num1 - num2

  def _getDivisibleSum(self, n: int, m: int) -> int:
    """Returns the sum of all the integers in [1, n] that are divisible by m."""
    last = n // m * m
    if last == 0:
      return 0
    first = m
    count = (last - first) // m + 1
    return (first + last) * count // 2
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.