|
@@ -1,5 +1,7 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import com.sun.media.sound.SoftMixingMixerProvider;
|
|
4
|
+
|
3
|
5
|
public class ArrayDrills {
|
4
|
6
|
|
5
|
7
|
|
|
@@ -10,7 +12,12 @@ public class ArrayDrills {
|
10
|
12
|
* firstLast(6, [1,2,3]); // Should return false
|
11
|
13
|
*/
|
12
|
14
|
public Boolean firstLast(Integer value, Integer[] input){
|
13
|
|
- return null;
|
|
15
|
+ if(input[0]==value ||input[input.length-1]==value)
|
|
16
|
+ {
|
|
17
|
+ return true;
|
|
18
|
+ }
|
|
19
|
+
|
|
20
|
+ return false;
|
14
|
21
|
}
|
15
|
22
|
|
16
|
23
|
/**
|
|
@@ -19,7 +26,11 @@ public class ArrayDrills {
|
19
|
26
|
* sameFirstLast([1,2,1]); // Should return true
|
20
|
27
|
*/
|
21
|
28
|
public Boolean sameFirstLast(Integer[] input){
|
22
|
|
- return null;
|
|
29
|
+ if(input.length!=0&&input[0]==input[input.length-1])
|
|
30
|
+ {
|
|
31
|
+ return true;
|
|
32
|
+ }
|
|
33
|
+ return false;
|
23
|
34
|
}
|
24
|
35
|
|
25
|
36
|
|
|
@@ -30,7 +41,11 @@ public class ArrayDrills {
|
30
|
41
|
* commonEnd([1, 2, 3], [7, 3, 2]); // Should return false
|
31
|
42
|
*/
|
32
|
43
|
public Boolean commonEnd(Integer[] input1, Integer[] input2){
|
33
|
|
- return null;
|
|
44
|
+ if(input1[0]==input2[0]||input1[input1.length-1]==input2[input2.length-1])
|
|
45
|
+ {
|
|
46
|
+ return true;
|
|
47
|
+ }
|
|
48
|
+ return false;
|
34
|
49
|
}
|
35
|
50
|
|
36
|
51
|
/**
|
|
@@ -39,7 +54,15 @@ public class ArrayDrills {
|
39
|
54
|
* rotateLeft([5, 11, 9]); // Should return [11,9,5]
|
40
|
55
|
*/
|
41
|
56
|
public Integer[] rotateLeft(Integer[] input){
|
42
|
|
- return null;
|
|
57
|
+ Integer[] newArray=new Integer[input.length];
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+ for(int i=0;i<input.length-1;i++)
|
|
61
|
+ {
|
|
62
|
+ newArray[i]=input[i+1];
|
|
63
|
+ }
|
|
64
|
+ newArray[input.length-1]=input[0];
|
|
65
|
+ return newArray;
|
43
|
66
|
}
|
44
|
67
|
|
45
|
68
|
|
|
@@ -50,7 +73,20 @@ public class ArrayDrills {
|
50
|
73
|
* maxValue([5, 11, 9]); // Should return [11,11,11]
|
51
|
74
|
*/
|
52
|
75
|
public Integer[] maxValue(Integer[] input){
|
53
|
|
- return null;
|
|
76
|
+ int max=0;
|
|
77
|
+ Integer[] newArray =new Integer[input.length];
|
|
78
|
+ for (int i=0;i<input.length;i++)
|
|
79
|
+ {
|
|
80
|
+ if(input[i]>max)
|
|
81
|
+ {
|
|
82
|
+ max=input[i];
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+ for (int i=0;i<input.length;i++)
|
|
86
|
+ {
|
|
87
|
+ newArray[i]=max;
|
|
88
|
+ }
|
|
89
|
+ return newArray;
|
54
|
90
|
}
|
55
|
91
|
|
56
|
92
|
|
|
@@ -61,7 +97,28 @@ public class ArrayDrills {
|
61
|
97
|
* middleWay([5, 1, 2, 9], [3, 4, 5, 5]); // Should return [3, 9]
|
62
|
98
|
*/
|
63
|
99
|
public Integer[] middleWay(Integer[] input1, Integer[] input2){
|
64
|
|
- return null;
|
|
100
|
+ Integer[] newArray=new Integer[2];
|
|
101
|
+ int len1=input1.length;
|
|
102
|
+ int len2=input2.length;
|
|
103
|
+ if(len1%2!=0)//odd input1
|
|
104
|
+ {
|
|
105
|
+ newArray[0]=input1[len1/2];
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+ }
|
|
109
|
+ else//even input1
|
|
110
|
+ {
|
|
111
|
+ newArray[0]=input1[(len1-1)/2]+input1[len1/2];
|
|
112
|
+ }
|
|
113
|
+ if(len2%2!=0)//odd
|
|
114
|
+ {
|
|
115
|
+ newArray[1]=input2[len2/2];
|
|
116
|
+ }
|
|
117
|
+ else
|
|
118
|
+ {
|
|
119
|
+ newArray[1]=input2[(len2-1)/2]+input2[(len2/2)];
|
|
120
|
+ }
|
|
121
|
+ return newArray;
|
65
|
122
|
}
|
66
|
123
|
|
67
|
124
|
|
|
@@ -71,7 +128,24 @@ public class ArrayDrills {
|
71
|
128
|
* Return the array which has the largest sum. In event of a tie, return a.
|
72
|
129
|
*/
|
73
|
130
|
public Integer[] biggerTwo(Integer[] a, Integer[] b){
|
74
|
|
- return null;
|
|
131
|
+ int sum1=0;
|
|
132
|
+ int sum2=0;
|
|
133
|
+
|
|
134
|
+ for(int i=0;i<a.length;i++)
|
|
135
|
+ {
|
|
136
|
+ sum1=sum1+a[i];
|
|
137
|
+ sum2=sum2+b[i];
|
|
138
|
+ }
|
|
139
|
+ if(sum1==sum2)
|
|
140
|
+ {
|
|
141
|
+ return a;
|
|
142
|
+ }
|
|
143
|
+ else if(sum1>sum2)
|
|
144
|
+ {
|
|
145
|
+ return a;
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ return b;
|
75
|
149
|
}
|
76
|
150
|
|
77
|
151
|
/**
|
|
@@ -81,6 +155,19 @@ public class ArrayDrills {
|
81
|
155
|
* midThree([8, 6, 7, 5, 3, 0, 9]); // Should return [7, 5, 3]
|
82
|
156
|
*/
|
83
|
157
|
public Integer[] midThree(Integer[] nums){
|
84
|
|
- return null;
|
|
158
|
+ Integer[] newArray= new Integer[3];
|
|
159
|
+ int len=nums.length;
|
|
160
|
+ int mid=len/2;
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+ newArray[0]=nums[mid-1];
|
|
164
|
+ newArray[1]=nums[mid];
|
|
165
|
+ newArray[2]=nums[mid+1];
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+ System.out.println(newArray);
|
|
171
|
+ return newArray;
|
85
|
172
|
}
|
86
|
173
|
}
|