nafis nibir 6 лет назад
Родитель
Сommit
d3a8a0c9f8
2 измененных файлов: 41 добавлений и 4 удалений
  1. 41
    4
      src/main/java/io/zipcoder/ArrayDrills.java
  2. Двоичные данные
      target/classes/io/zipcoder/ArrayDrills.class

+ 41
- 4
src/main/java/io/zipcoder/ArrayDrills.java Просмотреть файл

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 public class ArrayDrills {
4 6
 
5 7
 
@@ -64,7 +66,12 @@ public class ArrayDrills {
64 66
      *           maxValue([5, 11, 9]); // Should return [11,11,11]
65 67
      */
66 68
     public Integer[] maxValue(Integer[] input){
67
-        return null;
69
+        Arrays.sort(input);
70
+        int max = input[input.length-1];
71
+        for (int j = 0; j < input.length-1; j++){
72
+            input[j] = max;
73
+        }
74
+        return input;
68 75
     }
69 76
 
70 77
 
@@ -75,17 +82,43 @@ public class ArrayDrills {
75 82
      *           middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
76 83
      */
77 84
     public Integer[] middleWay(Integer[] input1, Integer[] input2){
78
-        return null;
85
+        Integer[] newArr = new Integer[2];
86
+        newArr[0] = findMiddleReturnSum(input1);
87
+        newArr[1] = findMiddleReturnSum(input2);
88
+        return newArr;
89
+    }
90
+
91
+    public Integer findMiddleReturnSum(Integer[] array){
92
+        Integer ans = 0;
93
+        if (array.length % 2 == 0){
94
+            ans += array[array.length/2] + array[(array.length/2)-1];
95
+        } else {
96
+            ans = array[array.length/2];
97
+        }
98
+        return ans;
79 99
     }
80 100
 
81 101
 
102
+
82 103
     /**
83 104
      * Start with 2 int arrays, a and b, each length 2.
84 105
      * Consider the sum of the values in each array.
85 106
      * Return the array which has the largest sum. In event of a tie, return a.
86 107
      */
87 108
     public Integer[] biggerTwo(Integer[] a, Integer[] b){
88
-        return null;
109
+        if (arraySum(a) > arraySum(b)){
110
+            return a;
111
+        } else if (arraySum(a) <  arraySum(b)){
112
+            return b;
113
+        } else { return a; }
114
+    }
115
+
116
+    public int arraySum(Integer[] arr){
117
+        int sum1 = 0;
118
+        for (Integer element: arr){
119
+            sum1 += element;
120
+        }
121
+        return sum1;
89 122
     }
90 123
 
91 124
     /**
@@ -95,6 +128,10 @@ public class ArrayDrills {
95 128
      *           midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
96 129
      */
97 130
     public Integer[] midThree(Integer[] nums){
98
-        return null;
131
+        Integer[] newArr = new Integer[3];
132
+        newArr[0] = nums[(nums.length/2) - 1];
133
+        newArr[1] = nums[nums.length/2];
134
+        newArr[2] = nums[(nums.length/2) + 1];
135
+        return newArr;
99 136
     }
100 137
 }

Двоичные данные
target/classes/io/zipcoder/ArrayDrills.class Просмотреть файл