How to solve codingbat Array problems

codingBat java

codingbat Array 1 questions and solutions

makePi -  Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}.


public int[] makePi() {
	int[] pi = new int[3];
	pi[0] = 3;
	pi[1] = 1;
	pi[2] = 4;
	return pi;
}

commonEnd -  Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element. Both arrays will be length 1 or more.


public boolean commonEnd(int[] a, int[] b) {
	if ((a[a.length - 1] == b[b.length - 1]) || ((a[0] == b[0]))) {
		return true;
	}
	return false;
}

sum3 -  Given an array of ints length 3, return the sum of all the elements.


public int sum3(int[] nums) {
	return nums[0] + nums[1] + nums[2];
}

rotateLeft3 -  Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.


public int[] rotateLeft3(int[] nums) {
	int temp, temp1;
	temp = nums[0];
	nums[0] = nums[1];
	nums[1] = temp;
	temp1 = nums[1];
	nums[1] = nums[2];
	nums[2] = temp1;
	return nums;
}

reverse3 -  Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.


public int[] reverse3(int[] nums) {
	int[] c = new int[3];
	c[0] = nums[2];
	c[1] = nums[1];
	c[2] = nums[0];
	return c;
}

maxEnd3 -  Given an array of ints length 3, figure out which is larger, the first or last element in the array, and set all the other elements to be that value. Return the changed array.


public int[] maxEnd3(int[] nums) {
	int max = nums[0];
	if (max < nums[2]) {
		max = nums[2];
	}
	for (int i = 0; i< 3; i++)
		nums[i] = max;
	return nums;
}

sum2 -  Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.


public int sum2(int[] nums) {
	if (nums.length > = 2) {
		return nums[0] + nums[1];
	} else if (nums.length > = 1) {
		return nums[0];
	} else {
		return 0;
	}
}

middleWay -  Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements.


public int[] middleWay(int[] a, int[] b) {
	int[] c = new int[2];
	c[0] = a[1];
	c[1] = b[1];
	return c;
}

makeEnds -  Given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more.


public int[] makeEnds(int[] nums) {
	int[] c = new int[2];
	if (nums.length - 1 > = 1) {
		c[0] = nums[0];
		c[1] = nums[nums.length - 1];
	} else {
		c[0] = nums[0];
		c[1] = nums[0];
	}
	return c;
}

has23 -  Given an int array length 2, return true if it contains a 2 or a 3.


public boolean has23(int[] nums) {
	if ((nums[0] == 2) || (nums[0] == 3) || (nums[1] == 2 || nums[1] == 3)) {
		return true;
	} else {
		return false;
	}
}

no23 -  Given an int array length 2, return true if it does not contain a 2 or 3.


public boolean no23(int[] nums) {
	if ((nums[0] != 2) &&
		(nums[0] != 3) &&
		(nums[1] != 2) &&
		(nums[1] != 3)) {
		return true;
	} else {
		return false;
	}
}

makeLast -  Given an int array, return a new array with double the length where its last element is the same as the original array, and all the other elements are 0. The original array will be length 1 or more. Note: by default, a new int array contains all 0's.


public int[] makeLast(int[] nums) {
	int[] c = new int[2 * nums.length];
	c[c.length - 1] = nums[nums.length - 1];
	return c;
}

double23 -  Given an int array, return true if the array contains 2 twice, or 3 twice. The array will be length 0, 1, or 2.


public boolean double23(int[] nums) {
	if (nums.length> = 2) {
		if ((nums[0] == 2 && nums[1] == 2) || (nums[0] == 3 && nums[1] == 3)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

fix23 -  Given an int array length 3, if there is a 2 in the array immediately followed by a 3, set the 3 element to 0. Return the changed array.


public int[] fix23(int[] nums) {
	if ((nums[0] == 2) &&
		(nums[1] == 3)) {
		nums[1] = 0;
		return nums;
	} else if ((nums[1] == 2) &&
		(nums[2] == 3)) {
		nums[2] = 0;
		return nums;
	} else {
		return nums;
	}
}

start1 -  Start with 2 int arrays, a and b, of any length. Return how many of the arrays have 1 as their first element.


public int start1(int[] a, int[] b) {
	int c = 0;
	if (a.length > = 1 &&b.length> = 1) {
		if ((a[0] == 1) &&
			(b[0] == 1)) c = 2;
		else if ((a[0] == 1) || (b[0] == 1)) c = 1;
	} else if (a.length> = 1 && b.length< = 0) {
		if (a[0] == 1) c = 1;
	} else if (b.length> = 1 &&a.length < = 0) {
		if (b[0] == 1) c = 1;
	}
	return c;
}

biggerTwo -  Start with 2 int arrays, a and b, each length 2. Consider the sum of the values in each array. Return the array which has the largest sum. In event of a tie, return a.


public int[] biggerTwo(int[] a, int[] b) {
	int sum1 = a[0] + a[1];
	int sum2 = b[0] + b[1];
	if (sum1 > sum2) {
		return a;
	} else if (sum1 == sum2) {
		return a;
	} else {
		return b;
	}
}

makeMiddle -  Given an array of ints of even length, return a new array length 2 containing the middle two elements from the original array. The original array will be length 2 or more.


public int[] makeMiddle(int[] nums) {
	int[] a = new int[2];
	a[0] = nums[nums.length / 2 - 1];
	a[1] = nums[nums.length / 2 - 1 + 1];
	return a;
}

plusTwo -  Given 2 int arrays, each length 2, return a new array length 4 containing all their elements.


public int[] plusTwo(int[] a, int[] b) {
	int[] c = new int[4];
	c[0] = a[0];
	c[1] = a[1];
	c[2] = b[0];
	c[3] = b[1];
	return c;
}

swapEnds -  Given an array of ints, swap the first and last elements in the array. Return the modified array. The array length will be at least 1.


public int[] swapEnds(int[] nums) {
	int temp;
	temp = nums[0];
	nums[0] = nums[nums.length - 1];
	nums[nums.length - 1] = temp;
	return nums;
}

midThree -  Given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array. The array length will be at least 3.


public int[] midThree(int[] nums) {
	int[] c = new int[3];
	if (nums.length > = 3) {
		c[0] = nums[nums.length / 2 - 1];
		c[1] = nums[nums.length / 2];
		c[2] = nums[nums.length / 2 + 1];
		return c;

	}
	return nums;
}

maxTriple -  Given an array of ints of odd length, look at the first, last, and middle values in the array and return the largest. The array length will be a least 1.


public int maxTriple(int[] nums) {
	int mid = Math.round((nums.length / 2));
	int n3 = nums[mid];
	int odd;
	if (nums[0] > nums[nums.length - 1] &&  nums[0] > nums[mid])
		odd = nums[0];
	else if (nums[nums.length - 1] > nums[0] &&  nums[nums.length - 1] > nums[mid])
		odd = nums[nums.length - 1];
	else
		odd = nums[mid];
	return odd;
}

frontPiece -  Given an int array of any length, return a new array of its first 2 elements. If the array is smaller than length 2, use whatever elements are present.


public int[] frontPiece(int[] nums) {
	int[] c = new int[2];
	if (nums.length > = 2) {
		c[0] = nums[0];
		c[1] = nums[1];
		return c;
	} else {
		return nums;
	}
}

unlucky1 -  We'll say that a 1 immediately followed by a 3 in an array is an "unlucky" 1. Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array.


public boolean unlucky1(int[] nums) {
	if (nums.length - 1 > = 1) {
		if (((nums[0] == 1) &&
				(nums[1] == 3)) || ((nums[1] == 1) &&
				(nums[2] == 3))) {
			return true;
		} else if ((nums[nums.length - 2] == 1) &&
			(nums[nums.length - 1] == 3)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

make2 -  Given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, the elements from a followed by the elements from b. The arrays may be any length, including 0, but there will be 2 or more elements available between the 2 arrays.


public int[] make2(int[] a, int[] b) {
	int[] n = new int[2];
	int l1 = a.length, l2 = b.length;
	if (l1 > = 2 &&l2 > = 2) {
		n[0] = a[0];
		n[1] = a[1];
	} else if (l1 < = 0 &&l2 > = 2) {
		n[0] = b[0];
		n[1] = b[1];
	} else if (l1 > = 2 &&l2 < = 0) {
		n[0] = a[0];
		n[1] = a[1];
	} else {
		n[0] = a[0];
		n[1] = b[0];
	}
	return n;
}

front11 -  Given 2 int arrays, a and b, of any length, return a new array with the first element of each array. If either array is length 0, ignore that array


public int[] front11(int[] a, int[] b) {
	if ((a.length > 0) && 
		(b.length > 0)) {
		return new int[] {
			a[0], b[0]
		};
	} else if ((a.length > 0) &&
		(b.length == 0)) {
		return new int[] {
			a[0]
		};
	} else if ((a.length == 0) &&
		(b.length > 0)) {
		return new int[] {
			b[0]
		};
	} else {
		return new int[0];
	}
}

codingbat Array 2 Questions and solutions java

codingbat Array2

countEvens - Return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.


public int countEvens(int[] nums) {
	int c = 0;
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] % 2 == 0) {
			c = c + 1;
		}
	}
	return c;
}

bigDiff -  Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in Math.min(v1, v2) and Math.max(v1, v2) methods return the smaller or larger of two values.


public int bigDiff(int[] nums) {
	int min = nums[0], max = 0;
	for (int i = 0; i < nums.length; i++) {
		max = Math.max(max, nums[i]);
		if (nums[i] < min) {
			min = nums[i];
		}
	}
	return max - min;
}

centeredAverage - Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.


public int centeredAverage(int[] nums) {
  int sum=0;
  Arrays.sort(nums);
  if(nums.length==3) return nums[1];
  for(int i=1;i&<nums.length-1;i++)
  sum=sum+nums[i];
  return sum/(nums.length-2);
}

sum13 - Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.


public int sum13(int[] nums) {
	int sum = 0;
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] == 13) {
			nums[i] = 0;
			if (i < nums.length - 1) {
				nums[i + 1] = 0;
			}
		}
		sum = sum + nums[i];
	}
	return sum;
}

sum67 -  Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.


public int sum67(int[] nums) {
	int l1 = 0, l2 = 0, sum = 0;
	boolean b = false;
	for (int i = 0; i &< nums.length; i++) {
		if (nums[i] == 6 || b == true) {
			b = true;
			if (nums[i] == 7) b = false;
		} else {
			sum = sum + nums[i];
		}
	}
	return sum;
}

has22 -  Given an array of ints, return true if the array contains a 2 next to a 2 somewhere.


public boolean has22(int[] nums) {
	for (int i = 0; i < nums.length - 1; i++) {
		if (nums[i] == 2 &&nums[i + 1] == 2) {
			return true;
		}
	}
	return false;
}

sum28 -  Given an array of ints, return true if the sum of all the 2's in the array is exactly 8.


public boolean sum28(int[] nums) {
	int sum = 0;
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] == 2) {
			sum = sum + nums[i];
		}
	}
	if (sum == 8) {
		return true;
	}
	return false;
}

more14 -  Given an array of ints, return true if the number of 1's is greater than the number of 4's


public boolean more14(int[] nums) {
	int c = 0, d = 0;
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] == 1) {
			c++;
		}
		if (nums[i] == 4) {
			d++;
		}
	}
	if (c > d) {
		return true;
	}
	return false;
}

fizzArray -  Given a number n, create and return a new int array of length n, containing the numbers 0, 1, 2, ... n-1. The given n may be 0, in which case just return a length 0 array. You do not need a separate if-statement for the length-0 case; the for-loop should naturally execute 0 times in that case, so it just works. The syntax to make a new int array is: new int[desired_length].


public int[] fizzArray(int n) {
	int[] a = new int[n];
	for (int i = 0; i < n; i++) {
		a[i] = i;
	}
	return a;
}

only14 -  Given an array of ints, return true if every element is a 1 or a 4.


public boolean only14(int[] nums) {
	for (int i = 0; i < nums.length; i++) {
		if ((nums[i] != 1) &&
			(nums[i] != 4)) {
			return false;
		}
	}
	return true;
}

fizzArray2 -  Given a number n, create and return a new string array of length n, containing the strings "0", "1" "2" .. through n-1. N may be 0, in which case just return a length 0 array. Note: String.valueOf(xxx) will make the String form of most types. The syntax to make a new string array is: new String[desired_length]


public String[] fizzArray2(int n) {
	String[] s = new String[n];
	for (int i = 0; i < n; i++) {
		s[i] = s[i].valueOf(i);
	}
	return s;
}

either24 -  Given an array of ints, return true if the array contains a 2 next to a 2 or a 4 next to a 4, but not both.


public boolean either24(int[] nums) {
	boolean s = false, s1 = false;
	for (int i = 0; i < nums.length - 1; i++) {
		if ((nums[i] == 2 &&nums[i + 1] == 2))
			s = true;
		if ((nums[i] == 4 &&nums[i + 1] == 4))
			s1 = true;
	}
	if (s1 == s)
		return false;
	else
		return true;
}

matchUp -  Given arrays nums1 and nums2 of the same length, for every element in nums1, consider the corresponding element in nums2 (at the same index). Return the count of the number of times that the two elements differ by 2 or less, but are not equal.


public int matchUp(int[] nums1, int[] nums2) {
	int count = 0;
	for (int i = 0; i < nums1.length; i++) {
		if (nums1[i] != nums2[i]) {
			if (Math.abs(nums2[i] - nums1[i]) < = 2) {
				count++;
			}
		}
	}
	return count;
}

modThree -  Given an array of ints, return true if the array contains either 3 even or 3 odd values all next to each other.


public boolean modThree(int[] nums) {
	int count = 0, count1 = 0;
	int len = nums.length;
	for (int i = 0; i < len; i++) {
		if (nums[i] % 2 == 0) {
			count++;
			if (count1 < 3) count1 = 0;
		} else {
			count1++;
			if (count < 3) count = 0;
		}
	}
	if (count == 3 || count1 == 3)
		return true;
	else
		return false;
}

lucky13 -  Given an array of ints, return true if the array contains no 1's and no 3's.


public boolean lucky13(int[] nums) {
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] == 1 || nums[i] == 3)
			return false;
	}
	return true;
}

shiftLeft -  Return an array that is "left shifted" by one -- so {6, 2, 5, 3} returns {2, 5, 3, 6}. You may modify and return the given array, or return a new array.


public int[] shiftLeft(int[] nums) {
	if (nums.length > 0) {
		int temp = nums[0];
		for (int i = 0; i < nums.length - 1; i++) {
			nums[i] = nums[i + 1];
		}
		nums[nums.length - 1] = temp;
	}
	return nums;
}

tenRun -  For each multiple of 10 in the given array, change all the values following it to be that multiple of 10, until encountering another multiple of 10. So {2, 10, 3, 4, 20, 5} yields {2, 10, 10, 10, 20, 20}


public int[] tenRun(int[] nums) {
	int num10 = 0;
	boolean found10 = false;
	for (int i = 0; i< nums.length; i++) {
		if (nums[i] % 10 == 0) {
			num10 = nums[i];
			found10 = true;
		} else if (nums[i] % 10 != 0 && found10) {
			nums[i] = num10;
		}
	}
	return nums;
}

withoutTen -  Return a version of the given array where all the 10's have been removed. The remaining elements should shift left towards the start of the array as needed, and the empty spaces a the end of the array should be 0. So {1, 10, 10, 2} yields {1, 2, 0, 0}. You may modify and return the given array or make a new array.


public int[] withoutTen(int[] nums) {
	int count = 0;
	int arr[] = new int[nums.length];
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] != 10) {
			arr[count] = nums[i];
			count++;
		}
	}
	return arr;
}

evenOdd -  Return an array that contains the exact same numbers as the given array, but rearranged so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order. You may modify and return the given array, or make a new array.


public int[] evenOdd(int[] nums) {
	int evenCount = 0;
	int oddCount = nums.length - 1;
	int arr[] = new int[nums.length];
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] % 2 == 0) {
			arr[evenCount] = nums[i];
			evenCount++;
		} else {
			arr[oddCount] = nums[i];
			oddCount--;
		}
	}
	return arr;
}

tripleUp -  Return true if the array contains, somewhere, three increasing adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25.


public boolean tripleUp(int[] nums) {
	for (int i = 0; i < = nums.length - 3; i++) {
		if (nums[i] + 1 == nums[i + 1] && nums[i + 1] + 1 == nums[i + 2])
			return true;
	}
	return false;
}

fizzArray3 -  Given start and end numbers, return a new array containing the sequence of integers from start up to but not including end, so start=5 and end=10 yields {5, 6, 7, 8, 9}. The end number will be greater or equal to the start number. Note that a length-0 array is valid.


public int[] fizzArray3(int start, int end) {
	int size = end - start;
	int arr[] = new int[size];
	for (int i = 0; i < size; i++) {
		arr[i] = start++;
	}
	return arr;
}

no14 -  Given an array of ints, return true if it contains no 1's or it contains no 4's.


public boolean no14(int[] nums) {
	boolean is4 = false;
	boolean is1 = false;
	if (nums.length < = 1)
		return true;
	for (int i = 0; i< nums.length; i++) {
		if (nums[i] == 1)
			is1 = true;
		if (nums[i] == 4)
			is4 = true;
	}
	return (is4 != is1);
}

pre4 -  Given a non-empty array of ints, return a new array containing the elements from the original array that come before the first 4 in the original array. The original array will contain at least one 4. Note that it is valid in java to create an array of length 0.


public int[] pre4(int[] nums) {
	int[] result = new int[0];
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] == 4) {
			result = new int[i];
			result = Arrays.copyOfRange(nums, 0, i);
			break;
		}
	}
	return result;
}

Codingbat Array3 questions and solutions in java

Codingbat Array3

linearIn -  Given two arrays of ints sorted in increasing order, outer and inner, return true if all of the numbers in inner appear in outer. The best solution makes only a single "linear" pass of both arrays, taking advantage of the fact that both arrays are already in sorted order.


public boolean linearIn(int[] outer, int[] inner) {
	int foundCount = 0;
	for (int i = 0; i < inner.length; i++) {
		for (int j = 0; j < outer.length; j++) {
			if (inner[i] == outer[j]) {
				foundCount++;
				break;
			}
		}
	}
	return (foundCount == inner.length);
}

canBalance -  Given a non-empty array, return true if there is a place to split the array so that the sum of the numbers on one side is equal to the sum of the numbers on the other side.


public boolean canBalance(int[] nums) {
	int sum1 = 0, sum2 = 0;
	boolean canBalance = false;
	for (int i = 0; i < nums.length; i++) {
		sum1 += nums[i];
		for (int j = i + 1; j < nums.length; j++) {
			sum2 += nums[j];
		}
		if (sum1 == sum2) {
			canBalance = true;
			break;
		}
		sum2 = 0;
	}
	return canBalance;
}
Also Checkout other codingbat solutions
codingbat String 2 codingbat Logic 2

Post a Comment

Previous Post Next Post

Recent Posts

Facebook