Problem 3131

Abstract / Summary

Given two arrays, find the number in the second that is not in the first.

Note

The full problem description, including helpful examples, can be found on LeetCode.

Python 3131.py

class Solution:
    def addedInteger(self, nums1: List[int], nums2: List[int]) -> int:
        return min(nums2) - min(nums1)
3 lines | 129 bytes | submitted May 4, 2024 | GitHub   tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 83% (100k+ submissions).

Typical patterns to solve this problem:
  • Array
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.

See this dedicated page for more details.
LeetCode profile | GitHub repo

Code Walkthrough

Overview

The provided implementation defines a Solution class with a single method, addedInteger. The method receives two integer arrays, nums1 and nums2, and computes the difference between the smallest element of the second array and the smallest element of the first array. This difference represents the constant value that, when added to every element of nums1, aligns its minimum with the minimum of nums2.

Key Steps

The core logic consists of three distinct operations: locating the minimum value in the first list, locating the minimum value in the second list, and calculating the arithmetic difference between those two minima. Each operation is expressed concisely using Python’s built-in min function.

min_nums1 = min(nums1)

The statement above scans nums1 to identify its smallest element. The built-in min is implemented in C, providing a fast linear pass over the sequence.

min_nums2 = min(nums2)

Similarly, this line extracts the smallest value from nums2. The symmetry of the two calls emphasizes the method’s focus on the extremal values of the inputs.

return min_nums2 - min_nums1

The final expression computes the required offset by subtracting the minimum of nums1 from the minimum of nums2. The result is returned directly, completing the method’s contract.

Edge Cases Handled

The algorithm naturally accommodates several edge conditions without additional code. When both input arrays contain negative numbers, the min function still correctly identifies the most negative value, and the subtraction yields the appropriate offset. If the arrays consist of a single element each, the method reduces to a simple subtraction of those two values, which is precisely the intended behavior. The use of built-in functions also ensures that the method correctly processes arrays containing duplicate values; the minimum is still accurately determined regardless of repetition.

Performance Optimizations

The implementation leverages Python’s native min function, which is highly optimized at the interpreter level. By delegating the search for the smallest element to this built-in, the code avoids explicit Python loops, reducing interpreter overhead. The method performs a single pass over each input list, resulting in minimal memory usage because no auxiliary data structures are allocated beyond the temporary variables holding the minima. The arithmetic operation itself is a constant-time integer subtraction, contributing negligible cost relative to the linear scans. Overall, the design achieves an efficient balance between readability and execution speed.

written by openai gpt

ELI5 - Simplified Explanation

Think of the two groups of numbers like two piles of building blocks. Every block in the first pile needs to grow by the same magic amount to turn into the blocks in the second pile. To find out what that magic amount is, look at the very smallest block in the first pile and compare it to the very smallest block in the second pile. By subtracting the size of the small block in the first pile from the size of the small block in the second pile, the difference tells exactly how much the blocks grew. This works because since every number increased by the same amount, the smallest number in the first group will always line up with the smallest number in the second group after the change.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 27 | median = 8 | μ = 7.67 | σ = 2.05

Complexity Analysis

Time: linear
As a rule of thumb, the maximum input size for one second execution time is
  • factorial: up to 10 elements
  • exponential: up to 20 elements
  • polynomial: a few thousand elements
  • loglinear: up to 100,000 elements
  • linear: a few million elements
  • logarithmic: a billion elements
  • constant: practically unlimited
generated automatically

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
Obviously, I did that after I solved the problem myself.

In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.

And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.

(click on a column header to sort the table)
author
author solutions
www AlgoMonster Java C++ Python TS
WalkCCC Java C++ Python
leetcode.ca Java C++ Go Python TS
Progiez Java C++ Python
github charles-wangkai Java
everthis JS
fartem Ruby
fsq C++
varunu28 Java
AlgoMonster C++, Python, Java, TypeScript
WalkCCC C++, Python, Java
leetcode.ca C++, Go, Java, Python, TypeScript
Progiez C++, Java, Python
charles-wangkai Java
everthis JavaScript
fartem Ruby
fsq C++
varunu28 Java
20 links | 9 authors | 7 languages

Videos

5 videos   did my crawler come up with invalid videos ? drop me a line.

Solved On The Same Day

5 solutions   May 4, 2024

Extra

There are a few extra pages giving you some insights into my setup:
  • Tech Stack - my development environment and tools
  • AI - how AI helped me create this website (NOT solving the problems)
  • Progress - progression over time
  • Quality - code quality