aboutsummaryrefslogtreecommitdiff
path: root/challenge-320/deadmarshal/java
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-320/deadmarshal/java')
-rw-r--r--challenge-320/deadmarshal/java/Ch1.java18
-rw-r--r--challenge-320/deadmarshal/java/Ch2.java25
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-320/deadmarshal/java/Ch1.java b/challenge-320/deadmarshal/java/Ch1.java
new file mode 100644
index 0000000000..6799e5c444
--- /dev/null
+++ b/challenge-320/deadmarshal/java/Ch1.java
@@ -0,0 +1,18 @@
+public class Ch1 {
+ public static void main(String[] args) {
+ System.out.println(maximum_count(new int[]{-3, -2, -1, 1, 2, 3}));
+ System.out.println(maximum_count(new int[]{-2, -1, 0, 0, 1}));
+ System.out.println(maximum_count(new int[]{1, 2, 3, 4}));
+ }
+
+ private static int maximum_count(int[] arr) {
+ int n = 0, p = 0;
+ for (int num : arr) {
+ if (num != 0) {
+ if (num < 0) n++;
+ else p++;
+ }
+ }
+ return Math.max(n, p);
+ }
+} \ No newline at end of file
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