Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5fe1765

Browse files
solves #2535: Difference Between Element Sum and Digit Sum of an Array in java
1 parent dc24bca commit 5fe1765

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@
794794
| 2520 | [Count the Digits That Divide a Number](https://leetcode.com/problems/count-the-digits-that-divide-a-number) | [![Java](assets/java.png)](src/CountTheDigitsThatDivideANumber.java) | |
795795
| 2525 | [Categorize Box According to Criteria](https://leetcode.com/problems/categorize-box-according-to-criteria) | [![Java](assets/java.png)](src/CategorizeBoxAccordingToCriteria.java) | |
796796
| 2529 | [Maximum Count of Positive Integer and Negative Integer](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer) | [![Java](assets/java.png)](src/MaximumCountOfPositiveIntegerAndNegativeInteger.java) | |
797-
| 2535 | [Difference Between Element Sum and Digit Sum of an Array](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array) | | |
797+
| 2535 | [Difference Between Element Sum and Digit Sum of an Array](https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array) | [![Java](assets/java.png)](src/DifferenceBetweenElementSumAndDigitSumOfAnArray.java) | |
798798
| 2540 | [Minimum Common Value](https://leetcode.com/problems/minimum-common-value) | | |
799799
| 2544 | [Alternating Digit Sum](https://leetcode.com/problems/alternating-digit-sum) | | |
800800
| 2549 | [Count Distinct Numbers on Board](https://leetcode.com/problems/count-distinct-numbers-on-board) | | |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array
2+
// T: O(N)
3+
// S: O(1)
4+
5+
import java.util.Arrays;
6+
7+
public class DifferenceBetweenElementSumAndDigitSumOfAnArray {
8+
public int differenceOfSum(int[] nums) {
9+
final int elementSum = Arrays.stream(nums).sum();
10+
final int digitSum = getDigitSum(nums);
11+
return Math.abs(elementSum - digitSum);
12+
}
13+
14+
private int getDigitSum(int[] array) {
15+
int sum = 0;
16+
for (int number : array) {
17+
sum += getDigitSum(number);
18+
}
19+
return sum;
20+
}
21+
22+
private int getDigitSum(int number) {
23+
final String num = number + "";
24+
int sum = 0;
25+
for (int index = 0 ; index < num.length() ; index++) {
26+
int digit = num.charAt(index) - '0';
27+
sum += digit;
28+
}
29+
return sum;
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /