Psedo code description of approach :
1. loop through array.
2. check if each individual element is either 1 or 4
3. if step2 is yes return true
4. if step2 no return 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.

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

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

why we have to take carollary backward approach rather than direct forward approach( which seems not working strangely)

0

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Because the current approach is returning true if even one number is 1 or 4.

So let's try dry running the first Pseudo code with numbers 1 and 2.
It will loop through the numbers first taking 1.
It will check whether 1is either 1 or 4.
Since the above is true it will return true.

So it is returning true without checking number 2. This is why the Pseudo code doesn't work.

Now let's try dry running the second Pseudo code with the same numbers 1 and 2.

It will loop through the numbers first taking number 1.
It will check whether 1 is neither 1 nor 4.
Since the above is false it will just move onto the next number which is 2.
It will now check whether 2 is neither 1 nor 4.
Since the above is true it will return false. (Since 2 is neither 1 nor 4)

to bring ! outside of() you ave rto change || to && inside ()?
Bit strange to me . Any good books, links, resources to understand these kind of logic parts more deeper?
may be some venn diagram?

first approach also worked as you mentioned. what is meaning of if loop with empty implementation in it?
i see both line commented then magically all test cases are passing? please advise
boolean algebra link is good. i am reading that now

so inside if it is always better to use condition which you know 100%(as above latest && approach in if condition) is true or false rather than putting some condition which is kind of shaky(like my original approach??)?

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.

are we not supposed to check all numbers to say statement "contains only 1 and 4" is false.??

you mean checking one number is suffice If the number is neither 1 nor 4 .

I am having some confusion still here?
may be some diagram will help me?

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.

Draw a truth table like the one above, showing what you can determine given that you know the current number is x and don't know the rest of the numbers. Then you can use the truth table to help you write the IF condition. Your IF statement should trigger for the x values that have either "true" or "false" in the second column of the table.

You could also write stuff like x<10 in the x column, and write what you can determine if x is less than 10.

0

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Open in new window

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.