**no14:**

public boolean no14(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 == 0 || fours == 0; }

**isEverywhere:**

public boolean isEverywhere(int[] nums, int val) { boolean flag1 = true; boolean flag2 = true; for (int i = 0; i < nums.length; i += 2) if (nums[i] != val) flag1 = false; for (int i = 0; i < nums.length - 1; i += 2) if (nums[i + 1] != val) flag2 = false; return flag1 || flag2; }

**either24:**

public boolean either24(int[] nums) { Boolean twos = false; Boolean fours = false; for (int i = 0; i < nums.length - 1; i++) { if (nums[i] == 2 && nums[i + 1] == 2) twos = true; if (nums[i] == 4 && nums[i + 1] == 4) fours = true; } return twos ^ fours; }

The caret (^) in the return statement represents the logical XOR operator.

**matchUp:**

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

**has77:**

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

**has12:**

public boolean has12(int[] nums) { int one = 0; int two = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] == 1) one = i; if (nums[i] == 2) two = i; } return two > one; }

The two variables keep track of the position of the number 1 and 2, respectively. After the for loop has finished, they will contain the position of the last 1 and last 2 in the array.

**modThree:**

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

**haveThree:**

public boolean haveThree(int[] nums) { int count = 0; int pos = -2; // in case nums[0] == 3 for (int i = 0; i < nums.length; i++) { if (nums[i] == 3) { count++; if (i - pos == 1) return false; pos = i; } } return count == 3; }

**twoTwo:**

public boolean twoTwo(int[] nums) { for (int i = 0; i < nums.length; i++) if (nums[i] == 2) { int count = 0; for (int j = i; j < nums.length; j++) if (nums[j] == 2) count++; else break; i += count; if (count < 2) return false; } return true; }

**sameEnds:**

public boolean sameEnds(int[] nums, int len) { for (int i = 0, j = nums.length - len; i < len; i++, j++) if (nums[i] != nums[j]) return false; return true; }

The for loop traverses the array from back to front and front to back simultaneously.

Louis N.Firstly, I’d like to thank you for taking the time to create meaningful solutions to the CodingBat exercises. My comment here is actually in response to your solution for the exercise “haveThree” in the section Array-2: http://codingbat.com/prob/p109783

While your solution is marked correct by CodingBats, I believe there is one problem with it that CodingBats didn’t check. This is your solution found at: http://gregorulm.com/codingbat-java-array-2-part-ii/

`public boolean haveThree(int[] nums) {`

boolean foundFirst = false;

int first3 = 0;

int last3 = 0;

int count = 0;

`for (int i = 0; i = 4);`

}

The point in question is the return statement. You check to see if the first “3” is far enough away from the last “3” for it to be possible to have a number in between each 3. It does not actually check if there is a 3 next to any other three, however. So if the array were to be initialized to {3, 3, 1, 1, 3} your program would return true when it should return false. CodingBats never initializes the array as such, which gives the illusion that your solution is correct.

Thanks again for your time, and I apologize if I am incorrect.

Gregor UlmPost authorHi Louis,

thank you very much for pointing this out! You are right indeed. Coding Bat does not cover the case you mentioned, and my solution didn’t account for it either. I have therefore rewritten the code, and updated the original post with a shorter and, in my opinion, cleaner solution. So, thanks again for letting me know.

On a related note, my first attempt to rewrite the solution consisted of a single traversal of the array, minus the very last element, and checking whether a “3” is immediately followed by another “3”. I had a counter variable to keep track of the number of occurrences of “3”, and of course I had to add one last check for the very last element of the array. This solution passed all tests on Coding Bat except the “other tests” part, so I had to find a workaround for this issue of the auto grader, which inspired the solution I posted. Unexpectedly, this problem therefore turned into a neat diversion.

FrankHi Gregor,

Thanks for your work!

I’ve got a remark about the has12 function. It is stated “Given an array of ints, return true if there is a 1 in the array with a 2 somewhere later in the array. “. However, as you check the position of the last 1 and of the last 2, you do not take into account something like {1,3,2,1}. This set should result in a true, however your function would return false, as there is a final 1. This case is not covered by the CodingBat tests.

My solution looks like this:

public boolean has12(int[] nums) {

int i = 0;

for(; i < nums.length; i++)

// a 1 is found

if(nums[i] == 1)

break;

//We start looking for a 2 from the position of the first found1

for (int j=i; j < nums.length; j++)

if(nums[j] ==2) return true;

return false;

}

Gregor UlmPost authorHi Frank,

thanks for your comment. Since I did not specify the requirements for has12, I can only rely on my own interpretation, based on the test cases Nick Parlante provides on his site. To me it therefore seems that his description on the problem — “return true if there is a 1 in the array with a 2 somewhere later in the array” — has to be understood as returning False if there is a 1 that is not followed by a 2 later in the array. Therefore, the case you provide, {1,3,2,1}, should result in False, not True.

I’m afraid we have to agree to disagree here, but please note that you’re writing code for a merely imagined test case. Based on Nick Parlante’s test cases, your solution is therefore needlessly complicated.

FrankThanks for the reply and let us indeed agree to disagree here.

HenrikHi Gregor,

comparing my answers of the Codingbat problems to yours and I have learnt quite a bit form your examples.

However, here I have to agree with Frank. You solution on “has12” is flawed.

To quote: “if there is A 1 in the array with a 2 somewhere later” specifies a single possible case not a case for each ‘1’. Franks case is indeed needlessly complicated, and your needlessly checks the entire array and gives some incorrect outcomes.

This is clearer:

`boolean has1=false;`

`for (int i : nums) {`

if(i == 1)

has1 = true;

if(has1 && i == 2)

return true;

}

return false;

Gregor UlmPost authorIt doesn’t really matter what we think the specification means. Nick Parlante, I presume, wrote the test cases on the Coding Bat website, and he would need to be the final arbiter in this case. I think my interpretation is obvious, and the only plausible one. You think the same about yours. My code passes all test cases on the Coding Bat site; yours presumably does as well. Since the test cases permit at least two solutions who differ in a non-trivial aspect, this problem is therefore underspecified. (Please note that I do not want to encourage you, or anyone else, to email Nick Parlante about this matter.)

FrankHi Gregor,

Concerning the “modThree” exercise, I would have a remark/alternative solution. The solution below is slightly less verbose than yours, as it only checks that three consecutive elements have the same parity, without checking whether they are even or odd.

public boolean modThree(int[] nums) {

for(int i = 0; i < nums.length-2; i++)

if( nums[i] % 2 == nums[i+1] % 2 && nums[i+1] % 2 == nums[i+2] %2 ) return true;

return false;

}

Thank again for the great work provided.

Gregor UlmPost authorThis is a neat idea! Thanks for letting me know.

whistl3rthe modThree solution is elegant. You should have seen the 60 line “solution” that i spent so many days on that btw did not work all the way looked like. Thanks for uploading it…

WayneHi Gregor,

For twoTwo, I think your solution didn’t take into account the situation where three “2” are placed in the middle, for example {3,3,2,2,2,1,1,3}. Following is my solution:

public boolean twoTwo(int[] nums) {

for (int i=0; i<nums.length; i++) {

if (nums[i]==2) {

if ((i+10 && nums[i-1]==2)) {

i++;

}

else return false;

}

}

return true;

}

Thanks so much for your effort and time.

Gregor UlmPost authorMy solution could probably be rewritten a bit more nicely. I might have a go at it later today. However, your code looks odd to me. Did you paste all of it? For one, (i+10 && nums[i-1]==2) isn’t a legal statement, and ‘i+10’ is cryptic to begin with.

WayneAha, that’s weird, because I just copied and pasted. Now it should work.

public boolean twoTwo(int[] nums) {

for (int i=0; i<nums.length; i++) {

if (nums[i]==2) {

if ((i+10 && nums[i-1]==2)) {

i++;

}

else return false;

}

}

return true;

}

WayneOkay, i don’t know what happened. Try one more time.

public boolean twoTwo(int[] nums) {

for (int i=0; i<nums.length; i++) {

if (nums[i]==2) {

if ((i+10 && nums[i-1]==2)) {

i++;

}

else return false;

}

}

return true;

}

WayneSorry for the spams. How can I delete my replies? The condition before i++ is:

(i+10 && nums[i-1]==2)

and the i++ should be removed. So the complete code is as following:

public boolean twoTwo(int[] nums) {

for (int i=0; i<nums.length; i++) {

if (nums[i]==2) {

if ((i+10 && nums[i-1]==2));

else return false;

}

}

return true;

}

I really hope it works this time……….

Gregor UlmPost authorYour code still doesn’t make any sense. It doesn’t even compile. Anyway, I’ve rewritten my solution to TwoTwo, which takes your test case into account. It has the added benefit that it is easier to understand, compared to the previous one.

WayneThanks for your time and sorry for my spams. Every time I wrote the comment, everything worked, but once I posted, it has been changed without reasons. Of course the posted version cannot compile…

Really the last post, if it doesn’t work, then just ignore it. The condition is

if (nums[i+1]==2 when i is not the end) or (nums[i-1]==2 when i is not the start), then do nothing, so simply put a “;”

else return false;

Have a nice weekend!

Gregor UlmPost authorTry pasting your code again with the

`tag.`

Ryan MemmottI have an interesting solution to either24.

public boolean either24 (int[] nums) {

String str=Arrays.toString (nums);

return str.indexOf (“2, 2”)!=-1^str.indexOf (“4, 4”)!=-1;

}

probably a background intensive means of doing it. but still….

Ryan MemmottFor modThree here’s a solution that only does 3 comparisons per loop and only 1 variable added.

public boolean modThree(int[] nums) {

int count=0;

for (int x: nums) {

count=x%2==0? (count> 0? count+1:1):(count 3) return true;

}

return false;

}

Ryan MemmottBasically for line 4 of my code the less then sign which should follow the count in ….count 3) paired with a greater then sign in line 5.

public boolean modThree(int[] nums) {

int count=0;

for (int x: nums) {

count=x%2==0? (count> 0? count+1:1):(0>count? count-1:-1);

if Math.abs (count==3) return true;

}

return false;

}

Ryan Memmotthmm my code got whacked from the html tags… need some help to preserve the raw code.

Gregor UlmPost authorYou can use the “code” tag for that purpose.

Bon RusselHey I just noticed that you guys don’t have a solution for fizzyArray2!?

Gregor UlmPost authorNo, that’s not correct. Just look around some more.

JR KimHey thank you for providing the answer. i really appreciate it! it’s good to take a look at different perspective!

I just had a quick comment on sameEnds function:

for time complexity(to avoid using double for-loops), can’t you do something like:

public boolean sameEnds(int[] nums, int len) {

for(int i=0; i<len; i++) {

if(nums[i]!=nums[i+nums.length-len]) {

return false;

}

}

return true;

}

let me know what you think! thanks!

RyanHello. Thank you for all of these explanations and solutions.

Your Twotwo solution has two loops in it, the requirements on the CodingBat site under the Array-2 section state to use only one loop though..

Gregor UlmPost authorThere is a solution with one loop in the comment section.

WilliamHey, found a much simpler one for isEverywhere, thought I’d share it ðŸ™‚

//basically checks every pair of numbers and sees if one is val

//since you’re checking in pairs and if you search to nums.length-1, no out of bounds and it covers everything

public boolean isEverywhere(int[] nums, int val) {

for(int i=0;i<nums.length-1;i++){

if(!(nums[i]==val||nums[i+1]==val))return false;

}

return true;

}

RichardThis is the approach I used, which is slightly easier to follow and uses the same logic:

public boolean isEverywhere(int[] nums, int val) {

for(int i = 0; i < nums.length – 1; i++){

if(nums[i] != val && nums[i+1] != val) return false;

}

return true;

}

JeffGregor’s solution for the isEverywhere problem may be flawed. Just one test case that doesn’t work with Gregor’s code:

[1, 2, 1, 2, 1, 1, 3, 1], 1

Gregor UlmPost authorYou are right. William as well as Richard provided a cleaner solution already, though.

BartoMy approach for “either24” was a bit different. I parsed int[] array to String and check if it contains 2s or 4s.

public boolean either24(int[] nums)

{

if( Arrays.toString( nums ).contains( “2, 2” ) &&

!Arrays.toString( nums ).contains( “4, 4” ) )

return true;

if( Arrays.toString( nums ).contains( “4, 4” ) &&

!Arrays.toString( nums ).contains( “2, 2” ) )

return true;

return false;

}

Wazim KarimBeginner’s Solution to sameEnds ðŸ™‚

Wazim KarimYour solution to has12 is nicely thought out! Here’s mine. This is just my initial approach after reading the problem. I program just for fun, btw. Total newbie.

RickygFor has 77, I have done with one FOR statement but with short-circuit AND’s.

public boolean has77(int[] nums) {

for (int i=0; i<nums.length-1; i++){

if (nums[i]==7 && nums[i+1]==7) return true;

if (nums[i]==7 && i <nums.length-2 && nums[i+2]==7) return true;

}

return false;

}