Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.
public boolean only14(int[] nums) {
boolean result=false;
for(int i=0; i<nums.length;i++){
if(nums[i]==1||nums[i]==4){
result=true;
return result;
}
else {
result=false;
}
}
return result;
}
Expected Run
only14([1, 4, 1, 4]) → true true OK
only14([1, 4, 2, 4]) → false true X
only14([1, 1]) → true true OK
only14([4, 1]) → true true OK
only14([2]) → false false OK
only14([]) → true false X
only14([1, 4, 1, 3]) → false true X
only14([3, 1, 3]) → false true X
only14([1]) → true true OK
only14([4]) → true true OK
only14([3, 4]) → false true X
only14([1, 3, 4]) → false true X
only14([1, 1, 1]) → true true OK
only14([1, 1, 1, 5]) → false true X
only14([4, 1, 4, 1]) → true true OK
other tests
X
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]) → tru
Psedo code should be like this :
1. loop through array.
2. check if each individual element is neither 1 nor 4
3. if step2 is yes, return false
4. After checking all elements, return true
public boolean only14(int[] nums) {
boolean result=true;
for(int i=0; i<nums.length;i++){
if(nums[i]!=1||nums[i]!=4){
result=false;
return result;
}
else {
result=true;
}
}
return result;
}
Expected Runplease advise
only14([1, 4, 1, 4]) → true false X
only14([1, 4, 2, 4]) → false false OK
only14([1, 1]) → true false X
only14([4, 1]) → true false X
only14([2]) → false false OK
only14([]) → true true OK
only14([1, 4, 1, 3]) → false false OK
only14([3, 1, 3]) → false false OK
only14([1]) → true false X
only14([4]) → true false X
only14([3, 4]) → false false OK
only14([1, 3, 4]) → false false OK
only14([1, 1, 1]) → true false X
only14([1, 1, 1, 5]) → false false OK
only14([4, 1, 4, 1]) → true false X
other tests
X
boolean result=true;
if(nums[i]!=1&&nums[i]!=4){
public boolean only14(int[] nums) {
boolean result=true;
for(int i=0; i<nums.length;i++){
// if(nums[i]!=1||nums[i]!=4){
if(nums[i]!=1&&nums[i]!=4){
result=false;
return result;
}
else {
//result=true;
}
}
return result;
}
if every element is a 1 or a 4.
if(nums[i]!=1||nums[i]!=4){
if(nums[i]!=1&&nums[i]!=4){
if(!(nums[i]==1||nums[i]==4)){
if(nums[i]!=1&&nums[i]!=4){
if(!(nums[i]==1||nums[i]==4)){
to bring ! outside of() you ave rto change || to && inside ()?public boolean only14(int[] nums) {
boolean result=true;
for(int i=0; i<nums.length;i++){
if(nums[i]==1||nums[i]==4){
//result=true;
// return result;
}
else {
result=false;
}
}
return result;
}
public boolean only14(int[] nums) {
boolean result=true;
for(int i=0; i<nums.length;i++){
// if(nums[i]!=1||nums[i]!=4){
if(nums[i]!=1&&nums[i]!=4){
result=false;
return result;
}
else {
//result=true;
}
}
return result;
}
public boolean only14(int[] nums) {
boolean result=true;
for(int i=0; i<nums.length;i++){
// if(nums[i]==1||nums[i]==4){
if(nums[i]!=1&&nums[i]!=4){
result=false;
return result;
}
else {
result=true;
}
}
return result;
}
public boolean only14(int[] nums) {
boolean result=true;
for(int i=0; i<nums.length;i++){
// if(nums[i]==1||nums[i]==4){
if(nums[i]!=1&&nums[i]!=4){
result=false;
return result;
}
else {
result=true;
}
}
return result;
}
If the number is neither 1 nor 4 we can be sure the statement "contains only 1 and 4" is false.
If the number is neither 1 nor 4 we can be sure the statement "contains only 1 and 4" is false. This is why the if condition has to be as in the code above. But otherwise (if number is either 1 or 4), we can't be sure whether the statement "contains only 1 and 4" is true or false without checking the rest of the numbers. And this is why the else statement is unnecessary.
I think I got it
for(int i=0; i<nums.length;i++){
// if(nums[i]==1||nums[i]==4){
if(nums[i]!=1&&nums[i]!=4){
lets say given array is {3,2,7,1}
so index 0 is 3 which is neither 1 nor 4 hence we can safely say array has only 1 or 4 as elements is false
where as if I write as below
for(int i=0; i<nums.length;i++){
if(nums[i]==1||nums[i]==4){
given array is {1,3,2,4}
index 0 is element 1 so if(nums[i]==1||nums[i]==4)===>true but we cannot stop here and say array has only 1 or 4 as elements as we are forced to check other elements as we well like 3, 2, 4 etc.
any kind of pictures or flow diagrams etc we can draw to understand these coding challenges easily.
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.