Skip to content

Commit b84843c

Browse files
solves #2894: Divisible and Non-divisible Sums Difference in java
1 parent 160642b commit b84843c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@
859859
| 2864 | [Maximum Odd Binary Number](https://leetcode.com/problems/maximum-odd-binary-number) | [![Java](assets/java.png)](src/MaximumOddBinaryNumber.java) | |
860860
| 2869 | [Minimum Operations to Collect Elements](https://leetcode.com/problems/minimum-operations-to-collect-elements) | [![Java](assets/java.png)](src/MinimumOperationsToCollectElements.java) | |
861861
| 2873 | [Maximum Value of an Ordered Triplet I](https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i) | [![Java](assets/java.png)](src/MaximumValueOfAnOrderedTripletI.java) | |
862-
| 2894 | [Divisible and Non-divisible Sums Difference](https://leetcode.com/problems/divisible-and-non-divisible-sums-difference) | | |
862+
| 2894 | [Divisible and Non-divisible Sums Difference](https://leetcode.com/problems/divisible-and-non-divisible-sums-difference) | [![Java](assets/java.png)](src/DivisibleAndNonDivisibleSumsDifference.java) | |
863863
| 2899 | [Last Visited Integers](https://leetcode.com/problems/last-visited-integers) | | |
864864
| 2900 | [Longest Unequal Adjacent Groups Subsequence I](https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i) | | |
865865
| 2903 | [Find Indices With Index and Value Difference I](https://leetcode.com/problems/find-indices-with-index-and-value-difference-i) | | |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://leetcode.com/problems/divisible-and-non-divisible-sums-difference
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class DivisibleAndNonDivisibleSumsDifference {
6+
public int differenceOfSums(int n, int m) {
7+
final int sumDivisibleIntegers = getSumOfDivisibleIntegers(n, m);
8+
final int sumNonDivisibleIntegers = (n * (n + 1)) / 2 - sumDivisibleIntegers;
9+
10+
return sumNonDivisibleIntegers - sumDivisibleIntegers;
11+
}
12+
13+
private static int getSumOfDivisibleIntegers(int n, int m) {
14+
int sum = 0;
15+
for (int i = 1 ; i <= n ; i++) {
16+
if (i % m == 0) {
17+
sum += i;
18+
}
19+
}
20+
return sum;
21+
}
22+
}

0 commit comments

Comments
 (0)