Skip to content

Commit 091c746

Browse files
author
Alfredo Miranda
committed
Added Array-2 problems
1 parent b6cc4bd commit 091c746

34 files changed

+605
-0
lines changed

java/array-2/bigDiff.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Given an array length 1 or more of ints, return the difference between the
2+
* largest and smallest values in the array.
3+
*/
4+
public int bigDiff(int[] nums) {
5+
int min = nums[0];
6+
int max = nums[0];
7+
8+
for(int i = 1; i < nums.length; i++) {
9+
min = Math.min(min, nums[i]);
10+
max = Math.max(max, nums[i]);
11+
}
12+
13+
return max - min;
14+
}

java/array-2/centeredAverage.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Return the "centered" average of an array of ints, which we'll say is the
2+
* mean average of the values, except ignoring the largest and smallest
3+
* values in the array. If there are multiple copies of the smallest value,
4+
* ignore just one copy, and likewise for the largest value. Use int division
5+
* to produce the final average. You may assume that the array is length 3
6+
* or more.
7+
*/
8+
public int centeredAverage(int[] nums) {
9+
int sum = 0;
10+
int count = 0;
11+
int min = nums[0];
12+
int max = nums[0];
13+
boolean foundMin = false;
14+
boolean foundMax = false;
15+
16+
for(int i = 1; i < nums.length; i++) {
17+
min = Math.min(min, nums[i]);
18+
max = Math.max(max, nums[i]);
19+
}
20+
21+
for(int i = 0; i < nums.length; i++) {
22+
if(nums[i] != min && nums[i] != max) {
23+
sum += nums[i];
24+
count++;
25+
}
26+
27+
if(nums[i] == min) {
28+
if(foundMin) {
29+
sum += nums[i];
30+
count++;
31+
} else {
32+
foundMin = true;
33+
}
34+
}
35+
36+
if(nums[i] == max) {
37+
if(foundMax) {
38+
sum += nums[i];
39+
count++;
40+
} else {
41+
foundMax = true;
42+
}
43+
}
44+
}
45+
46+
return sum / count;
47+
}

java/array-2/countEvens.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Return the number of even ints in the given array.
2+
*/
3+
public int countEvens(int[] nums) {
4+
int count = 0;
5+
6+
for(int i = 0; i < nums.length; i++) {
7+
if(nums[i] % 2 == 0)
8+
count++;
9+
}
10+
11+
return count;
12+
}

java/array-2/either24.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Given an array of ints, return true if the array contains a 2 next to
2+
* a 2 or a 4 next to a 4, but not both.
3+
*/
4+
public boolean either24(int[] nums) {
5+
boolean has22 = false;
6+
boolean has44 = false;
7+
8+
for(int i = 0; i < nums.length - 1; i++) {
9+
if(nums[i] == 2 && nums[i+1] == 2)
10+
has22 = true;
11+
12+
if(nums[i] == 4 && nums[i+1] == 4)
13+
has44 = true;
14+
}
15+
16+
return has22 != has44;
17+
}

java/array-2/evenOdd.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Return an array that contains the exact same numbers as the given array,
2+
* but rearranged so that all the even numbers come before all the odd
3+
* numbers. Other than that, the numbers can be in any order. You may modify
4+
* and return the given array, or make a new array.
5+
*/
6+
public int[] evenOdd(int[] nums) {
7+
int i = 0;
8+
9+
while(i < nums.length && nums[i] % 2 == 0)
10+
i++;
11+
12+
for(int j = i + 1; j < nums.length; j++) {
13+
if(nums[j] % 2 == 0) {
14+
int temp = nums[i];
15+
nums[i] = nums[j];
16+
nums[j] = temp;
17+
i++;
18+
}
19+
}
20+
21+
return nums;
22+
}

java/array-2/fizzArray.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Given a number n, create and return a new int array of length n,
2+
* containing the numbers 0, 1, 2, ... n-1. The given n may be 0, in which
3+
* case just return a length 0 array. You do not need a separate if-statement
4+
* for the length-0 case; the for-loop should naturally execute 0 times in
5+
* that case, so it just works.
6+
*/
7+
public int[] fizzArray(int n) {
8+
int[] arr = new int[n];
9+
10+
for(int i = 0; i < n; i++)
11+
arr[i] = i;
12+
13+
return arr;
14+
}

java/array-2/fizzArray2.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Given a number n, create and return a new string array of length n,
2+
* containing the strings "0", "1" "2" .. through n-1. N may be 0, in which
3+
* case just return a length 0 array.
4+
*/
5+
public String[] fizzArray2(int n) {
6+
String[] arr = new String[n];
7+
8+
for(int i = 0; i < n; i++)
9+
arr[i] = String.valueOf(i);
10+
11+
return arr;
12+
}

java/array-2/fizzArray3.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Given start and end numbers, return a new array containing the sequence of
2+
* integers from start up to but not including end, so start=5 and end=10
3+
* yields {5, 6, 7, 8, 9}. The end number will be greater or equal to the
4+
* start number. Note that a length-0 array is valid.
5+
*/
6+
public int[] fizzArray3(int start, int end) {
7+
int[] arr = new int[end - start];
8+
9+
for(int i = start; i < end; i++)
10+
arr[i - start] = i;
11+
12+
return arr;
13+
}

java/array-2/fizzBuzz.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* This is slightly more difficult version of the famous FizzBuzz problem
2+
* which is sometimes given as a first problem for job interviews. Consider
3+
* the series of numbers beginning at start and running up to but not
4+
* including end, so for example start=1 and end=5 gives the series
5+
* 1, 2, 3, 4. Return a new String[] array containing the string form of
6+
* these numbers, except for multiples of 3, use "Fizz" instead of the
7+
* number, for multiples of 5 use "Buzz", and for multiples of both 3 and 5
8+
* use "FizzBuzz". In Java, String.valueOf(xxx) will make the String form of
9+
* an int or other type. This version is a little more complicated than the
10+
* usual version since you have to allocate and index into an array instead
11+
* of just printing, and we vary the start/end instead of just always doing
12+
* 1..100.
13+
*/
14+
public String[] fizzBuzz(int start, int end) {
15+
String[] arr = new String[end - start];
16+
17+
for(int i = start; i < end; i++) {
18+
if(i % 15 == 0) {
19+
arr[i - start] = "FizzBuzz";
20+
} else if(i % 3 == 0) {
21+
arr[i - start] = "Fizz";
22+
} else if(i % 5 == 0) {
23+
arr[i - start] = "Buzz";
24+
} else {
25+
arr[i - start] = String.valueOf(i);
26+
}
27+
}
28+
29+
return arr;
30+
}

java/array-2/has12.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Given an array of ints, return true if there is a 1 in the array with a
2+
* 2 somewhere later in the array.
3+
*/
4+
public boolean has12(int[] nums) {
5+
boolean found1 = false;
6+
7+
for(int i = 0; i < nums.length; i++) {
8+
if(nums[i] == 1)
9+
found1 = true;
10+
11+
if(found1 && nums[i] == 2)
12+
return true;
13+
}
14+
15+
return false;
16+
}

java/array-2/has22.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* Given an array of ints, return true if the array contains a 2 next to a 2
2+
* somewhere.
3+
*/
4+
public boolean has22(int[] nums) {
5+
for(int i = 0; i < nums.length - 1; i++) {
6+
if(nums[i] == 2 && nums[i + 1] == 2)
7+
return true;
8+
}
9+
10+
return false;
11+
}

java/array-2/has77.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Given an array of ints, return true if the array contains two 7's next
2+
* to each other, or there are two 7's separated by one element, such as
3+
* with {7, 1, 7}.
4+
*/
5+
public boolean has77(int[] nums) {
6+
for(int i = 0; i < nums.length - 1; i++) {
7+
if(nums[i] == 7 && nums[i+1] == 7)
8+
return true;
9+
10+
if(i <= nums.length - 3 && nums[i] == 7 && nums[i+2] == 7)
11+
return true;
12+
}
13+
14+
return false;
15+
}

java/array-2/haveThree.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Given an array of ints, return true if the value 3 appears in the array
2+
* exactly 3 times, and no 3's are next to each other.
3+
*/
4+
public boolean haveThree(int[] nums) {
5+
int count = 0;
6+
7+
if(nums.length >= 1 && nums[0] == 3)
8+
count++;
9+
10+
for(int i = 1; i < nums.length; i++) {
11+
if(nums[i - 1] == 3 && nums[i] == 3)
12+
return false;
13+
14+
if(nums[i] == 3)
15+
count++;
16+
}
17+
18+
return count == 3;
19+
}

java/array-2/isEverywhere.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* We'll say that a value is "everywhere" in an array if for every pair of
2+
* adjacent elements in the array, at least one of the pair is that value.
3+
* Return true if the given value is everywhere in the array.
4+
*/
5+
public boolean isEverywhere(int[] nums, int val) {
6+
for(int i = 0; i < nums.length - 1; i++) {
7+
if(nums[i] != val && nums[i + 1] != val)
8+
return false;
9+
}
10+
11+
return true;
12+
}

java/array-2/lucky13.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* Given an array of ints, return true if the array contains no 1's and
2+
* no 3's.
3+
*/
4+
public boolean lucky13(int[] nums) {
5+
for(int i = 0; i < nums.length; i++) {
6+
if(nums[i] == 1 || nums[i] == 3)
7+
return false;
8+
}
9+
10+
return true;
11+
}

java/array-2/matchUp.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Given arrays nums1 and nums2 of the same length, for every element in
2+
* nums1, consider the corresponding element in nums2 (at the same index).
3+
* Return the count of the number of times that the two elements differ by 2
4+
* or less, but are not equal.
5+
*/
6+
public int matchUp(int[] nums1, int[] nums2) {
7+
int count = 0;
8+
9+
for(int i = 0; i < nums1.length; i++) {
10+
if(Math.abs(nums1[i] - nums2[i]) <= 2 && nums1[i] != nums2[i])
11+
count++;
12+
}
13+
14+
return count;
15+
}

java/array-2/modThree.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Given an array of ints, return true if the array contains either 3 even
2+
* or 3 odd values all next to each other.
3+
*/
4+
public boolean modThree(int[] nums) {
5+
if(nums.length < 3)
6+
return false;
7+
8+
for(int i = 0; i <= nums.length - 3; i++) {
9+
if(nums[i] % 2 == nums[i+1] % 2 && nums[i] % 2 == nums[i+2] % 2)
10+
return true;
11+
}
12+
13+
return false;
14+
}

java/array-2/more14.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Given an array of ints, return true if the number of 1's is greater than
2+
* the number of 4's
3+
*/
4+
public boolean more14(int[] nums) {
5+
int count1 = 0;
6+
int count4 = 0;
7+
8+
for(int i = 0; i < nums.length; i++) {
9+
if(nums[i] == 1)
10+
count1++;
11+
12+
if(nums[i] == 4)
13+
count4++;
14+
}
15+
16+
return count1 > count4;
17+
}

java/array-2/no14.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* Given an array of ints, return true if it contains no 1's or it contains
2+
* no 4's.
3+
*/
4+
public boolean no14(int[] nums) {
5+
boolean has1 = false;
6+
boolean has4 = false;
7+
for(int i = 0; i < nums.length; i++) {
8+
if(nums[i] == 1)
9+
has1 = true;
10+
11+
if(nums[i] == 4)
12+
has4 = true;
13+
}
14+
15+
return !has1 || !has4;
16+
}

java/array-2/notAlone.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* We'll say that an element in an array is "alone" if there are values
2+
* before and after it, and those values are different from it. Return a
3+
* version of the given array where every instance of the given value which
4+
* is alone is replaced by whichever value to its left or right is larger.
5+
*/
6+
public int[] notAlone(int[] nums, int val) {
7+
int[] arr = new int[nums.length];
8+
9+
if(nums.length >= 1) {
10+
arr[0] = nums[0];
11+
arr[arr.length-1] = nums[nums.length-1];
12+
}
13+
14+
for(int i = 1; i <= nums.length - 2; i++) {
15+
if(nums[i] == val && nums[i] != nums[i-1] && nums[i] != nums[i+1])
16+
arr[i] = Math.max(nums[i-1], nums[i+1]);
17+
else
18+
arr[i] = nums[i];
19+
}
20+
21+
return arr;
22+
}

java/array-2/only14.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Given an array of ints, return true if every element is a 1 or a 4.
2+
*/
3+
public boolean only14(int[] nums) {
4+
for(int i = 0; i < nums.length; i++) {
5+
if(nums[i] != 1 && nums[i] != 4)
6+
return false;
7+
}
8+
9+
return true;
10+
}

java/array-2/post4.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Given a non-empty array of ints, return a new array containing the
2+
* elements from the original array that come after the last 4 in the
3+
* original array. The original array will contain at least one 4. Note that
4+
* it is valid in java to create an array of length 0.
5+
*/
6+
public int[] post4(int[] nums) {
7+
int i = nums.length - 1;
8+
9+
while(nums[i] != 4)
10+
i--;
11+
12+
int[] arr = new int[nums.length - i - 1];
13+
14+
for(int j = 0; j < arr.length; j++)
15+
arr[j] = nums[i + j + 1];
16+
17+
return arr;
18+
}

0 commit comments

Comments
 (0)