Closest Numbers

  • + 2 comments

    one simple algorithm:
    0. get the inputs from console
    1. sort the input array as sortedInput
    2. set lowestDiff to maximum integer value
    3. set resultArray to empty array
    4. for i = 1 to length of sortedInput-1
        4a. delta = absolute value (sortedInput[i] - sortedInput[i-1])
        4b. if delta < lowestDiff, then
            -> lowestDiff = delta
            -> clear resultArray (remove all elements from array)
            -> add to resultArray sortedInpit[i-1], and then sortedInput[i]
    4c. Else, if delta == lowestDiff, then
            -> add to resultArray sortedInpit[i-1], and then sortedInput[i]
    5. print resultArray as space-separated integer on single line.