TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

AmbiguousDates

Problem statement, definition, constraints, and public examples.

Problem Statement

One day while I was traveling I came across an unopened can with the expiration date 03/05/01. In the US this would be the 5th of March 2001, but in other countries this could be the 3rd of May 2001 or the 1st of May 2003. Since I didn't know what date format the country was using, I checked all six permutations of the three numbers and assumed, for safety reasons, the expiration date was the earliest valid date of these. Dates that are not valid are for instance the 31st of April or the 29th of February in a year that is not a leap year, since those days do not exist in the calendar.

You can safely assume that the expiration date is in the 21st century, so the year is between 2000 and 2099, inclusive. During this period, every year divisible by 4 is a leap year. So 2000, 2004, ..., 2096 is a leap year.

Create a class AmbiguousDates containing the method earliestDate which takes an int[] date, containing the three numbers in the expiration date on the can, and returns an int[], the earliest valid date that date could be. The return value should contain exactly three elements describing the earliest valid date. The first element should be the month, the second the day and the third the year (without the century).

Definition

Class:
AmbiguousDates
Method:
earliestDate
Parameters:
int[]
Returns:
int[]
Method signature:
int[] earliestDate(int[] date)
(be sure your method is public)

Notes

  • The number of days in each month is 31,28,31,30,31,30,31,31,30,31,30,31 except that February has 29 days on a leap year.
  • The year will always belong to the 21st century.

Constraints

  • date will contain exactly three elements.
  • Each element in date is between 0 and 99, inclusive.
  • At least one permutation of the numbers in date will produce a valid date.

Examples

  1. {3,8,5}
    Returns: { 5,  8,  3 }
    All permutations result in valid dates, and the earliest of these is 5/8/3, so the method returns {5,8,3}.
  2. {6,4,31}
    Returns: { 4,  6,  31 }
    The last number must be the year, because both possible months (April and June) only have 30 days.
  3. {12,31,99}
    Returns: { 12,  31,  99 }
    There's only one valid date.
  4. {29,56,2}
    Returns: { 2,  29,  56 }
    The year 2056 is a leap year, so February has 29 days. Thus 2/29/56 is valid date and the method returns {2,29,56}
Back to all problems