CodingBat: Java. Array-1, Part III

swapEnds:

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

midThree:

public int[] midThree(int[] nums) {
	int mid = nums.length / 2;
	int[] result = { nums[mid - 1], nums[mid], nums[mid + 1] };
	return result;
}

The purpose of the variable mid is to make the code a bit more concise.

maxTriple:

public int maxTriple(int[] nums) {
	int mid = nums.length / 2;
	int end = nums.length - 1;
	int max = nums[0];
	if (nums[mid] > max) max = nums[mid];
	if (nums[end] > max) max = nums[end];
	return max;
}

frontPiece:

public int[] frontPiece(int[] nums) {
	if (nums.length <= 1) return nums;
	int[] result = { nums[0], nums[1] };
	return result;
}

unlucky1:

public boolean unlucky1(int[] nums) {
	int len = nums.length;
	if (len <= 1) return false;

	for (int i = 0; i <= 1; i++) {
		if (nums[i] == 1 && nums[i + 1] == 3)
			return true;
		if (len < 3) break;
	}

	return nums[len - 2] == 1 && nums[len - 1] == 3;
}

make2:

public int[] make2(int[] a, int[] b) {
	int[] res = new int[2];
	if (a.length == 0) {
		res[0] = b[0];
		res[1] = b[1];
	} else if (a.length == 1) {
		res[0] = a[0];
		res[1] = b[0];
	} else {
		res[0] = a[0];
		res[1] = a[1];
	}
	return res;
}

front11:

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

13 thoughts on “CodingBat: Java. Array-1, Part III

  1. Tay

    Thank you so much for posting the solutions! You are super smart. I was wondering about the “unlucky1”. What does the break statement do in this code? Can you please tell me why they were important in this code? I know that it will not work without them, I’m just confused how they work.

    -Tay

    Reply
    1. Gregor Ulm Post author

      The exercise asks you to check whether there is an “unlucky 1”, defined as a 1 immediately followed by a 3, in either the first two or last two positions of the array. Please note that the last line in the script returns False, which means that there was no “unlucky 1” in the array.

      This leads to the following strategy: you first check whether there is an “unlucky 1” at the front of the array. If there isn’t you break out of the for loop with the “break” statement. This leads to executing the second for loop, whether you check whether there is an “unlucky 1” at the end of the array.

      If both for loops don’t discover an “unlucky 1”, then you return “False”.

      Lastly, I’m using two for loops instead of a simple check because the “unlucky 1” could either be in position 0 or 1, or in position “nums.length-2” or “nums.length-1”.

      Reply
  2. Gregor Ulm Post author

    Just to record this since there is a comment that refers to unlucky1: I have rewritten the solution to remove the break statement by changing the conditions in the for loops. But here is the old code, which is equivalent, albeit slightly more verbose:

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

    Unfortunately code of unlucky1 will not work for {n, 1} array, where n is any integer.
    First loop will refer to index 2, which is outside the array’s upper bound.

    Reply
    1. Gregor Ulm Post author

      Good catch! I added a check for this condition, and simplified the second half of my solution, which works just as well without a loop.

      Reply
  4. Maxim

    Thank you for providing all the answers to the CodingBat website.

    Regarding the unlucky1 problem, I don’t think you’re supposed to use loops to solve it. It can easily be replaced with if..else statements since it only lasts for 2 iterations. Here is my solution to this problem, it’s slightly more elaborate though.


    public boolean unlucky1(int[] nums) {
    if (nums.length < 2) {
    return false;
    }

    int last = nums.length - 1;

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

    if (nums[last-1] == 1 && nums[last] == 3) {
    return true;
    }

    return false;
    }

    Reply
  5. XinKenan

    my attempt at unlucky1
    public boolean unlucky1(int[] nums) {
    if (nums.length 2 && nums[1] == 1 && nums[2] ==3
    || (nums[nums.length-2] == 1 && nums[nums.length-1] ==3)));
    }

    Reply
  6. James Gallhaway

    This is how I did unlucky1 (and it works):

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

    Reply
  7. Jimmy Twoninetwonince

    For swapEnds, though overly complicated, you could also use:

    public int[] swapEnds(int[] nums) {
    if(nums.length>1){
    int[] array ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    array[0]=nums[nums.length-1];
    int i=0;
    for(i =1;i<nums.length-1;i++){
    array[i]=nums[i];
    }
    array[i]=nums[0];
    int j=0;
    for(j=0;i<array.length;j++){
    if(array[j]==0){
    break;
    }
    }
    return Arrays.copyOfRange(array, 0, j);
    }else return nums;
    }

    Reply
  8. 321

    MAXTRIPLE

    public int maxTriple(int[] nums)
    {
    int first = nums[0];
    int middle = nums[nums.length/2];
    int end = nums[nums.length- 1];
    return ((first > middle ? first : middle) > end ? (first > middle ? first : middle) : end);
    }

    Reply
  9. 123

    How I did unlucky1:
    public boolean unlucky1(int[] nums) {
    if(nums.length < 2) return false;
    if(nums[0] == 1 && nums[1] == 3 || nums[1] == 1 && nums[2] == 3) return true;
    if(nums[nums.length-2] == 1 & nums[nums.length-1] == 3) return true;
    return false;
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Spammer prevention; the answer is an integer: * Time limit is exhausted. Please reload CAPTCHA.