Problem:
Given an array of ints, return true if every element is a 1 or a 4.
only14({1, 4, 1, 4}) → true
only14({1, 4, 2, 4}) → false
only14({1, 1}) → true
Solution:
public boolean only14(int[] nums) { boolean isTrue = true; for (int i = 0; i < nums.length; i++) { if (nums[i] != 1 && nums[i] != 4) isTrue = false; } return isTrue; }
public boolean only14(int[] nums) {
ReplyDeleteboolean isTrue = true;
for(int i = 0; i < nums.length; i++)
{
if(nums[i] != 1 && nums[i] !=4)
{
isTrue = false;
}
}
return isTrue;
}
public boolean only14(int[] nums) {
ReplyDeletefor (int i=0; i<nums.length; i++) {
if(nums[i] != 4 && nums[i] != 1) {
return false;
}
}
return true;
}
public boolean only14(int[] nums) {
ReplyDeleteboolean bool = false;
int amount = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 1 || nums[i] == 4) {
amount++;
}
}
if(amount == nums.length){
return true;
} else {
return false;
}
}
public boolean only14(int[] nums) {
ReplyDeletefor(final int num: nums){
if(num != 1 && num != 4){
return false;
}
}
return true;
}
public boolean more14(int[] nums) {
ReplyDeleteint one = 0;
int four = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) one++;
if (nums[i] == 4) four++;
}
return one > four ? true : false;
}
for(int i = 0; i < nums.length; i++)
ReplyDelete{
if(nums[i] != 1 && nums[i] != 4) return false;
}
return true;
Array-2 > only14
ReplyDeletepublic boolean only14(int[] nums) {
int c = 0;
for(int i =0; i<nums.length;i++){
if(nums[i]==1)
c++;
else if(nums[i]==4)
c++;}
return (c == nums.length);
}