'The Lasted Time from 6 digits C#

Having an array arrs of 6 digits, let's set a latest time (24-hour time) which can be form by putting each digit just one time.

The times (24-hour) are set by"HH:MM:SS", whichHH is from00 to23,MM is from00 to59, and SS is from00 to59.The start time is 00:00:00, and the end time is 23:59:59.

Let's return the latest time with"HH:MM:SS" style. If no time should be valid, return an empty string.

class ChallengeClass { string[] v = new string[720];

int l = 0;

void p(int[] s, int n)
{

    int i = n;

    if (n != 5) while (i < 6)
        {
            (s[i], s[n]) = (s[n], s[i]);

            p(s, n + 1);

            (s[i], s[n]) = (s[n], s[i++]);

        }else if (s[0] * 10 + s[1] < 24 & s[2] * 10 + s[3] < 60 & s[4] * 10 + s[5] < 60) v[l++] = string.Concat(s);

        }

    public string LastedTime(int[] a)

    {

        p(a, 0);

        System.Array.Sort(v);

        return l == 0 ? "" : v[^1].Insert(2, ":").Insert(5, ":");

    }
}

That's my code but it so long, can anyone make it shorter



Solution 1:[1]

public string Time(int[] x)
{
    int m = 86399;
    while (m --> 0)
    {
        var t = new System.TimeSpan(0, 0, m) + "";
        if (a(x) + "::" == a(t.ToCharArray())) return t;
    }
    return "";
}
string a<T>(T[] t)
{
    System.Array.Sort(t);
    return string.Concat(t);
}

This is the best solution

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KenDzz