|
@@ -9,8 +9,12 @@ 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;
|
|
12
|
+ public boolean firstLast(Integer value, Integer[] input) {
|
|
13
|
+ boolean result = false;
|
|
14
|
+ if (value == input[0] || value == input[input.length - 1]) {
|
|
15
|
+ result = true;
|
|
16
|
+ }
|
|
17
|
+ return result;
|
14
|
18
|
}
|
15
|
19
|
|
16
|
20
|
/**
|
|
@@ -18,8 +22,13 @@ public class ArrayDrills {
|
18
|
22
|
* example : sameFirstLast([1,2,3]); // Should return false
|
19
|
23
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
24
|
*/
|
21
|
|
- public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
25
|
+ public boolean sameFirstLast(Integer[] input){
|
|
26
|
+ boolean x = false;
|
|
27
|
+ if (input.length >= 1 && input[0].equals(input[input.length-1])){
|
|
28
|
+ x = true;
|
|
29
|
+ }
|
|
30
|
+ boolean x2 = x;
|
|
31
|
+ return x2;
|
23
|
32
|
}
|
24
|
33
|
|
25
|
34
|
|
|
@@ -30,7 +39,12 @@ public class ArrayDrills {
|
30
|
39
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
40
|
*/
|
32
|
41
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
42
|
+ boolean testing = false;
|
|
43
|
+ if (input1[0] == input2[0] || input1[input1.length-1].equals(input2[input2.length-1])){
|
|
44
|
+ testing = true;
|
|
45
|
+ }
|
|
46
|
+ Boolean testing2 = testing;
|
|
47
|
+ return testing2;
|
34
|
48
|
}
|
35
|
49
|
|
36
|
50
|
/**
|
|
@@ -39,7 +53,14 @@ public class ArrayDrills {
|
39
|
53
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
54
|
*/
|
41
|
55
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
56
|
+ Integer x = input[0];
|
|
57
|
+
|
|
58
|
+ for (Integer i = 0; i < input.length-1; i++){
|
|
59
|
+ input[i] = input[i + 1];
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ input[input.length - 1] = x;
|
|
63
|
+ return input;
|
43
|
64
|
}
|
44
|
65
|
|
45
|
66
|
|
|
@@ -50,7 +71,21 @@ public class ArrayDrills {
|
50
|
71
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
72
|
*/
|
52
|
73
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
74
|
+ Integer[] x = new Integer[input.length];
|
|
75
|
+
|
|
76
|
+ for(Integer i = 0; i < input.length; i++){
|
|
77
|
+ Integer changing = 0;
|
|
78
|
+ if(input[i] > changing){
|
|
79
|
+ changing = i;
|
|
80
|
+ }
|
|
81
|
+ for(Integer q = 0; q < x.length; q++){
|
|
82
|
+ x[q] = changing;
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+// for(Integer q = 0; q > x.length; q++){
|
|
86
|
+// x[q] = changing;
|
|
87
|
+// }
|
|
88
|
+ return x;
|
54
|
89
|
}
|
55
|
90
|
|
56
|
91
|
|
|
@@ -61,7 +96,23 @@ public class ArrayDrills {
|
61
|
96
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
97
|
*/
|
63
|
98
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
99
|
+ Integer[] x = new Integer[2];
|
|
100
|
+ if(input1.length % 2 != 0){
|
|
101
|
+ x[0] = input1[input1.length/2];
|
|
102
|
+ } else
|
|
103
|
+
|
|
104
|
+ if(input1.length % 2 == 0){
|
|
105
|
+ x[0] = (input1[input1.length/2]) + (input1[input1.length/2 - 1]);
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ if(input2.length % 2 != 0){
|
|
109
|
+ x[1] = input2[input2.length/2];
|
|
110
|
+ } else
|
|
111
|
+
|
|
112
|
+ if(input2.length % 2 == 0){
|
|
113
|
+ x[1] = (input2[input2.length/2]) + (input2[input2.length/2 - 1]);
|
|
114
|
+ }
|
|
115
|
+ return x;
|
65
|
116
|
}
|
66
|
117
|
|
67
|
118
|
|
|
@@ -71,7 +122,12 @@ public class ArrayDrills {
|
71
|
122
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
123
|
*/
|
73
|
124
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
125
|
+ Integer q = a[0] + a[1];
|
|
126
|
+ Integer k = b[0] + b[1];
|
|
127
|
+ if (k > q){
|
|
128
|
+ return b;
|
|
129
|
+ }
|
|
130
|
+ return a;
|
75
|
131
|
}
|
76
|
132
|
|
77
|
133
|
/**
|
|
@@ -81,6 +137,10 @@ public class ArrayDrills {
|
81
|
137
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
138
|
*/
|
83
|
139
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
140
|
+ Integer[] newArray = new Integer[3];
|
|
141
|
+ newArray[0] = nums[nums.length/2 - 1];
|
|
142
|
+ newArray[1] = nums[nums.length/2];
|
|
143
|
+ newArray[2] = nums[nums.length/2 + 1];
|
|
144
|
+ return newArray;
|
85
|
145
|
}
|
86
|
146
|
}
|