# maxMod5 challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p115384

I tried my code as below
``````public int maxMod5(int a, int b) {
if(a>b){
return a;
}
else{

if( (a==b)){

return 0;
}

else if( (b%5==a%5)){

return 0;
}

}
return 1;

}
``````

I am getting below result
Expected      Run
maxMod5(2, 3) → 3      1      X
maxMod5(6, 2) → 6      6      OK
maxMod5(3, 2) → 3      3      OK
maxMod5(8, 12) → 12      1      X
maxMod5(7, 12) → 7      0      X
maxMod5(11, 6) → 6      11      X
maxMod5(2, 7) → 2      0      X
maxMod5(7, 7) → 0      0      OK
maxMod5(9, 1) → 9      9      OK
maxMod5(9, 14) → 9      0      X
maxMod5(1, 2) → 2      1      X
other tests
OK

how to  improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
Java EEJavaProgramming Languages-OtherProgramming

Last Comment
gudii9
krakatoa

`````` return a==b?0:(a%5)==(b%5)?Math.min(a,b):Math.max(a,b);
``````
gudii9

can i write else if without condition similar to else?
krakatoa

Apart from Venn diagrams, I think you'll eventually need to grasp narratively-presented logic as in the present challenge.

"Given two int values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the return the smaller value. However, in all cases, if the two values are the same, return 0."

So deal with the clear instruction "However, in all cases, if the two values are the same, return 0." first. This gives you 'a==b?0;' Now you have two other conditions to assess. You have to deal with the mod (remainder) case first, because comparing the two ints  just based on their size at this stage, is not good enough, as the two ints *could* be something like 15 and 500, both of which mod by 5, leaving 0 remainder in both cases.  So you go 'a%5 == b%5 ?Math.min(a,b)' and you have the second part of your return statement. That just leaves the clean-up comparison, which is a simple size issue -> return the largest of a and b.
krakatoa

``````if(a==b){
return 0;

}

else if(a%5==b%5){
return Math.min(a,b);
}

return Math.max(a,b);
``````
gudii9

``````if(a==b){
return 0;

}

else if(a%5==b%5){
return Math.min(a,b);
}

return Math.max(a,b);
``````

above worked perfect?

My question is which condition to put in top if block
and which condition to put in middle else if block
and which condition to put in bottom else block.

when i interachage like below if with else if condition it failed one

``````public int maxMod5(int a, int b) {

if(a%5==b%5){

return Math.min(a,b);
}
else  if( (a==b)){

return 0;
}

else {

return Math.max(a,b);

}

}

``````

krakatoa

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
gudii9

``````public int maxMod5(int a, int b) {

if(a%5==b%5){

return Math.min(a,b);
}
else  if( (a==b)){

return 0;
}

else {

return Math.max(a,b);

}

}

``````

why above failed one test as below when i interchange if and else if conditions as above?

Expected      Run
maxMod5(2, 3) → 3      3      OK
maxMod5(6, 2) → 6      6      OK
maxMod5(3, 2) → 3      3      OK
maxMod5(8, 12) → 12      12      OK
maxMod5(7, 12) → 7      7      OK
maxMod5(11, 6) → 6      6      OK
maxMod5(2, 7) → 2      2      OK
maxMod5(7, 7) → 0      7      X
maxMod5(9, 1) → 9      9      OK
maxMod5(9, 14) → 9      9      OK
maxMod5(1, 2) → 2      2      OK
other tests
X
Correct for more than half the tests
krakatoa

What you need to think about is when the ints are the same and you run the mod test first.
gudii9

``````What you need to think about is when the ints are the same and you run the mod test first.
``````

but we are doing reverse right as below

``````public int maxMod5(int a, int b) {

if(a==b){return 0;}

if(a%5==b%5){return Math.min(a,b);}

else {return Math.max(a,b);

}

}
``````

as above we are instead doing as below

What you need to think about is when the ints are the same and you run the mod test second or next or later??

krakatoa

Ask yourself what happens when a==10 and say b==20.
gudii9

Ask yourself what happens when a==10 and say b==20.

mods same as % gives 0 and 0 which are equal

first if skipped goes to second one i.e else if
as below
``````if(a==b){
return 0;

}

else if(a%5==b%5){
return Math.min(a,b);
}
``````
gudii9

However, in all cases,
in the challenge i have to read above more clearly.

If it says however in all cases means it comes at top if loop right? (not in else if /else)
gudii9

public int maxMod5(int a, int b) {

if(a%5==b%5){

return Math.min(a,b);
}
else  if( (a==b)){

return 0;
}

else {

return Math.max(a,b);

}

}

if code as above i see why below is failing

maxMod5(7, 7) → 0      7      X
as the % is 2 and other % also 2 so goes to first above if loop prints min of 7, 7 which is 7.

But challenge says there is a deviation by saying 'However in all cases'
which means this one is like overriding all other instructions and comes as high priority number one thing(not two, three) to take care.

krakatoa

Yes, understanding the wording is important.

But it looks like you can see the reason now.
gudii9

yes kind of 90%
krakatoa

So you must then know the 10% that you still don't know . . . so what is that last 10% ?
gudii9

understanding the wording is important.

which comes first which comes second which comes third and last and also i am sometimes confusing between || and && which makes up rest of 10% i feel
krakatoa

"Given two int values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the [sic] return the smaller value. However, in all cases, if the two values are the same, return 0. "

Is the Question.

Here is another way of writing that :

Given two int values, return whichever value is larger, BUT if the two values have the same remainder when divided by 5, then return the smaller value, EXCEPT in all cases where the two values are the same, in which case return 0.

So you need to look at the particular cases first, (edges) and then after that you can deal with the remaining cases, which cover everything else - and "everything else" in this question means : "the larger of the two ints". So cases that should return "0" are the most particular, so deal with them first, then the mod cases, and then, as I said above.
gudii9

However means BUT and EXCEPT
identifying the edges or edge case important.

So cases that should return "0" are the most particular, so deal with them first

how do i conclude some sentence(out of given 3 sentences) is most particular than other?
gudii9

i am getting after trying 5-10 times that some sentence is most particular? But not first time or just before start attacking the challenge?
krakatoa

how do i conclude some sentence(out o . . . . .

"
When it rains, I don't go out at all, HOWEVER I do still go to the end of the street if it's a Wednesday, HOWEVER, if it's after 3 o'clock, then I just go into the garden.
"
gudii9

HOWEVER, if it's after 3 o'clock

above is most particular...no matter rain or which day of week you go out i.e to garden.

Is my understanding is correct?
krakatoa

I thought you might list the tests, in the order they need to be made, to decide what action to take.
krakatoa

Is my understanding is correct?

Yes.

-------------------------

I'm not saying that was the best or even a very good example, but you should look at each "clause" in a problem, and see how it impacts the other clauses or terms in the statement, and build the logic based on that. As you touched on in earlier questions, perhaps sometimes a Venn diagram could help, but that would be your personal preference to use or not. I would personally prefer a flow chart approach, but then again, I don't know what the production experts here would say to that these days.

In short, just think things through, several times over, and usually deal with special cases  before anything else.
gudii9

I too like flow chart than anything else
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts

TRUSTED BY