Skip to content

Commit 6b2b509

Browse files
solves #2652: Sum Multiples in java
1 parent 7815810 commit 6b2b509

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@
817817
| 2643 | [Row With Maximum Ones](https://leetcode.com/problems/row-with-maximum-ones) | [![Java](assets/java.png)](src/RowWithMaximumOnes.java) | |
818818
| 2644 | [Find the Maximum Divisibility Score](https://leetcode.com/problems/find-the-maximum-divisibility-score) | [![Java](assets/java.png)](src/FindTheMaximumDivisibilityScore.java) | |
819819
| 2651 | [Calculate Delayed Arrival Time](https://leetcode.com/problems/calculate-delayed-arrival-time) | [![Java](assets/java.png)](src/CalculateDelayedArrivalTime.java) | |
820-
| 2652 | [Sum Multiples](https://leetcode.com/problems/sum-multiples) | | |
820+
| 2652 | [Sum Multiples](https://leetcode.com/problems/sum-multiples) | [![Java](assets/java.png)](src/SumMultiples.java) | |
821821
| 2656 | [Maximum Sum With Exactly K Elements](https://leetcode.com/problems/maximum-sum-with-exactly-k-elements) | | |
822822
| 2660 | [Determine the Winner of a Bowling Game](https://leetcode.com/problems/determine-the-winner-of-a-bowling-game) | | |
823823
| 2670 | [Find the Distinct Difference Array](https://leetcode.com/problems/find-the-distinct-difference-array) | | |

Diff for: src/SumMultiples.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/sum-multiples
2+
// T: O(1)
3+
// S: O(1)
4+
5+
public class SumMultiples {
6+
public int sumOfMultiples(int n) {
7+
return 3 * summation(n / 3)
8+
+ 5 * summation(n / 5)
9+
+ 7 * summation(n / 7)
10+
- 15 * summation(n / 15)
11+
- 21 * summation(n / 21)
12+
- 35 * summation(n / 35)
13+
+ 105 * summation(n / 105);
14+
}
15+
16+
private int summation(int x) {
17+
return (x * (x + 1)) / 2;
18+
}
19+
}

0 commit comments

Comments
 (0)