Java > Array-2 >either24 (CodingBat Solution)

Problem:

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.

either24({1, 2, 2}) → true
either24({4, 4, 1}) → true
either24({4, 4, 1, 2, 2}) → false


Solution:

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


18 comments :

  1. 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);
    }

    ReplyDelete
    Replies
    1. its great developers like you who help Jr. developers like myself think out side the box thanks for your answer i'm learning so much by looking at other developers code i really appreciate it.

      Delete
    2. what is the function of return(twos^fours)?

      Delete
    3. ^ is the XOR operator in java. It simply checks that the 2 values are different.

      In this case, if the twos and fours booleans are the same (both true or both false) it will return true, but if they are different (one is true and the other false) it will return true.

      Delete
    4. It's great to see XOR operation minimizing whole bunch of code. Thank you!

      Delete
  2. public boolean either24(int[] nums) {
    int next2 = 0;
    int next4 = 0;

    for(int i = 0; i < nums.length - 1; i++){
    if(nums[i] == 2 && nums[i+1] == 2){
    next2++;
    }
    if(nums[i] == 4 && nums[i+1] == 4){
    next4++;
    }
    }

    return !(next2 == next4);
    }

    ReplyDelete
  3. Hi Guys - how to explain return (twos ^ fours) in words?

    ReplyDelete
  4. boolean two=false;
    boolean four=false;
    if(nums.length>1){
    for(int i=0;i<nums.length-1;i++){
    if(nums[i]==2&&nums[i+1]==2) two=true;
    if(nums[i]==4&&nums[i+1]==4) four=true;
    }}else return false;
    if(two==true&&four==true)return false;
    else return (two||four);

    ReplyDelete
  5. public boolean either24(int[] nums) {
    int j = 0;

    boolean result = false;

    for(int i = 1; i<nums.length; i++){
    if(nums[j] == 2 && nums[i] == 2) {
    if(result){
    return false;
    } else {
    result = true;
    }
    }
    if(nums[j] == 4 && nums[i] == 4) {
    if(result) {
    return false;
    } else {
    result = true;
    }
    }
    j++;
    }
    return result;
    }

    ReplyDelete
  6. public boolean either24(int[] nums) {
    int two = 0;
    int four = 0;

    for(int i = 0; i < nums.length - 1; i++){
    if(nums[i] == 2 && nums[i+1] == 2){
    two++;
    }
    if(nums[i] == 4 && nums[i+1] == 4){
    four++;
    }
    }

    if(two != 0 && four != 0){
    return false;
    } else if(two != 0 || four != 0){
    return true;
    } else {
    return false;
    }

    }

    ReplyDelete
  7. public boolean either24(int[] nums) {
    boolean found2 = false, found4 = false;
    int i = 0;
    while (i + 1 < nums.length && !(found2 && found4)) {
    if (nums[i] == 2 && nums[i+1] == 2) {
    found2 = true;
    } else if (nums[i] == 4 && nums[i+1] == 4) {
    found4 = true;
    }
    i++;
    }
    return !found2 && !found4 ? false: !(found2 && found4);
    }

    ReplyDelete
  8. public boolean either24(int[] nums) {
    boolean either = false;
    int count4 = 0;
    int count2 = 0;
    for(int i=0; i1 && count4<2) || (count4>1 && count2<2)){
    either = true;
    }

    return either;
    }

    ReplyDelete
  9. public boolean either24(int[] nums) {
    boolean either = false;
    int count4 = 0;
    int count2 = 0;
    for(int i=0; i1 && count4<2) || (count4>1 && count2<2)){
    either = true;
    }

    return either;

    }

    ReplyDelete
  10. Anyone know the reason why it paste incomplete code? My code wasn`t like that, but somehow there is a problem with copy-paste. And I can`t delete my comment

    ReplyDelete
  11. public boolean either24(int[] nums) {
    int count = 0;
    for(int i = 0; i < nums.length - 1; i++) {
    if((nums[i] == 2 && nums[i+1] == 2)
    || (nums[i] == 4 && nums[i+1] == 4)) {
    count++;
    }
    }
    return count == 1;
    }

    ReplyDelete
  12. public boolean either24(int[] nums)
    {
    HashMap map = new HashMap<>();

    for(int i = 1; i < nums.length; i++)
    {
    if(nums[i-1] == 4 && nums[i] == 4) map.put(nums[i],nums[i]);
    if(nums[i-1] == 2 && nums[i] == 2) map.put(nums[i],nums[i]);
    }

    if(map.containsKey(4) && map.containsKey(2)) return false;
    if(!map.containsKey(4) && !map.containsKey(2)) return false;
    else return true;
    }

    ReplyDelete
  13. public boolean either24(int[] nums) {
    int two = 0;
    int four = 0;
    for (int i = 0; i < nums.length - 1; i++) {
    if (nums[i] == 2 && nums[i + 1] == 2)
    two++;
    if (nums[i] == 4 && nums[i + 1] == 4)
    four++;
    }

    return ((two>0 && four==0)|| (two==0 && four>0));
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact