|
@@ -7,19 +7,25 @@ public class ArrayDrills {
|
7
|
7
|
* Given an array of ints, return true if value appears as either the first or last element in the array.
|
8
|
8
|
* The array will be length 1 or more.
|
9
|
9
|
* example : firstLast(6, [1,2,6); // Should return true
|
10
|
|
- * firstLast(6, [1,2,3]); // Should return false
|
|
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
|
+ for (int i = 0; i < input.length; i++) {
|
|
14
|
+ if (input[i] == value)
|
|
15
|
+ return true;
|
|
16
|
+ }
|
|
17
|
+ return false;
|
14
|
18
|
}
|
15
|
19
|
|
16
|
20
|
/**
|
17
|
21
|
* 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
|
22
|
* example : sameFirstLast([1,2,3]); // Should return false
|
19
|
|
- * sameFirstLast([1,2,1]); // Should return true
|
|
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
|
+ if (input[0] == input[input.length - 1])
|
|
27
|
+ return true;
|
|
28
|
+ else return false;
|
23
|
29
|
}
|
24
|
30
|
|
25
|
31
|
|
|
@@ -27,19 +33,27 @@ public class ArrayDrills {
|
27
|
33
|
* 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
|
34
|
* Both arrays will be length 1 or more.
|
29
|
35
|
* example : commonEnd([1, 2, 3], [7, 3]); // Should return true
|
30
|
|
- * commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
|
36
|
+ * commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
37
|
*/
|
32
|
|
- public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
38
|
+ public Boolean commonEnd(Integer[] input1, Integer[] input2) {
|
|
39
|
+ if (input1[0] == input2[0])
|
|
40
|
+ return true;
|
|
41
|
+ else if (input1[input1.length - 1] == input2[input2.length - 1])
|
|
42
|
+ return true;
|
|
43
|
+ return false;
|
34
|
44
|
}
|
35
|
45
|
|
|
46
|
+ /* true = (input1[0] == input2[0] || input1[input1.length-1] == input2[input2.length-1] */
|
|
47
|
+
|
|
48
|
+
|
36
|
49
|
/**
|
37
|
50
|
* Given an array of ints, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
|
38
|
51
|
* example : rotateLeft([1, 2, 3]); // Should return [2,3,1]
|
39
|
|
- * rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
|
52
|
+ * rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
53
|
*/
|
41
|
|
- public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
54
|
+ public Integer[] rotateLeft(Integer[] input) {
|
|
55
|
+ Integer[] rotating = {input[1], input[2], input[0]};
|
|
56
|
+ return rotating;
|
43
|
57
|
}
|
44
|
58
|
|
45
|
59
|
|
|
@@ -47,10 +61,19 @@ public class ArrayDrills {
|
47
|
61
|
* Given an array of ints, figure out which is element in the array is largest,
|
48
|
62
|
* and set all the other elements to be that value. Return the changed array.
|
49
|
63
|
* example : maxValue([1, 2, 3]); // Should return [3,3,3]
|
50
|
|
- * maxValue([5, 11, 9]); // Should return [11,11,11]
|
|
64
|
+ * maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
65
|
*/
|
52
|
|
- public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
66
|
+ public Integer[] maxValue(Integer[] input) {
|
|
67
|
+ Integer maxValue = input[0];
|
|
68
|
+ for(Integer i = 0; i< input.length; i++) {
|
|
69
|
+ if (input[i] > maxValue) {
|
|
70
|
+ maxValue = input[i]; }
|
|
71
|
+ }
|
|
72
|
+ Integer[]outPut = new Integer[input.length];
|
|
73
|
+ for(int j=0; j<input.length; j++) {
|
|
74
|
+ outPut[j]=maxValue;
|
|
75
|
+ }
|
|
76
|
+ return outPut;
|
54
|
77
|
}
|
55
|
78
|
|
56
|
79
|
|
|
@@ -58,29 +81,60 @@ public class ArrayDrills {
|
58
|
81
|
* Given 2 int arrays, a and b, return a new array length 2 containing their middle elements, if length is odd.
|
59
|
82
|
* If the array length is even the sum of the middle 2 elements in the array.
|
60
|
83
|
* example : middleWay([1, 2, 3], [4,5,6,2]); // Should return [2,11]
|
61
|
|
- * middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
|
84
|
+ * middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
85
|
*/
|
63
|
|
- public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
86
|
+ public Integer[] middleWay(Integer[] input1, Integer[] input2) {
|
|
87
|
+ Integer[] newInput = new Integer[2];
|
|
88
|
+ if (input1.length % 2 == 0) {
|
|
89
|
+ newInput[0] = ((input1[input1.length / 2]) + (input1[input1.length / 2 - 1]));
|
|
90
|
+ } else if (input1.length % 2 != 0) {
|
|
91
|
+ newInput[0] = input1[input1.length / 2];
|
|
92
|
+ }
|
|
93
|
+ if (input2.length % 2 == 0) {
|
|
94
|
+ newInput[1] = ((input2[input2.length / 2]) + (input2[input2.length / 2 - 1]));
|
|
95
|
+ } else if (input2.length % 2 != 0) {
|
|
96
|
+ newInput[1] = input2[input2.length / 2];
|
|
97
|
+ }
|
|
98
|
+ return newInput;
|
65
|
99
|
}
|
66
|
100
|
|
67
|
|
-
|
68
|
101
|
/**
|
69
|
102
|
* Start with 2 int arrays, a and b, each length 2.
|
70
|
103
|
* Consider the sum of the values in each array.
|
71
|
104
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
105
|
*/
|
73
|
|
- public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
106
|
+ public Integer[] biggerTwo(Integer[] a, Integer[] b) {
|
|
107
|
+ int sumA = 0;
|
|
108
|
+ int sumB = 0;
|
|
109
|
+
|
|
110
|
+ for (int i = 0; i < a.length; i++) {
|
|
111
|
+ sumA += a[i];
|
|
112
|
+ sumB += b[i];
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ if (sumA >= sumB) {
|
|
116
|
+ return a;
|
|
117
|
+
|
|
118
|
+ }
|
|
119
|
+ return b;
|
75
|
120
|
}
|
76
|
121
|
|
77
|
|
- /**
|
78
|
|
- * Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array.
|
79
|
|
- * The array length will be at least 3.
|
80
|
|
- * example : midThree([1, 2, 3, 4, 5]); // Should return [2, 3, 4]
|
81
|
|
- * midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
|
- */
|
83
|
|
- public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+ /**
|
|
125
|
+ * Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array.
|
|
126
|
+ * The array length will be at least 3.
|
|
127
|
+ * example : midThree([1, 2, 3, 4, 5]); // Should return [2, 3, 4]
|
|
128
|
+ * midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
|
129
|
+ */
|
|
130
|
+ public Integer[] midThree (Integer[]nums){
|
|
131
|
+ Integer[] halfArray = new Integer[3];
|
|
132
|
+ Integer half = (nums.length) / 2;
|
|
133
|
+ halfArray[0] = nums[half-1];
|
|
134
|
+ halfArray[1] = nums[half];
|
|
135
|
+ halfArray[2] = nums[half+1];
|
|
136
|
+ return halfArray;
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+
|
85
|
140
|
}
|
86
|
|
-}
|