Skip to content

Commit 68a87e7

Browse files
author
Alfredo Miranda
committed
Added array-1 problems
1 parent 189cbbe commit 68a87e7

27 files changed

+285
-0
lines changed

java/array-1/biggerTwo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Start with 2 int arrays, a and b, each length 2. Consider the sum of the
2+
* values in each array. Return the array which has the largest sum. In event
3+
* of a tie, return a.
4+
*/
5+
public int[] biggerTwo(int[] a, int[] b) {
6+
if(a[0] + a[1] < b[0] + b[1])
7+
return b;
8+
9+
return a;
10+
}

java/array-1/commonEnd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* Given 2 arrays of ints, a and b, return true if they have the same first
2+
* element or they have the same last element. Both arrays will be length 1 or
3+
* more.
4+
*/
5+
public boolean commonEnd(int[] a, int[] b) {
6+
return a[0] == b[0] || a[a.length - 1] == b[b.length - 1];
7+
}

java/array-1/double23.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Given an int array, return true if the array contains 2 twice, or 3 twice.
2+
* The array will be length 0, 1, or 2.
3+
*/
4+
public boolean double23(int[] nums) {
5+
int count2 = 0;
6+
int count3 = 0;
7+
8+
for(int i = 0; i < nums.length; i++) {
9+
if(nums[i] == 2)
10+
count2++;
11+
12+
if(nums[i] == 3)
13+
count3++;
14+
}
15+
16+
return count2 == 2 || count3 == 2;
17+
}

java/array-1/firstLast6.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* Given an array of ints, return true if 6 appears as either the first or
2+
* last element in the array. The array will be length 1 or more.
3+
*/
4+
public boolean firstLast6(int[] nums) {
5+
return nums[0] == 6 || nums[nums.length - 1] == 6;
6+
}

java/array-1/fix23.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* Given an int array length 3, if there is a 2 in the array immediately
2+
* followed by a 3, set the 3 element to 0. Return the changed array.
3+
*/
4+
public int[] fix23(int[] nums) {
5+
for(int i = 0; i < nums.length - 1; i++) {
6+
if(nums[i] == 2 && nums[i + 1] == 3)
7+
nums[i + 1] = 0;
8+
}
9+
10+
return nums;
11+
}

java/array-1/front11.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Given 2 int arrays, a and b, of any length, return a new array with the
2+
* first element of each array. If either array is length 0, ignore that array.
3+
*/
4+
public int[] front11(int[] a, int[] b) {
5+
if(a.length > 0 && b.length > 0) {
6+
return new int[] {a[0], b[0]};
7+
} else if(a.length > 0) {
8+
return new int[] {a[0]};
9+
} else if(b.length > 0) {
10+
return new int[] {b[0]};
11+
}
12+
13+
return new int[0];
14+
}

java/array-1/frontPiece.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Given an int array of any length, return a new array of its first 2
2+
* elements. If the array is smaller than length 2, use whatever elements are
3+
* present.
4+
*/
5+
public int[] frontPiece(int[] nums) {
6+
int[] arr;
7+
if(nums.length < 2)
8+
arr = new int[nums.length];
9+
else
10+
arr = new int[2];
11+
12+
if(nums.length > 0)
13+
arr[0] = nums[0];
14+
15+
if(nums.length > 1)
16+
arr[1] = nums[1];
17+
18+
return arr;
19+
}

java/array-1/has23.java

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

java/array-1/make2.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Given 2 int arrays, a and b, return a new array length 2 containing, as
2+
* much as will fit, the elements from a followed by the elements from b. The
3+
* arrays may be any length, including 0, but there will be 2 or more
4+
* elements available between the 2 arrays.
5+
*/
6+
public int[] make2(int[] a, int[] b) {
7+
int[] arr = new int[2];
8+
int count = 0;
9+
int i;
10+
11+
i = 0;
12+
while(count < 2 && i < a.length) {
13+
arr[count] = a[i];
14+
count++;
15+
i++;
16+
}
17+
18+
i = 0;
19+
while(count < 2 && i < b.length) {
20+
arr[count] = b[i];
21+
count++;
22+
i++;
23+
}
24+
25+
return arr;
26+
}

java/array-1/makeEnds.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* Given an array of ints, return a new array length 2 containing the first
2+
* and last elements from the original array. The original array will be
3+
* length 1 or more.
4+
*/
5+
public int[] makeEnds(int[] nums) {
6+
return new int[] {nums[0], nums[nums.length - 1]};
7+
}

0 commit comments

Comments
 (0)