Alle Herausforderungen
Einfach 0 gelöst 0% Erfolgsquote

Min and Max Together

Find the smallest and largest value in one pass.

Problem

Walk through the list once and track both the smallest and the largest value.

Implement min_and_max(numbers).

Input

numbers: A non-empty list of integers.

Output

Return [smallest, largest].

Einschränkungen

1 ≤ length ≤ 100,000

Beispiele

Beispiel 1
Eingabe:
numbers = [4, -2, 9, 0]

Erwartete Ausgabe:
expected = [-2, 9]
Eingabe:
std::vector<long long> numbers = {4, -2, 9, 0};

Erwartete Ausgabe:
std::vector<long long> expected = {-2, 9};
Eingabe:
long[] numbers = new long[] {4, -2, 9, 0};

Erwartete Ausgabe:
long[] expected = new long[] {-2, 9};

Erläuterung

The smallest value is -2 and the largest is 9: [-2, 9].

Beispiel 2
Eingabe:
numbers = [7]

Erwartete Ausgabe:
expected = [7, 7]
Eingabe:
std::vector<long long> numbers = {7};

Erwartete Ausgabe:
std::vector<long long> expected = {7, 7};
Eingabe:
long[] numbers = new long[] {7};

Erwartete Ausgabe:
long[] expected = new long[] {7, 7};

Erläuterung

With one value it is both the smallest and largest: [7, 7].

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.