Solved

# only14 challenge

Posted on 2016-08-03
88 Views
Hi,

I am working on below challenge

http://codingbat.com/prob/p162010

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

I wrote my code as below

``````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;
}
``````

I am not passing all tests
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

0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 11
• 7

LVL 4

Expert Comment

ID: 41741699
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
0

LVL 7

Author Comment

ID: 41741703
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
'

above is description of the challenge right

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

LVL 4

Expert Comment

ID: 41741708
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)
0

LVL 7

Author Comment

ID: 41741709
0

LVL 7

Author Comment

ID: 41741713
``````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;
}
``````

i wonder why i am failing still some test cases

Expected      Run
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
0

LVL 4

Expert Comment

ID: 41741717
Your original code will work if you change the second line as shown below and delete lines 5 and 6.

``````boolean result=true;
``````

Your second code will work if you change line 4 as shown below and delete line 9.

``````    if(nums[i]!=1&&nums[i]!=4){
``````
0

LVL 7

Author Comment

ID: 41742499
``````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;
}
``````

as above second approach passed all tests
if every element is a 1 or a 4.

as underline above challenge says OR right and then why when i use || it won't work but surprisingly works with &&.

Any tips on when to use || and when to use && (as here it seems paradox to me to use short cut AND when challenge gave OR
0

LVL 4

Expert Comment

ID: 41743133
``````if(nums[i]!=1||nums[i]!=4){
``````

above condition will return true for any given number.

#      nums[i]!=1      nums[i]!=4      nums[i]!=1||nums[i]!=4
1      F                        T                        T
2      T                        T                        T
3      T                        T                        T
4      T                        F                        T
5      T                        T                        T

The two codes below are equivalent. If the first code below seems paradoxical you may use the second code which uses "||".

``````if(nums[i]!=1&&nums[i]!=4){
``````
``````if(!(nums[i]==1||nums[i]==4)){
``````
0

LVL 7

Author Comment

ID: 41743213
``````if(nums[i]!=1&&nums[i]!=4){

if(!(nums[i]==1||nums[i]==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?
0

LVL 4

Expert Comment

ID: 41743252
0

LVL 32

Expert Comment

ID: 41744773
gudii9, not sure what happened but codingbat link is showing me countEvens challenge and not only14 challenge?
0

LVL 7

Author Comment

ID: 41745096
``````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;
}
``````

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
0

LVL 7

Author Comment

ID: 41745101
``````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;
}
``````

what is meaning of empty else implementation as below in case of second approach?

else {
//result=true;

}

what happens inside else as no code is there in that?? please advise
0

LVL 7

Author Comment

ID: 41745104
``````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;
}
``````

i was more clear on above which passes all tests.

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??)?
0

LVL 4

Accepted Solution

Hammadh Abdul Rahman earned 500 total points
ID: 41745138
If there is no code in the bracket after the if statement, then nothing happens inside the if statement. Same for else statement.

``````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. 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.
0

LVL 7

Author Comment

ID: 41745635
If the number is neither 1 nor 4 we can be sure the statement "contains only 1 and 4" is false.

when you say statement you mena if statement or else statement or the array statement like {1,4,2,4}

0

LVL 7

Author Comment

ID: 41745641
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?
0

LVL 7

Author Comment

ID: 41745644
``````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.
``````
0

LVL 4

Expert Comment

ID: 41745739
I think a truth table is the best tool with this kind of problems.

It is a diagram in rows and columns showing how the truth or falsity of a proposition varies with that of its components.

x      condition
1      true/false/Can't Determine
2      true/false/Can't Determine
...      true/false/Can't Determine

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Six Sigma Control Plans
###### Suggested Courses
Course of the Month8 days, 6 hours left to enroll