|
@@ -72,17 +72,19 @@ public class ArrayDrills {
|
72
|
72
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
73
|
73
|
*/
|
74
|
74
|
public Integer[] middleWay(Integer[] input1, Integer[] input2) {
|
75
|
|
-//
|
76
|
|
-// Integer[] a;
|
77
|
|
-// if(input1.length % 2 ==0)
|
78
|
|
-// a = new Integer[2];
|
79
|
|
-// a[0] = input1[(input1.length/2) -1];
|
80
|
|
-// a[1] = input1[input1.length/2];
|
81
|
|
-// } else {
|
82
|
|
-// a = new Integer[1];
|
83
|
|
-// a[0]
|
84
|
|
-// }
|
85
|
|
- return null;
|
|
75
|
+ Integer[] newInput = new Integer[2];
|
|
76
|
+ if(input1.length % 2 == 0) {
|
|
77
|
+ newInput[0] = (input1.length / 2) + (input1.length / 2-1);
|
|
78
|
+ } else if (input1.length % 2 != 0){
|
|
79
|
+ newInput[0] = input1[input1.length / 2];
|
|
80
|
+ }
|
|
81
|
+ if (input2.length % 2 == 0) {
|
|
82
|
+ newInput[1] = ((input2[input2.length / 2]) + (input2[input2.length / 2 - 1]));
|
|
83
|
+ }
|
|
84
|
+ else if (input2.length % 2 != 0) {
|
|
85
|
+ newInput[1] = input2[input2.length / 2];
|
|
86
|
+ }
|
|
87
|
+ return newInput;
|
86
|
88
|
}
|
87
|
89
|
|
88
|
90
|
/**
|
|
@@ -91,12 +93,11 @@ public class ArrayDrills {
|
91
|
93
|
* Return the array which has the largest sum. In event of a tie, return a.
|
92
|
94
|
*/
|
93
|
95
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
94
|
|
- int sum = a[0] + a[1] - b[0] - b[1];
|
95
|
|
- if (sum >= 0)
|
|
96
|
+ int sumA = a[0] + a[1];
|
|
97
|
+ int sumB = b[0] + b[1];
|
|
98
|
+ if (sumA > sumB)
|
96
|
99
|
return a;
|
97
|
|
- else {
|
98
|
100
|
return b;
|
99
|
|
- }
|
100
|
101
|
}
|
101
|
102
|
|
102
|
103
|
/**
|