आसान 0 हल किए गए 0% स्वीकृति

Array Maximum

Find the largest value in a list.

आप क्या सीखेंगे

  • Find the largest value in one pass
  • Initialize the maximum with the first element
  • Work with negative numbers

समस्या

अनुवाद दिखाएँ

Return the largest value in the list by checking each element once.

Implement array_maximum(numbers).

Input

numbers: A non-empty list of integers.

Output

Return the maximum.

सीमाएँ

1 ≤ length ≤ 100,000

उदाहरण

उदाहरण 1
इनपुट:
numbers = [3, 9, 2]

अपेक्षित आउटपुट:
expected = 9
इनपुट:
std::vector<long long> numbers = {3, 9, 2};

अपेक्षित आउटपुट:
long long expected = 9;
इनपुट:
long[] numbers = new long[] {3, 9, 2};

अपेक्षित आउटपुट:
long expected = 9;

व्याख्या

9 is larger than 3 and 2.

उदाहरण 2
इनपुट:
numbers = [-4, -1, -7]

अपेक्षित आउटपुट:
expected = -1
इनपुट:
std::vector<long long> numbers = {-4, -1, -7};

अपेक्षित आउटपुट:
long long expected = -1;
इनपुट:
long[] numbers = new long[] {-4, -1, -7};

अपेक्षित आउटपुट:
long expected = -1;

व्याख्या

With only negative values, -1 is the largest.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Start with the first element as the best value so far.

संकेत 2

Scan the list once and update the best value whenever you find a larger one.

संकेत 3

Do not assume values are positive; negatives can still be the maximum.

अपेक्षित जटिलता

O(n) time, O(1) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।