返回介绍

solution / 2800-2899 / 2894.Divisible and Non-divisible Sums Difference / README_EN

发布于 2024-06-17 01:02:59 字数 4154 浏览 0 评论 0 收藏 0

2894. Divisible and Non-divisible Sums Difference

中文文档

Description

You are given positive integers n and m.

Define two integers, num1 and num2, as follows:

  • num1: The sum of all integers in the range [1, n] that are not divisible by m.
  • num2: The sum of all integers in the range [1, n] 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

Solutions

Solution 1: Simulation

We traverse every number in the range $[1, n]$. If it is divisible by $m$, we subtract it from the answer. Otherwise, we add it to the answer.

After the traversal, we return the answer.

The time complexity is $O(n)$, where $n$ is the given integer. The space complexity is $O(1)$.

class Solution:
  def differenceOfSums(self, n: int, m: int) -> int:
    return sum(i if i % m else -i for i in range(1, n + 1))
class Solution {
  public int differenceOfSums(int n, int m) {
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      ans += i % m == 0 ? -i : i;
    }
    return ans;
  }
}
class Solution {
public:
  int differenceOfSums(int n, int m) {
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      ans += i % m ? i : -i;
    }
    return ans;
  }
};
func differenceOfSums(n int, m int) (ans int) {
  for i := 1; i <= n; i++ {
    if i%m == 0 {
      ans -= i
    } else {
      ans += i
    }
  }
  return
}
function differenceOfSums(n: number, m: number): number {
  let ans = 0;
  for (let i = 1; i <= n; ++i) {
    ans += i % m ? i : -i;
  }
  return ans;
}

Solution 2

class Solution {
  public int differenceOfSums(int n, int m) {
    int sum = n * (n + 1) / 2;
    int k = n / m;
    int nums2 = k * (k + 1) / 2 * m;
    return sum - nums2 * 2;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文