aboutsummaryrefslogtreecommitdiff
path: root/challenge-320/deadmarshal/java/Ch2.java
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-320/deadmarshal/java/Ch2.java')
-rw-r--r--challenge-320/deadmarshal/java/Ch2.java25
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-320/deadmarshal/java/Ch2.java b/challenge-320/deadmarshal/java/Ch2.java
new file mode 100644
index 0000000000..b2dfa8b596
--- /dev/null
+++ b/challenge-320/deadmarshal/java/Ch2.java
@@ -0,0 +1,25 @@
+public class Ch2 {
+ public static void main(String[] args) {
+ System.out.println(sum_difference(new int[]{1, 23, 4, 5}));
+ System.out.println(sum_difference(new int[]{1, 2, 3, 4, 5}));
+ System.out.println(sum_difference(new int[]{1, 2, 34}));
+ }
+
+ private static int sum_difference(int[] arr) {
+ int sum1 = 0, sum2 = 0;
+ for (int n : arr) {
+ sum1 += n;
+ sum2 += sum_digits(n);
+ }
+ return Math.abs(sum1 - sum2);
+ }
+
+ private static int sum_digits(int n) {
+ int sum = 0;
+ while (n != 0) {
+ sum += n % 10;
+ n /= 10;
+ }
+ return sum;
+ }
+} \ No newline at end of file