|
@@ -10,7 +10,7 @@ public class ArrayDrills {
|
10
|
10
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
11
|
*/
|
12
|
12
|
public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
13
|
+ return input[0] == value || input[input.length - 1] == value;
|
14
|
14
|
}
|
15
|
15
|
|
16
|
16
|
/**
|
|
@@ -19,7 +19,7 @@ public class ArrayDrills {
|
19
|
19
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
20
|
*/
|
21
|
21
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
22
|
+ return input.length > 1 && input[0] == input[input.length - 1];
|
23
|
23
|
}
|
24
|
24
|
|
25
|
25
|
|
|
@@ -30,7 +30,7 @@ public class ArrayDrills {
|
30
|
30
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
31
|
*/
|
32
|
32
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
33
|
+ return input1[0] == input2[0] || input1[input1.length - 1] == input2[input2.length - 1];
|
34
|
34
|
}
|
35
|
35
|
|
36
|
36
|
/**
|
|
@@ -39,7 +39,7 @@ public class ArrayDrills {
|
39
|
39
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
40
|
*/
|
41
|
41
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
42
|
+ return new Integer[] {input[1], input[2], input[0]};
|
43
|
43
|
}
|
44
|
44
|
|
45
|
45
|
|
|
@@ -50,7 +50,14 @@ public class ArrayDrills {
|
50
|
50
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
51
|
*/
|
52
|
52
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
53
|
+ int max = Integer.MIN_VALUE;
|
|
54
|
+ for (Integer e : input) {
|
|
55
|
+ max = Math.max(max, e);
|
|
56
|
+ }
|
|
57
|
+ for (int i = 0; i < input.length; i++) {
|
|
58
|
+ input[i] = max;
|
|
59
|
+ }
|
|
60
|
+ return input;
|
54
|
61
|
}
|
55
|
62
|
|
56
|
63
|
|
|
@@ -60,8 +67,16 @@ public class ArrayDrills {
|
60
|
67
|
* example : middleWay([1, 2, 3], [4,5,6,2]); // Should return [2,11]
|
61
|
68
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
69
|
*/
|
|
70
|
+ public boolean isEvenArray(Integer[] input) {
|
|
71
|
+ return input.length % 2 == 0;
|
|
72
|
+ }
|
63
|
73
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
74
|
+ int i1 = input1.length / 2;
|
|
75
|
+ int i2 = input2.length / 2;
|
|
76
|
+ return new Integer[] {
|
|
77
|
+ isEvenArray(input1) ? input1[i1] + input1[i1 - 1] : input1[i1],
|
|
78
|
+ isEvenArray(input2) ? input2[i2] + input2[i2 - 1] : input2[i2]
|
|
79
|
+ };
|
65
|
80
|
}
|
66
|
81
|
|
67
|
82
|
|
|
@@ -71,7 +86,13 @@ public class ArrayDrills {
|
71
|
86
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
87
|
*/
|
73
|
88
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
89
|
+ int sumA = 0;
|
|
90
|
+ int sumB = 0;
|
|
91
|
+ for (int i = 0; i < 2; i++) {
|
|
92
|
+ sumA += a[i];
|
|
93
|
+ sumB += b[i];
|
|
94
|
+ }
|
|
95
|
+ return sumA >= sumB ? a : b;
|
75
|
96
|
}
|
76
|
97
|
|
77
|
98
|
/**
|
|
@@ -80,7 +101,11 @@ public class ArrayDrills {
|
80
|
101
|
* example : midThree([1, 2, 3, 4, 5]); // Should return [2, 3, 4]
|
81
|
102
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
103
|
*/
|
83
|
|
- public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
104
|
+ public Integer[] midThree(Integer[] nums) {
|
|
105
|
+ return new Integer[] {
|
|
106
|
+ nums[nums.length / 2 - 1],
|
|
107
|
+ nums[nums.length / 2],
|
|
108
|
+ nums[nums.length / 2 + 1]
|
|
109
|
+ };
|
85
|
110
|
}
|
86
|
111
|
}
|