-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2894-DivisibleAndNonDivisibleSumsDifference.go
More file actions
97 lines (84 loc) · 3.66 KB
/
2894-DivisibleAndNonDivisibleSumsDifference.go
File metadata and controls
97 lines (84 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
// 2894. Divisible and Non-divisible Sums Difference
// You are given positive integers n and m.
// Define two integers as follows:
// 1. num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m.
// 2. 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
import "fmt"
func differenceOfSums(n int, m int) int {
sum := n * (n + 1) / 2
k := n / m
return sum - 2 * (m * k * (k + 1) / 2)
}
func differenceOfSums1(n int, m int) int {
num1, num2 := 0, 0
for i := 1; i <= n; i++ {
if i % m == 0 {
num2 += i
} else {
num1 += i
}
}
return num1 - num2
}
func main() {
// 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.
fmt.Println(differenceOfSums(10, 3)) // 19
// 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.
fmt.Println(differenceOfSums(5, 6)) // 15
// 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.
fmt.Println(differenceOfSums(5, 1)) // -15
fmt.Println(differenceOfSums(1, 1)) // -1
fmt.Println(differenceOfSums(1000, 1000)) // 498500
fmt.Println(differenceOfSums(1, 1000)) // 1
fmt.Println(differenceOfSums(1000, 1)) // -500500
fmt.Println(differenceOfSums1(10, 3)) // 19
fmt.Println(differenceOfSums1(5, 6)) // 15
fmt.Println(differenceOfSums1(5, 1)) // -15
fmt.Println(differenceOfSums1(1, 1)) // -1
fmt.Println(differenceOfSums1(1000, 1000)) // 498500
fmt.Println(differenceOfSums1(1, 1000)) // 1
fmt.Println(differenceOfSums1(1000, 1)) // -500500
}