Java > Array-2 > no14 (CodingBat Solution)

Problem:

Given an array of ints, return true if it contains no 1's or it contains no 4's.

no14({1, 2, 3}) → true
no14({1, 2, 3, 4}) → false
no14({2, 3, 4}) → true


Solution:

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


20 comments :

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

    ReplyDelete
  2. public boolean no14(int[] nums) {
    boolean ones=false;
    boolean fours=false;
    for(int s: nums){
    if(s == 1)
    ones = true;
    if(s == 4)
    fours = true;
    }

    if(ones && fours)
    return false;
    else
    return true;
    }

    ReplyDelete
  3. public boolean no14(int[] nums) {
    boolean res = true;
    for (int i = 0; i < nums.length; i++) {
    for (int j = 0; j < nums.length; j++) {
    if (nums[i] == 1 && nums[j] == 4)
    res = false;
    }
    }
    return res;
    }

    ReplyDelete
  4. public boolean no14(int[] nums) {

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

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

    }

    }
    return true;
    }

    ReplyDelete
  5. public boolean no14(int[] nums) {
    return !Arrays.toString(nums).contains("1") || !Arrays.toString(nums).contains("4") ;
    }

    ReplyDelete
  6. public boolean no14(int[] nums) {
    return !Arrays.toString(nums).contains("1") || !Arrays.toString(nums).contains("4") ;
    }

    ReplyDelete
  7. This is my solution :
    public boolean no14(int[] nums) {
    boolean a = false;
    boolean b=false;
    for(int i=0;i<nums.length;i++){
    if(nums[i]==1) a=true;
    if(nums[i]==4) b=true;



    }

    if(a && b) return false;
    return true;
    }

    ReplyDelete
  8. public boolean no14(int[] nums) {
    boolean ones = false ;
    boolean fours = false ;

    for ( int i = 0 ; i < nums.length ; i++ ) {
    if(nums[i]==1)
    ones=true;

    if(nums[i]==4)
    fours=true;
    }

    if ( fours && ones )
    return false;
    return true;
    }

    ReplyDelete
  9. public boolean no14(int[] nums) {
    boolean one = false, four = false;
    for(int i=0; i<nums.length; i++){
    if(nums[i]==1)
    one = true;
    if(nums[i]==4)
    four = true;
    }
    if(one==true&&four==true)
    return false;
    return true;
    }

    ReplyDelete
  10. public boolean no14(int[] nums) {
    int four = 0;
    int one = 0;
    for (int n : nums) {
    if (n == 1) one++;
    else if (n==4) four++;
    }
    if (four==0 && one==0) return true;
    return !(one>0 && four>0);
    }

    ReplyDelete
    Replies
    1. I did exactly like this

      public boolean no14(int[] nums) {
      int oneCount = 0;
      int fourCount = 0;
      for (int i = 0; i < nums.length; i++) {
      if (nums[i] == 1) {
      oneCount++;
      }
      if (nums[i] == 4) {
      fourCount++;
      }
      }
      return !(oneCount > 0 && fourCount > 0);
      }

      Delete
  11. public boolean no14(int[] nums) {
    int count1=0;
    int count4=0;
    for(int i=0 ; i0 &&count4>0)return false;
    return true;
    }

    ReplyDelete
  12. public boolean no14(int[] nums) {
    boolean one=false;
    boolean two=false;
    for(int i=0;i<nums.length;i++){
    if(nums[i]==1)
    one=true;
    if(nums[i]==4)
    two=true;
    if(one==true && two==true){
    return false;
    }
    }return true;
    }

    ReplyDelete
  13. int count1 =0;
    int count4 =0;

    for(int i =0;i= 1 && count4 >=1)
    return false;
    else
    return true;

    ReplyDelete
  14. public boolean no14(int[] nums) {

    boolean one = false;
    boolean four = false;

    for (int n: nums) {
    if (n == 1) {
    one = true;
    }
    if (n == 4) {
    four = true;
    }
    }

    if(one ^ four) {
    return true;
    }
    if (!(one && four)) {
    return true;
    }

    return false;
    }

    ReplyDelete
  15. public boolean no14(int[] nums) {
    int a=0;
    int b=0;
    for(int i:nums){if (i==1) a++;}
    for(int j:nums){if (j==4) b++;}
    if (a*b>0) return false;
    return true;
    }

    ReplyDelete
  16. public boolean no14(int[] nums) {

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

    return true;
    }

    ReplyDelete
  17. With Java Stream

    public boolean no14(int[] nums) {
    List list = Arrays.stream(nums).boxed().collect(Collectors.toList());
    return !list.contains(1) || !list.contains(4);
    }

    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