|
@@ -9,17 +9,19 @@ public class ArrayDrills {
|
9
|
9
|
* example : firstLast(6, [1,2,6); // Should return true
|
10
|
10
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
11
|
*/
|
12
|
|
- public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
14
|
|
- }
|
|
12
|
+ public Boolean firstLast(Integer value, Integer[] input) {
|
15
|
13
|
|
|
14
|
+ if (value.equals(input[0]) || value.equals(input[input.length - 1])) return true;
|
|
15
|
+ return false;
|
|
16
|
+ }
|
16
|
17
|
/**
|
17
|
18
|
* Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal.
|
18
|
19
|
* example : sameFirstLast([1,2,3]); // Should return false
|
19
|
20
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
21
|
*/
|
21
|
|
- public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
22
|
+ public Boolean sameFirstLast(Integer[] input) {
|
|
23
|
+ if (input.length > 1 && input[0] == input[input.length - 1]) return true;
|
|
24
|
+ return false;
|
23
|
25
|
}
|
24
|
26
|
|
25
|
27
|
|
|
@@ -30,7 +32,8 @@ public class ArrayDrills {
|
30
|
32
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
33
|
*/
|
32
|
34
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
35
|
+ if (input1[0].equals(input2[0]) || input1[input1.length-1].equals(input2[input2.length-1])) return true;
|
|
36
|
+ return false;
|
34
|
37
|
}
|
35
|
38
|
|
36
|
39
|
/**
|
|
@@ -39,18 +42,39 @@ public class ArrayDrills {
|
39
|
42
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
43
|
*/
|
41
|
44
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
45
|
+ Integer[] newArray = new Integer[input.length];
|
|
46
|
+ for (int i = 0; i < input.length; i++) {
|
|
47
|
+ if (i == 0) newArray[2] = input[i];
|
|
48
|
+ else if (i == 1) newArray[0] = input[i];
|
|
49
|
+ else if (i ==2) newArray[1] = input[i];
|
|
50
|
+ } return newArray;
|
43
|
51
|
}
|
44
|
52
|
|
45
|
53
|
|
46
|
54
|
/**
|
47
|
|
- * Given an array of ints, figure out which is element in the array is largest,
|
|
55
|
+ * Given an array of ints, figure out which element in the array is largest,
|
48
|
56
|
* and set all the other elements to be that value. Return the changed array.
|
49
|
57
|
* example : maxValue([1, 2, 3]); // Should return [3,3,3]
|
50
|
58
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
59
|
*/
|
52
|
60
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
61
|
+
|
|
62
|
+ Integer[] maxValueArray = new Integer[input.length];
|
|
63
|
+ int currentMaxValue = 0;
|
|
64
|
+ int currentValue;
|
|
65
|
+
|
|
66
|
+ for(int i =0; i < input.length; i++ ) {
|
|
67
|
+ currentValue = input[i];
|
|
68
|
+ if (currentValue > currentMaxValue) {
|
|
69
|
+ currentMaxValue = currentValue;
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ for (int j =0; j < input.length; j++) {
|
|
74
|
+ maxValueArray[j] = currentMaxValue;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ return maxValueArray;
|
54
|
78
|
}
|
55
|
79
|
|
56
|
80
|
|
|
@@ -61,7 +85,21 @@ public class ArrayDrills {
|
61
|
85
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
86
|
*/
|
63
|
87
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
88
|
+ Integer[] newArray = new Integer[2];
|
|
89
|
+
|
|
90
|
+ newArray[0] = middleWayHelp(input1);
|
|
91
|
+ newArray[1] = middleWayHelp(input2);
|
|
92
|
+ return newArray;
|
|
93
|
+ }
|
|
94
|
+ public Integer middleWayHelp (Integer[] input) {
|
|
95
|
+
|
|
96
|
+ int middleElements;
|
|
97
|
+ if (input.length % 2 == 0)
|
|
98
|
+ middleElements = input[(Math.round(input.length/2))] + (input[(Math.round(input.length/2) -1)]);
|
|
99
|
+ else middleElements = input[Math.round(input.length/2)];
|
|
100
|
+
|
|
101
|
+ return middleElements;
|
|
102
|
+
|
65
|
103
|
}
|
66
|
104
|
|
67
|
105
|
|
|
@@ -71,7 +109,12 @@ public class ArrayDrills {
|
71
|
109
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
110
|
*/
|
73
|
111
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
112
|
+ int sumOfA = a[0] +a[1];
|
|
113
|
+ int sumOfB = b[0] + b[1];
|
|
114
|
+
|
|
115
|
+ if (sumOfA >= sumOfB) return a;
|
|
116
|
+ else return b;
|
|
117
|
+
|
75
|
118
|
}
|
76
|
119
|
|
77
|
120
|
/**
|
|
@@ -81,6 +124,8 @@ public class ArrayDrills {
|
81
|
124
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
125
|
*/
|
83
|
126
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
127
|
+ int length = nums.length;
|
|
128
|
+ Integer[] newArray = {nums[(length/2 -1)], nums[length/2], nums[(length/2 +1)]};
|
|
129
|
+ return newArray;
|
85
|
130
|
}
|
86
|
131
|
}
|