|
@@ -1,4 +1,5 @@
|
1
|
1
|
package io.zipcoder;
|
|
2
|
+import java.util.*;
|
2
|
3
|
|
3
|
4
|
public class ArrayDrills {
|
4
|
5
|
|
|
@@ -68,13 +69,12 @@ public class ArrayDrills {
|
68
|
69
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
69
|
70
|
*/
|
70
|
71
|
public Integer[] rotateLeft(Integer[] input){
|
71
|
|
- Integer[] arr = new Integer[input.length-1];
|
72
|
|
- arr[0] = input[input.length-1];
|
73
|
|
- for(int i=1;i<input.length;i++) {
|
74
|
|
- arr[i] = input[i - 1];
|
|
72
|
+ Integer[] arr = new Integer[input.length];
|
|
73
|
+ for(int i=0;i<input.length;i++) {
|
|
74
|
+ arr[(i+(input.length-1))%input.length] = input[i];
|
75
|
75
|
|
76
|
76
|
}
|
77
|
|
- return null;
|
|
77
|
+ return arr;
|
78
|
78
|
}
|
79
|
79
|
|
80
|
80
|
|
|
@@ -87,7 +87,23 @@ public class ArrayDrills {
|
87
|
87
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
88
|
88
|
*/
|
89
|
89
|
public Integer[] maxValue(Integer[] input){
|
90
|
|
- return null;
|
|
90
|
+ Integer [] arr = new Integer[input.length];
|
|
91
|
+ Integer element = null;
|
|
92
|
+ for(int i = 0; i < input.length;i++ ){
|
|
93
|
+ Integer tempElem = input[i];
|
|
94
|
+ for (int j =0; j<input.length; j++){
|
|
95
|
+ if (input[j] > tempElem){
|
|
96
|
+ element = input[j];
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ }
|
|
101
|
+ for (int x = 0; x < arr.length;x++ ){
|
|
102
|
+ arr[x] = element;
|
|
103
|
+
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ return arr;
|
91
|
107
|
}
|
92
|
108
|
|
93
|
109
|
|
|
@@ -98,7 +114,23 @@ public class ArrayDrills {
|
98
|
114
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
99
|
115
|
*/
|
100
|
116
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
101
|
|
- return null;
|
|
117
|
+ Integer [] arr = new Integer[2];
|
|
118
|
+ int sum;
|
|
119
|
+ int sum1;
|
|
120
|
+ if (input1.length % 2 == 0){
|
|
121
|
+ sum = input1[input1.length/2 -1] + input1[input1.length/2];
|
|
122
|
+ } else{
|
|
123
|
+ sum = input1[(input1.length+1)/2 -1];
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ if (input2.length % 2 == 0){
|
|
127
|
+ sum1 = input2[input2.length/2 -1] + input2[input2.length/2];
|
|
128
|
+ } else{
|
|
129
|
+ sum1 = input2[(input2.length+1)/2 -1];
|
|
130
|
+ }
|
|
131
|
+ arr[0] = sum;
|
|
132
|
+ arr[1] = sum1;
|
|
133
|
+ return arr;
|
102
|
134
|
}
|
103
|
135
|
|
104
|
136
|
|
|
@@ -108,7 +140,14 @@ public class ArrayDrills {
|
108
|
140
|
* Return the array which has the largest sum. In event of a tie, return a.
|
109
|
141
|
*/
|
110
|
142
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
111
|
|
- return null;
|
|
143
|
+ int sum;
|
|
144
|
+ int sum1;
|
|
145
|
+ sum = a[0] + a[1];
|
|
146
|
+ sum1 = b[0] + b[1];
|
|
147
|
+ if (sum >= sum1){
|
|
148
|
+ return a;
|
|
149
|
+ }
|
|
150
|
+ return b;
|
112
|
151
|
}
|
113
|
152
|
|
114
|
153
|
/**
|
|
@@ -118,6 +157,21 @@ public class ArrayDrills {
|
118
|
157
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
119
|
158
|
*/
|
120
|
159
|
public Integer[] midThree(Integer[] nums){
|
121
|
|
- return null;
|
|
160
|
+ int n0;
|
|
161
|
+ int n1;
|
|
162
|
+ int n2;
|
|
163
|
+ Integer [] arr = new Integer[3];
|
|
164
|
+ if(nums.length == 3){
|
|
165
|
+ return nums;
|
|
166
|
+ }
|
|
167
|
+ else {
|
|
168
|
+ n0 = nums[(nums.length+1)/2 -2];
|
|
169
|
+ n1 = nums[ (nums.length+1)/2 -1];
|
|
170
|
+ n2 = nums[(nums.length+1)/2 ];
|
|
171
|
+ arr[0] = n0;
|
|
172
|
+ arr[1] = n1;
|
|
173
|
+ arr[2] = n2;
|
|
174
|
+ }
|
|
175
|
+ return arr;
|
122
|
176
|
}
|
123
|
177
|
}
|