|
@@ -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,10 +19,9 @@ 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
|
|
-
|
26
|
25
|
/**
|
27
|
26
|
* Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element.
|
28
|
27
|
* Both arrays will be length 1 or more.
|
|
@@ -30,7 +29,7 @@ public class ArrayDrills {
|
30
|
29
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
30
|
*/
|
32
|
31
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
32
|
+ return (input1[0] == input2[0] || input1[input1.length-1] == input2[input2.length-1]);
|
34
|
33
|
}
|
35
|
34
|
|
36
|
35
|
/**
|
|
@@ -38,8 +37,10 @@ public class ArrayDrills {
|
38
|
37
|
* example : rotateLeft([1, 2, 3]); // Should return [2,3,1]
|
39
|
38
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
39
|
*/
|
41
|
|
- public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
40
|
+ public Integer[] rotateLeft(Integer[] input) {
|
|
41
|
+
|
|
42
|
+ Integer[] rotating = {input[1], input[2], input[0]};
|
|
43
|
+ return rotating;
|
43
|
44
|
}
|
44
|
45
|
|
45
|
46
|
|
|
@@ -50,8 +51,18 @@ public class ArrayDrills {
|
50
|
51
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
52
|
*/
|
52
|
53
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
54
|
+ Integer maxValue = input[0];
|
|
55
|
+ for(Integer i = 0; i< input.length; i++) {
|
|
56
|
+ if (input[i] > maxValue) {
|
|
57
|
+ maxValue = input[i];
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ Integer[]outPut = new Integer[input.length];
|
|
61
|
+ for(int j=0; j<input.length; j++) {
|
|
62
|
+ outPut[j]=maxValue;
|
54
|
63
|
}
|
|
64
|
+ return outPut;
|
|
65
|
+}
|
55
|
66
|
|
56
|
67
|
|
57
|
68
|
/**
|
|
@@ -60,18 +71,33 @@ public class ArrayDrills {
|
60
|
71
|
* example : middleWay([1, 2, 3], [4,5,6,2]); // Should return [2,11]
|
61
|
72
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
73
|
*/
|
63
|
|
- public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
74
|
+ public Integer[] middleWay(Integer[] input1, Integer[] input2) {
|
|
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;
|
65
|
88
|
}
|
66
|
89
|
|
67
|
|
-
|
68
|
90
|
/**
|
69
|
91
|
* Start with 2 int arrays, a and b, each length 2.
|
70
|
92
|
* Consider the sum of the values in each array.
|
71
|
93
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
94
|
*/
|
73
|
95
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
96
|
+ int sumA = a[0] + a[1];
|
|
97
|
+ int sumB = b[0] + b[1];
|
|
98
|
+ if (sumA > sumB)
|
|
99
|
+ return a;
|
|
100
|
+ return b;
|
75
|
101
|
}
|
76
|
102
|
|
77
|
103
|
/**
|
|
@@ -81,6 +107,12 @@ public class ArrayDrills {
|
81
|
107
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
108
|
*/
|
83
|
109
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
110
|
+
|
|
111
|
+ Integer[] halfArray = new Integer[3];
|
|
112
|
+ Integer half = (nums.length) / 2;
|
|
113
|
+ halfArray[0] = nums[half-1];
|
|
114
|
+ halfArray[1] = nums[half];
|
|
115
|
+ halfArray[2] = nums[half+1];
|
|
116
|
+ return halfArray;
|
85
|
117
|
}
|
86
|
118
|
}
|