Data Structures and Algorithms (DSA)  

How to Find the Maximum Number in an Array: The Smart Way Insurers Detect Risk Outliers

Table of Contents

  1. Introduction

  2. What Does "Find the Maximum" Really Mean?

  3. Real-World Scenario: Identifying High-Risk Claims in Real Time

  4. Complete Implementation with Test Cases

  5. Best Practices and Performance Tips

  6. Conclusion

Introduction

Finding the maximum number in an array seems trivial.

max(arr) — done.

But in insurance systems processing thousands of claims per hour, the "maximum" isn't just a number — it's a red flag.

A single unusually large claim can signal fraud, system error, or a catastrophic event. Missing it? That's how insurers get blindsided.

This article shows you how top insurance tech teams find the maximum — not just with max(), but with context, validation, and real-time logic that protects millions in reserves.

What Does "Find the Maximum" Really Mean?

In insurance, the maximum isn't always the largest value.

Sometimes it's:

  • The highest claim in a policyholder's history

  • The largest payout in a region this month

  • The most severe loss in a portfolio of 10,000 policies

And you often need more than the value — you need its position, its context, and whether it's an outlier.

That's why max() alone isn't enough.

Real-World Scenario: Identifying High-Risk Claims in Real Time

You're building a real-time monitoring system for a property insurer.

You receive a stream of claims:

claims = [1200, 4500, 8900, 21000, 3200, 18500, 950]

Your task

Find the largest claim — and if it exceeds a dynamic threshold (e.g., 3x the median), flag it for fraud review.

But you can't just call max().
You need to:

  • Track the index (to pull full claim details)

  • Compare against historical norms

  • Avoid scanning the list multiple times

Efficiency and accuracy are non-negotiable.

Complete Implementation with Test Cases

import unittest
from typing import List, Dict, Tuple, Optional
import numpy as np #  Use NumPy for fast, explicit statistical calculations

class ClaimAnalyzer:
    """Analyzes a list of insurance claim amounts."""
    
    def __init__(self, claims: List[float]):
        # Convert claims to a NumPy array for vectorized operations
        self.claims = np.array(claims)

    def find_max_claim(self) -> Optional[float]:
        """Return the largest claim amount."""
        if self.claims.size == 0:
            return None
        # Use NumPy's max for speed and simplicity
        return self.claims.max()

    def find_max_claim_with_index(self) -> Optional[Tuple[int, float]]:
        """Return (index, value) of the largest claim."""
        if self.claims.size == 0:
            return None
        
        # Use NumPy's argmax to find the index of the first occurrence of the maximum value
        max_idx = self.claims.argmax()
        max_val = self.claims[max_idx]
        return (int(max_idx), float(max_val))

    def find_max_claim_object(self, claim_list: List[Dict]) -> Optional[Dict]:
        """Find the claim object with the highest amount."""
        if not claim_list:
            return None
        # This implementation is already optimal and highly Pythonic, so we keep it.
        return max(claim_list, key=lambda x: x["amount"])

    def is_outlier(self, threshold_factor: float = 3.0) -> bool:
        """Check if max claim is an outlier (e.g., > 3x median)."""
        if self.claims.size < 2:
            return False
            
        # Use NumPy's functions for robust statistical calculations
        med = np.median(self.claims)
        max_claim = self.claims.max()
        
        # Avoid division by zero, though median is typically non-zero for claim amounts
        if med == 0 and max_claim > 0:
             return True
             
        return max_claim > threshold_factor * med

class TestClaimAnalyzer(unittest.TestCase):
    # Setup remains the same
    def setUp(self):
        self.claims = [1200, 4500, 8900, 21000, 3200, 18500, 950]
        self.analyzer = ClaimAnalyzer(self.claims)
        self.claim_objects = [
            {"id": "C101", "amount": 1200},
            {"id": "C102", "amount": 21000}, # Max
            {"id": "C103", "amount": 8900}
        ]
        # Median is 4500. Max is 21000. 3 * 4500 = 13500. 21000 > 13500 (Outlier)

    def test_find_max_claim(self):
        self.assertEqual(self.analyzer.find_max_claim(), 21000.0)

    def test_find_max_with_index(self):
        idx, val = self.analyzer.find_max_claim_with_index()
        self.assertEqual(val, 21000.0)
        self.assertEqual(idx, 3)

    def test_find_max_claim_object(self):
        result = self.analyzer.find_max_claim_object(self.claim_objects)
        self.assertEqual(result["id"], "C102")
        self.assertEqual(result["amount"], 21000)

    def test_outlier_detection(self):
        self.assertTrue(self.analyzer.is_outlier())
        
        # Test a non-outlier case (median 1000, max 2000, threshold 3x)
        not_outlier_analyzer = ClaimAnalyzer([1000, 1000, 2000])
        self.assertFalse(not_outlier_analyzer.is_outlier()) 

    def test_edge_cases(self):
        empty = ClaimAnalyzer([])
        self.assertIsNone(empty.find_max_claim())
        self.assertIsNone(empty.find_max_claim_with_index())
        self.assertFalse(empty.is_outlier()) 

        single = ClaimAnalyzer([5000])
        self.assertEqual(single.find_max_claim(), 5000.0)
        self.assertFalse(single.is_outlier())
        
        # Test zero median case
        zero_median_analyzer = ClaimAnalyzer([0, 0, 100])
        self.assertTrue(zero_median_analyzer.is_outlier()) # max (100) > 3 * med (0) 
        

if __name__ == "__main__":
    # Demo
    claims = [1200, 4500, 8900, 21000, 3200, 18500, 950]
    analyzer = ClaimAnalyzer(claims)

    max_val = analyzer.find_max_claim()
    idx, val = analyzer.find_max_claim_with_index()
    is_risky = analyzer.is_outlier()

    print(" INSURANCE CLAIM MAX DETECTOR (NumPy Enhanced)")
    print(f"Largest claim: ${val:,.0f} (at position {idx})")
    print(f"Outlier? {'Yes — flag for review! ' if is_risky else 'No'}")

    claim_objects = [
        {"id": "C101", "amount": 1200},
        {"id": "C102", "amount": 21000},
        {"id": "C103", "amount": 8900}
    ]
    largest_obj = analyzer.find_max_claim_object(claim_objects)
    print(f"\nFull claim details of the largest claim: {largest_obj}")

    print("\n Running tests...")
    unittest.main(argv=[''], exit=False, verbosity=1)
q

Best Practices and Performance Tips

  • Use max() for simple values — it’s implemented in C and blazing fast.

  • Use key parameter for objects — no need to extract values manually.

  • Track index in a single loop if you need position — avoid list.index(max()) (scans twice).

  • Validate empty lists — max([]) throws an exception.

  • For large data, use NumPy — np.argmax() is optimized for speed.

  • Combine with statistical checks — max alone isn’t enough; compare to median or mean.

Conclusion

Finding the maximum isn’t about syntax — it’s about insight. In insurance, the largest claim isn’t just a number. It’s a signal.

Master the right method — and you won’t just find the max. You’ll find the risk, the fraud, and the opportunity hiding in plain sight.

The best systems don’t just find the maximum — they ask why it’s there.