Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.
public boolean no14(int[] nums) {
boolean result=false;
for(int i=0;i<nums.length;i++){
if(nums[i]!=1||nums[i]!=4){
result=true;
return result;
}
}
return result;
}
Expected Run
no14([1, 2, 3]) → true true OK
no14([1, 2, 3, 4]) → false true X
no14([2, 3, 4]) → true true OK
no14([1, 1, 4, 4]) → false true X
no14([2, 2, 4, 4]) → true true OK
no14([2, 3, 4, 1]) → false true X
no14([2, 1, 1]) → true true OK
no14([1, 4]) → false true X
no14([2]) → true true OK
no14([2, 1]) → true true OK
no14([1]) → true true OK
no14([4]) → true true OK
no14([]) → true false X
no14([1, 1, 1, 1]) → true true OK
no14([9, 4, 4, 1]) → false true X
no14([4, 2, 3, 1]) → false true X
no14([4, 2, 3, 5]) → true true OK
no14([4, 4, 2]) → true true OK
no14([1, 4, 4]) → false true X
other tests
X
Correct for more than half the tests
if(nums[i]!=1||nums[i]!=4){
public boolean no14(int[] nums) {
boolean no1=true, no4=true;
for(int i=0;i<nums.length;i++){
if(nums[i]==1)
no1=false; // it contains 1's, so it contains no 1's is false
else if(nums[i]==4)
no4=false; // it contains 4's, so it contains no 4's is false
}
return no1||no4; // return true if it contains no 1's or it contains no 4's.
}
Having return statement within the loop will return the result before checking all the numbers.
public boolean no14(int[] nums) {
boolean result=false;
for(int i=0;i<nums.length;i++){
if(nums[i]!=1||nums[i]!=4){
result=true;
//return result;
}
}
return result;
}
even putting return outside fails tests. i wonder why?Expected Run
no14([1, 2, 3]) → true true OK
no14([1, 2, 3, 4]) → false true X
no14([2, 3, 4]) → true true OK
no14([1, 1, 4, 4]) → false true X
no14([2, 2, 4, 4]) → true true OK
no14([2, 3, 4, 1]) → false true X
no14([2, 1, 1]) → true true OK
no14([1, 4]) → false true X
no14([2]) → true true OK
no14([2, 1]) → true true OK
no14([1]) → true true OK
no14([4]) → true true OK
no14([]) → true false X
no14([1, 1, 1, 1]) → true true OK
no14([9, 4, 4, 1]) → false true X
no14([4, 2, 3, 1]) → false true X
no14([4, 2, 3, 5]) → true true OK
no14([4, 4, 2]) → true true OK
no14([1, 4, 4]) → false true X
other tests
X
if(nums[i]==1)
no1=false; // it contains 1's, so it contains no 1's is false
else if(nums[i]==4)
no4=false; // it contains 4's, so it contains no 4's is false
}
if(nums[i]!=1||nums[i]!=4){
result=true;
//return result;
}
i thought both same?
return Arrays.toString(nums).indexOf("1")<0||Arrays.toString(nums).indexOf("4")<0;
above also passed all tests
if(nums[i]==1)
no1=false; // it contains 1's, so it contains no 1's is false
else if(nums[i]==4)
no4=false; // it contains 4's, so it contains no 4's is false
}
if(nums[i]!=1||nums[i]!=4){
result=true;
//return result;
}
if(nums[i]!=1||nums[i]!=4){
result=true;
//return result;
}
if(nums[i]==1)
no1=false; // it contains 1's, so it contains no 1's is false
else if(nums[i]==4)
no4=false; // it contains 4's, so it contains no 4's is false
}
boolean b = true;
for(int i : nums){if((i==1)){b = false;}}
if(b==true)return b;
b = true;
for(int i : nums){if((i==4)){b = false;}}
return b;
//how to decide to take as two separate variables???
//how to decide to take as two separate variables???
because the question asks you to return true if EITHER 1 or 4 is not found. So a test like 2,3,4 should return true, because there are no 1s in it. A test like 2,3,1 would return true because . . . well, you tell us.
So you need to keep track of their existence *separately*.
//how to decide to take as two separate variables???
because the question asks you to return true if EITHER 1 or 4 is not found. So a test like 2,3,4 should return true, because there are no 1s in it. A test like 2,3,1 would return true because ...there are no 4s
So you need to keep track of their existence *separately*.
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
null output | 3 | 35 | |
How to concatenate fields in zpl, and how to use conditional statement? | 3 | 49 | |
Object Oriented Programming, C#, referencing, scoping. | 13 | 69 | |
sql import cannot be resolved jsp | 3 | 47 |
Join the community of 500,000 technology professionals and ask your questions.