Skip to content

Instantly share code, notes, and snippets.

@varaprasadh
Last active July 30, 2021 10:47
Show Gist options
  • Save varaprasadh/f770cef14d19a2e7546e1f94a6dc4bb3 to your computer and use it in GitHub Desktop.
Save varaprasadh/f770cef14d19a2e7546e1f94a6dc4bb3 to your computer and use it in GitHub Desktop.
islucky - Code Signal
/*
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