TopcoderArchiveVisit Topcoder
SRM28 Jul 2005
SRM 255

SequenceOfNumbers

Problem statement, definition, constraints, and public examples.

Problem Statement

It is a common mistake to sort numbers as strings. For example, a sorted sequence like {"1", "174", "23", "578", "71", "9"} is not correctly sorted if its elements are interpreted as numbers rather than strings.

You will be given a String[] sequence that is sorted in non-descending order using string comparison. Return this sequence sorted in non-descending order using numerical comparison instead.

Definition

Class:
SequenceOfNumbers
Method:
rearrange
Parameters:
String[]
Returns:
String[]
Method signature:
String[] rearrange(String[] sequence)
(be sure your method is public)

Constraints

  • sequence will contain between 2 and 50 elements inclusive.
  • Each element of sequence will contain between 1 and 9 characters inclusive.
  • Each element of sequence will consist of only digits ('0'-'9').
  • Each element of sequence will not start with a '0' digit.
  • sequence will be ordered lexicographically.

Examples

  1. {"1","174","23","578","71","9"}
    Returns: {"1", "9", "23", "71", "174", "578" }
  2. {"172","172","172","23","23"}
    Returns: {"23", "23", "172", "172", "172" }
  3. {"183","2","357","38","446","46","628","734","741","838"}
    Returns: {"2", "38", "46", "183", "357", "446", "628", "734", "741", "838" }
Back to all problems