Last active
July 30, 2021 10:47
-
-
Save varaprasadh/f770cef14d19a2e7546e1f94a6dc4bb3 to your computer and use it in GitHub Desktop.
islucky - Code Signal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half. | |
Given a ticket number n, determine if it's lucky or not. | |
Example | |
For n = 1230, the output should be | |
isLucky(n) = true; | |
For n = 239017, the output should be | |
isLucky(n) = false. | |
*/ | |
boolean isLucky(int n) { | |
String str = String.valueOf(n); | |
int i = 0,j = str.length()-1; | |
int a = 0; | |
int b = 0; | |
while(i<j){ | |
a += Integer.parseInt(""+str.charAt(i)); | |
b += Integer.parseInt(""+str.charAt(j)); | |
i++; | |
j--; | |
} | |
return a == b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment