Created
August 28, 2024 22:50
-
-
Save sandipchitale/24399a4ea5f450665a7a0ef34b2feeef to your computer and use it in GitHub Desktop.
Uniform INteger Count between inclusive Range #facebook
This file contains hidden or 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
static int getUniformIntegerCountInInterval(long A, long B) { | |
int count = 0; | |
int lenA = String.valueOf(A).length(); | |
int lenB = String.valueOf(B).length(); | |
for (int i = lenA; i <= lenB; i++) { | |
String spacesOfLengthI = String.format("%" + i + "s", " "); | |
for (int j = 1; j <= 9; j++) { | |
Long uniformNumber = Long.valueOf(spacesOfLengthI.replace(" ", String.valueOf(j))); | |
if (uniformNumber >= A && uniformNumber <= B) { | |
count++; | |
} | |
} | |
} | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment