Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the max date and time from given set of digits in java

Tags:

java

datetime

The problem states that there are some random digits given as input and we have to find the maximum date and time in the format MM/DD HH:MM. Ex:

Input is: 2,2,3,1,3,3,3,5,6,2,0

Output is: 12/30 23:56

I tried the brute force method where each digit is combined with every other digit to form a 2 digit number and so on. But the whole program had around 8 or 9 for loops. Is there a more efficient way to do this??

Edit1: If no possible month or date or time can be formed then the output must be 0. There is no auto insertion of 0s i.e. if you want to form a month 09 then 0 has to be in input and there cannot be repetition of digits.

like image 855
varunj6v1k9 Avatar asked Jan 21 '26 13:01

varunj6v1k9


1 Answers

If you need a largest date, I can think of something like that (in preudocode):

Turn the input digits into a MultiSet // e.g. https://www.geeksforgeeks.org/multiset-interface-guava-java/
for i = 12 to 1 // find the biggest possible month
  try to find if the digits of i are in the multi set. if they are, init the month value & remove them from the multi set.
for d = 31 to 1 // get the biggest possible day
  same
for h = 23 to 1 // get the biggest possible hour
  same
for m = 59 to 1 // get the biggest possible minute
  same
like image 176
riorio Avatar answered Jan 24 '26 03:01

riorio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!