Monthly Archives: March 2013

CodingBat: Java. Array-2, Part I


For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


The Array-2 section of CodingBat present 30 problems of varying difficulty. Most you should be able to solve straight away, while a few may take you up to half an hour or so.

All solutions were successfully tested on 3 March 2013.

countEvens:

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

bigDiff:

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

It wasn’t clear to me why the instructions mentioned the inbuilt Math.min(int a, int b) and Math.max(int a, int b) functions. If you use those, you’ll only end up with a more complicated method.

A common mistake people make in this kind of exercise is to initialize the maximum or minimum value with 0 and a very large number, respectively, instead of using an actual value from the array. Given the test cases on CodingBat, you would get away with, for instance, initializing “max” with 0 and “min” with 1000, but if the array you process only consists of numbers that are either all smaller than “max” or all larger than “min”, you’d get the wrong result.

centeredAverage:

public int centeredAverage(int[] nums) {
	int max = nums[0];
	int min = nums[0];
	int sum = 0;

	for (int i = 0; i < nums.length; i++) {
		sum += nums[i];
		if (nums[i] > max) max = nums[i];
		if (nums[i] < min) min = nums[i];
	}
	return (sum - (max + min)) / (nums.length - 2);
}

sum13:

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

sum67:

public int sum67(int[] nums) {
	int sum = 0;
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] != 6) sum += nums[i];
		else
			while (nums[i] != 7) i++;
	}
	return sum;
}

has22:

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

lucky13:

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

sum28:

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

more14:

public boolean more14(int[] nums) {
	int ones = 0;
	int fours = 0;

	for (int i = 0; i < nums.length; i++) {
		if (nums[i] == 1) ones++;
		if (nums[i] == 4) fours++;
	}
	return ones > fours;
}

only14:

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

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.