|
@@ -10,7 +10,15 @@ 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
|
+ for(int i=0; i<input.length; i++){
|
|
14
|
+ if(value == input[i] || value == input[input.length-1]){
|
|
15
|
+ return true;
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ return false;
|
14
|
22
|
}
|
15
|
23
|
|
16
|
24
|
/**
|
|
@@ -19,8 +27,16 @@ public class ArrayDrills {
|
19
|
27
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
28
|
*/
|
21
|
29
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
23
|
|
- }
|
|
30
|
+ for(int i=0; i<input.length; i++) {
|
|
31
|
+ if(input.length >= 1 && input[i] == input[input.length-1]) {
|
|
32
|
+ return true;
|
|
33
|
+ }
|
|
34
|
+ }
|
|
35
|
+ return false;
|
|
36
|
+
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+
|
24
|
40
|
|
25
|
41
|
|
26
|
42
|
/**
|
|
@@ -30,8 +46,13 @@ public class ArrayDrills {
|
30
|
46
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
47
|
*/
|
32
|
48
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
34
|
|
- }
|
|
49
|
+ if(input1[0] == input2[0] || input1[input1.length-1] == input2[input2.length-1]){
|
|
50
|
+ return true;
|
|
51
|
+ }
|
|
52
|
+ return false;
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+
|
35
|
56
|
|
36
|
57
|
/**
|
37
|
58
|
* Given an array of ints, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
|
|
@@ -39,8 +60,17 @@ public class ArrayDrills {
|
39
|
60
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
61
|
*/
|
41
|
62
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
43
|
|
- }
|
|
63
|
+
|
|
64
|
+ Integer[] newArr = new Integer[input.length];
|
|
65
|
+
|
|
66
|
+ for(int i=0; i<input.length-1; i++){
|
|
67
|
+ newArr[i] = input[i+1];
|
|
68
|
+ }
|
|
69
|
+ newArr[newArr.length-1] = input[0];
|
|
70
|
+ return newArr;
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+
|
44
|
74
|
|
45
|
75
|
|
46
|
76
|
/**
|
|
@@ -50,7 +80,26 @@ public class ArrayDrills {
|
50
|
80
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
81
|
*/
|
52
|
82
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
83
|
+ Integer[] newArr = new Integer[input.length];
|
|
84
|
+ int largestValue = input[0];
|
|
85
|
+ int k = 0;
|
|
86
|
+ for(int i=1; i<input.length; i++) {
|
|
87
|
+ if (input[i] > largestValue) {
|
|
88
|
+ largestValue = input[i];
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+ for(int i=0; i<input.length; i++ ){
|
|
92
|
+ if(input[i] != largestValue){
|
|
93
|
+ input[i] = largestValue;
|
|
94
|
+ newArr[k] = input[i];
|
|
95
|
+ k++;
|
|
96
|
+ }
|
|
97
|
+ else {
|
|
98
|
+ newArr[k] = input[i];
|
|
99
|
+ k++;
|
|
100
|
+ }
|
|
101
|
+ }
|
|
102
|
+ return newArr;
|
54
|
103
|
}
|
55
|
104
|
|
56
|
105
|
|
|
@@ -61,7 +110,27 @@ public class ArrayDrills {
|
61
|
110
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
111
|
*/
|
63
|
112
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
113
|
+ Integer[] newArr = new Integer[2];
|
|
114
|
+ int len1 = input1.length;
|
|
115
|
+ int len2 = input2.length;
|
|
116
|
+
|
|
117
|
+ if(len1%2 == 0){
|
|
118
|
+ newArr[0] = input1[len1/2] + input1[(len1-2)/2];
|
|
119
|
+
|
|
120
|
+ }
|
|
121
|
+ else {
|
|
122
|
+ newArr[0] = input1[len1/2];
|
|
123
|
+
|
|
124
|
+ }
|
|
125
|
+ if(len2%2 == 0){
|
|
126
|
+ newArr[1] = input2[len2/2] + input2[(len2-2)/2];
|
|
127
|
+
|
|
128
|
+ }
|
|
129
|
+ else {
|
|
130
|
+ newArr[1] = input2[len2/2];
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ return newArr;
|
65
|
134
|
}
|
66
|
135
|
|
67
|
136
|
|
|
@@ -71,7 +140,18 @@ public class ArrayDrills {
|
71
|
140
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
141
|
*/
|
73
|
142
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
143
|
+
|
|
144
|
+ int total1 = a[0] + a[1];
|
|
145
|
+
|
|
146
|
+ int total2 = b[0] + b[1];
|
|
147
|
+
|
|
148
|
+ if(total1 >= total2)
|
|
149
|
+ return a;
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+ return b;
|
|
154
|
+
|
75
|
155
|
}
|
76
|
156
|
|
77
|
157
|
/**
|
|
@@ -81,6 +161,15 @@ public class ArrayDrills {
|
81
|
161
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
162
|
*/
|
83
|
163
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
164
|
+
|
|
165
|
+ Integer[] newArr = new Integer[3];
|
|
166
|
+
|
|
167
|
+ int len = nums.length;
|
|
168
|
+
|
|
169
|
+ newArr[0] = nums[(len-2)/2];
|
|
170
|
+ newArr[1] = nums[len/2];
|
|
171
|
+ newArr[2] = nums[(len+1)/2];
|
|
172
|
+
|
|
173
|
+ return newArr;
|
85
|
174
|
}
|
86
|
175
|
}
|