↓Skip to main content
  1. LeetCode/

LeetCode 3131: Find the Integer Added to Array I

·1 min· ·
LeetCode Easy Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2024-04-28

  • Submissions: Runtime Error → Wrong Answer → Accepted

Approach
#

Adding the same x to every element of nums1 gives nums2 (possibly reordered), so the minimums also differ by x: x = min(nums2) - min(nums1). The difference of sums divided by the length also works, with integer division.

My Attempts
#

Attempt 1 had a wrong variable name (NameError); attempt 2 divided the sum difference by the length with /, returning the float 5.0, judged wrong. Wrapping it in int passed.

Solution
#

class Solution:
    def addedInteger(self, nums1: List[int], nums2: List[int]) -> int:
        return min(nums2) - min(nums1)

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • / produces a float; the answer must be an integer.

Interview Explanation
#

Since every element gains the same x, the two minimums differ by exactly x, so I return their difference. Time O(n).

Related

LeetCode 3074: Apple Redistribution into Boxes
·1 min
LeetCode Easy Array Greedy Sorting
LeetCode 3074 note: apples can be split freely, so take boxes from the largest capacity until the total fits every apple
LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 min
LeetCode Easy Array Hash-Table Sorting Heap-Priority-Queue
LeetCode 2099 note: pick the k largest values along with their indices, then sort by index to restore the original order
LeetCode 2335: Minimum Amount of Time to Fill Cups
·1 min
LeetCode Easy Array Greedy Sorting Heap-Priority-Queue
LeetCode 2335 note: the answer is max(the largest count, ceil(total / 2)); simulating means filling one cup from each of the two largest every second
LeetCode 1464: Maximum Product of Two Elements in an Array
·1 min
LeetCode Easy Array Sorting Heap-Priority-Queue
LeetCode 1464 note: all values are positive, so take the two largest a and b and return (a - 1)(b - 1)
LeetCode 2357: Make Array Zero by Subtracting Equal Amounts
·1 min
LeetCode Easy Array Hash-Table Greedy Sorting
LeetCode 2357 note: each operation zeroes the group of the smallest positive value, so the answer is the number of distinct positive values
LeetCode 1646: Get Maximum in Generated Array
·2 mins
LeetCode Easy Array Simulation
LeetCode 1646 note: generate by the rules, nums[i // 2] at even i and nums[i // 2] + nums[i // 2 + 1] at odd i, handling n = 0 separately