r/dailyprogrammer • u/jnazario 2 0 • Mar 02 '18
Weekly #28 - Mini Challenges
So this week, let's do some mini challenges. Too small for an easy but great for a mini challenge. Here is your chance to post some good warm up mini challenges. How it works. Start a new main thread in here.
if you post a challenge, here's a template we've used before from /u/lengau for anyone wanting to post challenges (you can copy/paste this text rather than having to get the source):
**[CHALLENGE NAME]** - [CHALLENGE DESCRIPTION]
**Given:** [INPUT DESCRIPTION]
**Output:** [EXPECTED OUTPUT DESCRIPTION]
**Special:** [ANY POSSIBLE SPECIAL INSTRUCTIONS]
**Challenge input:** [SAMPLE INPUT]
If you want to solve a mini challenge you reply in that thread. Simple. Keep checking back all week as people will keep posting challenges and solve the ones you want.
Please check other mini challenges before posting one to avoid duplications (within reason).
View all comments
Show parent comment
3
u/M4D5-Music Mar 02 '18 edited Mar 02 '18
Java 8
A simple solution (solveSmall), and a memory optimized solution (solveBig) that uses ranges rather than a set. solveBig is a few orders of magnitude slower, but at some point it should get faster than solveSmall if the number is big enough. solveSmall would probably use all the memory on the machine first though.
edit: found out that NavigableMap::floorEntry (TreeMap implementation) has pretty fast access time; using it for solveBig makes it almost half as fast as solveSmall at lower numbers, and it seems to pass solveSmall at about 10 million.
Both have the following output:
Code