โ— LIVE
OpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leakedOpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leaked
๐Ÿ“… Sat, 21 Mar, 2026โœˆ๏ธ Telegram
AiFeed24

AI & Tech News

๐Ÿ”
โœˆ๏ธ Follow
๐Ÿ Home๐Ÿค–AI๐Ÿ’ปTech๐Ÿš€Startupsโ‚ฟCrypto๐Ÿ”’Security๐Ÿ‡ฎ๐Ÿ‡ณIndiaโ˜๏ธCloud๐Ÿ”ฅDeals
โœˆ๏ธ News Channel๐Ÿ›’ Deals Channel
Home/Cloud & DevOps/Find Minimum and maximum in an array
โ˜๏ธCloud & DevOps

Find Minimum and maximum in an array

Find Minimum and maximum in an array Given an array, find: min val max val Code class Solution: def getMinMax(self, arr): min_val = arr[0] max_val = arr[0] for num in arr: if num max_val: max_val = num return [min_val, max_val] Line-by-Line Explanation Initialize min and max min_val = arr[0] max_val

โšกQuick SummaryAI generating...
J

JAYA SRI J

๐Ÿ“… Mar 21, 2026ยทโฑ 2 min readยทDev.to โ†—
โœˆ๏ธ Telegram๐• TweetWhatsApp
๐Ÿ“ก

Original Source

Dev.to

https://dev.to/jaya_srij_c37a6ea796335c/find-minimum-and-maximum-in-an-array-4i7l
Read Full โ†—

Find Minimum and maximum in an array
Problem

Given an array, find:

  1. min val
  2. max val Code
class Solution:
    def getMinMax(self, arr):
        min_val = arr[0]
        max_val = arr[0]
        for num in arr:
            if num < min_val:
                min_val = num
            elif num > max_val:
                max_val = num
        return [min_val, max_val]

Line-by-Line Explanation

  1. Initialize min and max min_val = arr[0] max_val = arr[0] Why?

We assume first element is both min and max
Avoids using extra comparisons or infinity values
Works for all arrays (even negative numbers)

  1. Loop through array
    for num in arr:
    Why?

    To check every element
    Needed to find smallest and largest values

  2. Check for minimum
    if num < min_val:
    min_val = num

    Why?

    If current number is smaller โ†’ update minimum
    Ensures we always keep the smallest value

  3. Check for maximum
    elif num > max_val:
    max_val = num

    Why?

    If current number is larger โ†’ update maximum
    elif avoids unnecessary check if already smaller

  4. Return result
    return [min_val, max_val]
    Why?

    Returns both values together as a list

Why This Approach is Better

  1. Single Loop (Efficient)
  1. Only one traversal
  2. Time Complexity โ†’ O(n)
  1. No Extra Space

Uses only two variables
Space Complexity โ†’ O(1)

  1. Optimized Comparison

    Uses elif โ†’ avoids extra comparison
    Reduces total operations

  2. Simple and Clean

    Easy to understand
    Works for all types of numbers

Example
arr = [3, 5, 1, 8, 2]

Output:

[1, 8]

Important Points

  1. Initialize with first element
  2. Traverse only once
  3. Update min and max dynamically
  4. Use elif for efficiency
Tags:#cloud#dev.to

Found this useful? Share it!

โœˆ๏ธ Telegram๐• TweetWhatsApp

Read the Full Story

Continue reading on Dev.to

Visit Dev.to โ†—

Related Stories

โ˜๏ธ
โ˜๏ธCloud & DevOps

Majority Element

about 2 hours ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

Building a SQL Tokenizer and Formatter From Scratch โ€” Supporting 6 Dialects

about 2 hours ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

Markdown Knowledge Graph for Humans and Agents

about 2 hours ago

Moving Beyond Disk: How Redis Supercharges Your App Performance
โ˜๏ธCloud & DevOps

Moving Beyond Disk: How Redis Supercharges Your App Performance

about 2 hours ago

๐Ÿ“ก Source Details

Dev.to

๐Ÿ“… Mar 21, 2026

๐Ÿ• about 2 hours ago

โฑ 2 min read

๐Ÿ—‚ Cloud & DevOps

Read Original โ†—

Web Hosting

๐ŸŒ Hostinger โ€” 80% Off Hosting

Start your website for โ‚น69/mo. Free domain + SSL included.

Claim Deal โ†’

๐Ÿ“ฌ AiFeed24 Daily

Top 5 AI & tech stories every morning. Join 40,000+ readers.

โœฆ 40,218 subscribers ยท No spam, ever

Cloud Hosting

โ˜๏ธ Vultr โ€” $100 Free Credit

Deploy cloud servers in 25+ locations. From $2.50/mo. No contract.

Claim $100 Credit โ†’
AiFeed24

India's AI-powered tech news hub. Daily coverage of AI, startups, crypto and emerging technology.

โœˆ๏ธ๐Ÿ›’

Topics

Artificial IntelligenceStartups & VCCryptocurrencyCybersecurityCloud & DevOpsIndia Tech

Company

About AiFeed24Write For UsContact

Daily Digest

Top 5 AI stories every morning. 40,000+ readers.

No spam, ever.

ยฉ 2026 AiFeed24 Media.Affiliate Disclosure โ€” We earn commission on qualifying purchases at no extra cost to you.
PrivacyTermsCookies