# lastDigit challenge

Hi,

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

I tried my code as below

``````public boolean lastDigit(int a, int b, int c) {

if( ((a%10==2)==(b%10==2))&&((b%10==2)==(c%10==2)) )
return true;

else
return false;

}
``````

I am getting below result

Expected      Run
lastDigit(23, 19, 13) → true      true      OK
lastDigit(23, 19, 12) → false      false      OK
lastDigit(23, 19, 3) → true      true      OK
lastDigit(23, 19, 39) → true      true      OK
lastDigit(1, 2, 3) → false      false      OK
lastDigit(1, 1, 2) → true      false      X
lastDigit(1, 2, 2) → true      false      X
lastDigit(14, 25, 43) → false      true      X
lastDigit(14, 25, 45) → true      true      OK
lastDigit(248, 106, 1002) → false      false      OK
lastDigit(248, 106, 1008) → true      true      OK
lastDigit(10, 11, 20) → true      true      OK
lastDigit(0, 11, 0) → true      true      OK
other tests
X

how to  improve my approach and design of this challenge. How do i make a

graphical venn or some other relevant diagram to design it before writing single

line of code to decide best strategy?
LVL 7
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
``````return a%10==b%10||a%10==c%10||b%10==c%10;
``````
0
Commented:
There are 3 possible pair combinations in the three params. You only have to compare the members of each pair, after having removed any powers of 10 they hold, by applying the mod operator % to get rid of everything but the units.
0
Commented:
public boolean lastDigit(int a, int b, int c) {
a%=10; b%=10; c%=10;
return a==b||b==c||c==a;
}

public boolean lastDigit(int a, int b, int c) {
return (a-b)%10==0||(b-c)%10==0||(c-a)%10==0;
}

public boolean lastDigit(int a, int b, int c) {
return (a-b)*(b-c)*(c-a)%10==0;    // could fail if it overflows Integer.MAX_VALUE
}

public boolean lastDigit(int a, int b, int c) {
return (a-b)%10*((b-c)%10)*((c-a)%10)==0;  // avoids overflow
}
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
``````public boolean lastDigit(int a, int b, int c) {

String[] sA = {Integer.toString(a),Integer.toString(b),Integer.toString(c)};
int q=0;
if(sA[q].substring(sA[q].length()-1).equals(sA[q+1].substring(sA[q+1].length()-1))||sA[q].substring(sA[q].length()-1).equals(sA[q+2].substring(sA[q+2].length()-1))||sA[q+1].substring(sA[q+1].length()-1).equals(sA[q+2].substring(sA[q+2].length()-1))){return true;}

return false;

}
``````

:)

Is there anyone out there  . . . . ?
0
Commented:
``````public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();

return al.size()<=2;

}
``````

:)
0
Author Commented:
i will re run and let you know.
0
Commented:
public boolean lastDigit(int a, int b, int c) {
return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
}
0
Author Commented:
return a%10==b%10||a%10==c%10||b%10==c%10;

above i understood other i am still trying to understand
0
Author Commented:
return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
difference then % sum should be 0?

0
Commented:
" contains("0") "

the "0" would be there - in the String - if any of the 3 pairs last digits are the same (after using % to remove their multiples of 10 (if any). So the "0" means there must have been a pair (at least) with 2 same last digits.
0
Commented:
This makes it easier to understand:

``````return (""+(a-b)+(b-c)+(c-a)%10).contains("0");
``````
0
Commented:
string + number is string concatenation after converting number to string
Is it easy to understand that  (""+(a-b)+(b-c)+(c-a)%10).contains("0") would fail if one of the tests was
lastDigit(1,102,3)
0
Commented:
Which two (or more) final digits are the same in " 1, 102, and 3" ??
0
Commented:
none

But penultimate digits are the same
0
Commented:
right. You are right.
0
Commented:
Again, shows up codingbat's shortcomings at least in terms of their "invisible" "other tests". Misleading results.
0
Author Commented:
``````public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();

return al.size()<=2;

}
``````

i was not clear on above?
you are Stringifying a, b,c and doing substring to find last digit an d then adding to al and checking sized less than or equal to 2?

why only 2 why not 3? i was not clear?
0
Author Commented:
string + number is string concatenation after converting number to string

above is clear.
Is it easy to understand that  (""+(a-b)+(b-c)+(c-a)%10).contains("0") would fail if one of the tests was
lastDigit(1,102,3)

above not clear. Why it will fail for 1, 10,23
a-b is 1-102=-101?//no right as it became string cannot perform subtract??but it is in between parenthesis ()??
0
Commented:
lastDigit(1,102,3)
not
1, 10,23
0
Commented:
I'm not sure I can add anything more to this question. In fact, I am sure - I cannot add anything more.
0
Commented:
why only 2 why not 3?
If al.size()==3, it would mean that
Integer.toString(a).substring(Integer.toString(a).length()-1), Integer.toString(b).substring(Integer.toString(b).length()-1), and Integer.toString(c).substring(Integer.toString(c).length()-1)
were all different.
0
Author Commented:
lastDigit(1,102,3)
not
1, 10,23

sorry i mean lastDigit(1,102,3)

i did not understand what you mean by penultimate and why this fail?

``````public class Test10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
lastDigit(1,102,3);
}
public static boolean lastDigit(int a, int b, int c) {
System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
}
}
``````

i got output
is--->false
0
Author Commented:
``````public class Test10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
lastDigit(8,0,9);
}
public static boolean lastDigit(int a, int b, int c) {
System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
}
}
``````

above gave below
is--->8-91
is--->false
0
Author Commented:
why only 2 why not 3?
If al.size()==3, it would mean that
Integer.toString(a).substring(Integer.toString(a).length()-1), Integer.toString(b).substring(Integer.toString(b).length()-1), and Integer.toString(c).substring(Integer.toString(c).length()-1)
were all different.

this is clear

``````import java.util.HashSet;
import java.util.Set;

public class Test10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//lastDigit(0,0,9);
lastDigit(1,11,3);
}
public static boolean lastDigit(int a, int b, int c) {
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");

//public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();

System.out.println("al is "+al.toString());
return al.size()<=2;

}
}
``````

i got
al is [1, 3]
0
Author Commented:
``````import java.util.HashSet;
import java.util.Set;

public class Test10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//lastDigit(0,0,9);
lastDigit(1,11,3);
}
public static boolean lastDigit(int a, int b, int c) {
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");

//public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();

// System.out.println("al is "+al.toString());
return al.size()<=2;

}
}
``````
i wonder above not resulting any boolean value as output?
0
Author Commented:
we are saying atmost 2 not more than 2
0
Commented:
i got output
is--->false
So do you see why (""+(a-b)%10+(b-c)%10+(c-a)%10)) works better than (""+(a-b)+(b-c)+(c-a)%10)?
0
Author Commented:
to summarize we have 7 approaches for this challenge

solution 1:

public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();

return al.size()<=2;

}

solution 2:

public boolean lastDigit(int a, int b, int c) {
return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");
}

solution 3

public boolean lastDigit(int a, int b, int c) {
a%=10; b%=10; c%=10;
return a==b||b==c||c==a;
}

solution 4
public boolean lastDigit(int a, int b, int c) {
return (a-b)%10==0||(b-c)%10==0||(c-a)%10==0;
}

solution 5
public boolean lastDigit(int a, int b, int c) {
return (a-b)*(b-c)*(c-a)%10==0;    // could fail if it overflows Integer.MAX_VALUE
}

solution6

public boolean lastDigit(int a, int b, int c) {
return (a-b)%10*((b-c)%10)*((c-a)%10)==0;  // avoids overflow
}

solution 7
return a%10==b%10||a%10==c%10||b%10==c%10;

So do you see why (""+(a-b)%10+(b-c)%10+(c-a)%10)) works better than (""+(a-b)+(b-c)+(c-a)%10)?

(""+(a-b)%10+(b-c)%10+(c-a)%10))
above means find difference then do % to find last digit on each then add right?
(""+(a-b)+(b-c)+(c-a)%10)?

above means find difference then add to get sum then finally find the last digit on the sum which is not better approach right as it fails for lastDigit(1,102,3) due to penultimate digit??
i am not still clear on this
0
Author Commented:
``````import java.util.HashSet;
import java.util.Set;

public class CopyOfTest10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//lastDigit(0,0,9);
System.out.println("valiue is--->"+lastDigit(1,11,3));
}
public static boolean lastDigit(int a, int b, int c) {
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");

//public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();

System.out.println("al is "+al.toString());
return (al.size()<=2);

}
}
``````
output is
al is [1, 3]
valiue is--->true

i got my answer why boolean not printed as i never printed in calling method. i corrected as above
0
Author Commented:
``````import java.util.HashSet;
import java.util.Set;

public class CopyOfTest10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// lastDigit(0,0,9);
System.out.println("valiue is--->" + lastDigit(1, 102, 3));
}

public static boolean lastDigit(int a, int b, int c) {
System.out.println("is--->"
+ ("" + (a - b) % 10 + (b - c) % 10 + (c - a) % 10));
System.out.println("is--->"
+ ("" + (a - b) % 10 + (b - c) % 10 + (c - a) % 10)
.contains("0"));
return ("" + (a - b) % 10 + (b - c) % 10 + (c - a) % 10).contains("0");

// System.out.println (""+(a-b)+(b-c)+(c-a)%10);
// System.out.println("is--->"+
// (""+(a-b)+(b-c)+(c-a)%10).contains("0"));
// return (""+(a-b)+(b-c)+(c-a)%10).contains("0");

}
}
``````

above gave below output
is--->-192
is--->false
valiue is--->false

``````import java.util.HashSet;
import java.util.Set;

public class CopyOfTest10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// lastDigit(0,0,9);
System.out.println("valiue is--->" + lastDigit(1, 102, 3));
}

public static boolean lastDigit(int a, int b, int c) {
// System.out.println("is--->"
// + ("" + (a - b) % 10 + (b - c) % 10 + (c - a) % 10));
// System.out.println("is--->"
// + ("" + (a - b) % 10 + (b - c) % 10 + (c - a) % 10)
// .contains("0"));
// return ("" + (a - b) % 10 + (b - c) % 10 + (c - a) %
// 10).contains("0");

System.out.println("" + (a - b) + (b - c) + (c - a) % 10);
System.out.println("is--->"
+ ("" + (a - b) + (b - c) + (c - a) % 10).contains("0"));
return ("" + (a - b) + (b - c) + (c - a) % 10).contains("0");

}
}
``````

above gave output as

-101992
is--->true
valiue is--->true

which is wrong it should have given false.
0
Author Commented:
So do you see why (""+(a-b)%10+(b-c)%10+(c-a)%10)) works better than (""+(a-b)+(b-c)+(c-a)%10)?

now i see why it works better.

better to make individual differences then reminder by deviding with 10 and contactinate and check contains 0

" contains("0") "

the "0" would be there - in the String - if any of the 3 pairs last digits are the same (after using % to remove their multiples of 10 (if any). So the "0" means there must have been a pair (at least) with 2 same last digits.

as -192 do not have 0 so false whereas
-101992 has 0 so true ideally should give false
0
Author Commented:
Integer.toString(a).substring(Integer.toString(a).length()-1)

what is meaning of above line.

we are calling toString of wrapper class of int a and doing substring to print last digit??not clear here??

``````import java.util.HashSet;
import java.util.Set;

public class Test10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//lastDigit(0,0,9);
System.out.println("value is---->"+lastDigit(1,11,3));
}
public static boolean lastDigit(int a, int b, int c) {
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");

//public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();
System.out.println("al 1 is--->"+Integer.toString(a).substring(Integer.toString(a).length()-1));
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(a).length()-1));
System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(a).length()-1));

System.out.println("al is "+al.toString());
return (al.size()<=2);

}
}
``````

above cocd gave below output
al 1 is--->1
al 2 is--->11
al 3 is--->3
al is [1, 3]
value is---->true

0
Author Commented:

solution 5
public boolean lastDigit(int a, int b, int c) {
return (a-b)*(b-c)*(c-a)%10==0;    // could fail if it overflows Integer.MAX_VALUE
}

solution6

public boolean lastDigit(int a, int b, int c) {
return (a-b)%10*((b-c)%10)*((c-a)%10)==0;  // avoids overflow
}

wht it means by overflow???
0
Commented:
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(a).length()-1));
System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(a).length()-1))
should be
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(b).length()-1));
System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(c).length()-1))
0
Commented:
public boolean lastDigit(int a, int b, int c) {
return (a-b)*(b-c)*(c-a)%10==0;    // could fail if it overflows Integer.MAX_VALUE
}
2000000000000 does not fit in Integer.MAX_VALUE
so lastDigit(10000,20000,30000) would not work properly
0
Author Commented:
``````package snippet;
import java.util.HashSet;
import java.util.Set;

public class Test10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//lastDigit(0,0,9);
System.out.println("value is---->"+lastDigit(1,11,3));
}
public static boolean lastDigit(int a, int b, int c) {
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");

//public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();
System.out.println("al 1 is--->"+Integer.toString(a).substring(Integer.toString(a).length()-1));
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(b).length()-1));
System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(c).length()-1));

System.out.println("al is "+al.toString());
return (al.size()<=2);

}
}
``````

i fixed as you mentioned and got below output

al 1 is--->1
al 2 is--->1
al 3 is--->3
al is [1, 3]
value is---->true

i wonder why i got al is [1, 3] but not as al is [1,11, 3] which is being passed? please advise
0
Author Commented:
``````Integer.toString(a).substring(Integer.toString(a).length()-1)
``````

what is the complete meaning of above line.
0
Commented:
Integer.toString(b).substring(Integer.toString(b).length()-1)  is the last digit of b
0
Author Commented:
Integer.toString(b).substring(Integer.toString(b).length()-1)  is the last digit of b

to get last digit of b why are we doing all this round about?
Integer.toString(b).substring(Integer.toString(b).length()-1)
what above line means , can you please break it down and explain in simple terms
0
Commented:
krakatoa might have thought you could learn something from it.
I thought that challenge verbose, when a simple %10 is sufficient.
0
Author Commented:
Integer.toString(b).substring(Integer.toString(b).length()-1)

where should i put more ( ) so that it is better readable and understandable and breakable?
0
Commented:
bString=Integer.toString(b);
bLength=bString.length();
bLast=bLength-1;
bString.substring(bLast);

If you lookup the java documentation, you should be able to break apart any expression yourself.
0
Author Commented:
bString.substring(bLast);

how above gives last digit

http://www.tutorialspoint.com/java/java_string_substring.htm

String Str = new String("Welcome to Tutorialspoint.com");

System.out.print("Return Value :" );
System.out.println(Str.substring(10) );

when i pass startIndex 10 i get below output right?

Return Value : Tutorialspoint.com
0
Commented:
So what output do you think you would get from
System.out.println(Str.substring(Str.length()-1) );
0
Author Commented:
if string Str is abc

then Str.length()-1 ===3-1=2

(Str.substring(Str.length()-1) ); is

(Str.substring(2) );

we get c
i think i got it now.

thank you
0
Author Commented:
i wonder why i got al is [1, 3] but not as al is [1,11, 3] which is being passed? please advise

``````package snippet;
import java.util.HashSet;
import java.util.Set;

public class Test10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//lastDigit(0,0,9);
System.out.println("value is---->"+lastDigit(1,11,3));
}
public static boolean lastDigit(int a, int b, int c) {
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");

//public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();
System.out.println("al 1 is--->"+Integer.toString(a).substring(Integer.toString(a).length()-1));
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(b).length()-1));
System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(c).length()-1));

System.out.println("al is "+al.toString());
return (al.size()<=2);

}
}
``````

al 1 is--->1
al 2 is--->1
al 3 is--->3
al is [1, 3]
value is---->true
0
Commented:
al 2 is--->1
0
Author Commented:
``````import java.util.HashSet;
import java.util.Set;

public class Test10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//lastDigit(0,0,9);
System.out.println("value is---->"+lastDigit(1,11,3));
}
public static boolean lastDigit(int a, int b, int c) {
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");

//public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();
System.out.println("al 1 is--->"+Integer.toString(a).substring(Integer.toString(a).length()-1));
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(a).length()-1));
System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(a).length()-1));

System.out.println("al is "+al.toString());
return (al.size()<=2);

}
}
``````
when i ran above code i got below output

al 1 is--->1
al 2 is--->11//why this 11 is missing
al 3 is--->3
al is [1, 3]//why 11 is missing in this line..i expected this line output as --->al is [1,11,3]
value is---->true
0
Commented:
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(a).length()-1));
0
Author Commented:
``````import java.util.HashSet;
import java.util.Set;

public class Test10 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//lastDigit(0,0,9);
System.out.println("value is---->"+lastDigit(1,11,3));
}
public static boolean lastDigit(int a, int b, int c) {
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10));
//System.out.println("is--->"+(""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0"));
//return (""+(a-b)%10+(b-c)%10+(c-a)%10).contains("0");

//public boolean lastDigit(int a, int b, int c) {

Set<String> al = new HashSet<String>();
System.out.println("al 1 is--->"+Integer.toString(a).substring(Integer.toString(a).length()-1));
System.out.println("al 2 is--->"+Integer.toString(b).substring(Integer.toString(b).length()-1));
System.out.println("al 3 is--->"+Integer.toString(c).substring(Integer.toString(a).length()-1));

System.out.println("al is "+al.toString());
return (al.size()<=2);

}
}
``````

i corrected as you advised. I am getting below output now.

al 1 is--->1
al 2 is--->1
al 3 is--->3
al is [1, 3]===>i am expecting here output as ===>al is [1,1,3]....not sure why 1 is missing ??
value is---->true
0
Commented:
https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
A collection that contains no duplicate elements.
0
Author Commented:
right it is HashSet right.

My naming al tricked me thinking that it is arralist
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

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

Have a better answer? Share it in a comment.