Java > Array-2 > has12 (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; }

No comments:

Post a Comment