Java > Array-1 > no23 (CodingBat Solution)

Problem:

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

no23({4, 5}) → true
no23({4, 2}) → false
no23({3, 5}) → false


Solution:

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


4 comments :

  1. Needs no more then one line of code

    ReplyDelete
  2. public boolean no23(int[] nums) {
    return (nums[0]!=2 && nums[0]!=3 && nums[1]!=2 && nums[1]!=3);
    }

    ReplyDelete
  3. public boolean no23(int[] nums) {
    return !(nums[0]== 2 || nums[0] == 3 || nums[1]== 2 || nums[1] == 3);
    }

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

    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