Minimum deletions from one array so it shares no values with the first k elements of another

Quick Overview

Find the fewest deletions from one integer array so that none of its remaining values appear among the first k elements of a second array. The problem tests fast membership checks, counting repeated values correctly, and reading the prefix bound without an off-by-one error.

Minimum deletions from one array so it shares no values with the first k elements of another

Company: Elastic

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given two integer arrays `nums1` and `nums2` and an integer `k`, find the minimum number of deletions required in `nums2` so that it has no overlap with the first `k` elements of `nums1`. A deletion removes one element from `nums2`. Return the minimum number of deletions needed so that no element of the modified `nums2` appears among the first `k` elements of `nums1`. ### Function Signature ```python def min_deletions(nums1: list[int], nums2: list[int], k: int) -> int: ``` ### Rules - The first `k` elements of `nums1` are `nums1[0]` through `nums1[k - 1]`. A value that appears only later in `nums1` is allowed in `nums2`. - Repeated values in `nums2` count separately. If a forbidden value appears three times in `nums2`, all three copies must be deleted. - Repeated values inside the first `k` elements of `nums1` change nothing; only which values appear there matters. ### Constraints - `1 <= nums1.length <= 10^4` - `1 <= nums2.length <= 10^4` - `1 <= k <= nums1.length` - `-10^9 <= nums1[i], nums2[i] <= 10^9` ### Examples ```text Input: nums1 = [1,2,3,4,5], nums2 = [3,4,5,6,7], k = 3 Output: 1 ``` The first 3 elements of `nums1` are 1, 2 and 3. Only the 3 in `nums2` is one of them. The 4 and 5 also appear in `nums1`, but after the first 3 positions, so they stay. ```text Input: nums1 = [10,20,30], nums2 = [10,20,30,40], k = 2 Output: 2 ``` The forbidden values are 10 and 20, and both appear in `nums2`, so both are deleted. The 30 is in `nums1` but outside its first 2 elements. ```text Input: nums1 = [5,5,8,2], nums2 = [5,2,5,8,9], k = 2 Output: 2 ``` The first 2 elements of `nums1` are both 5, so 5 is the only forbidden value. It appears twice in `nums2`, and each copy costs one deletion. The 2 and 8 come later in `nums1` and stay.

Overview: Find the fewest deletions from one integer array so that none of its remaining values appear among the first k elements of a second array. The problem tests fast membership checks, counting repeated values correctly, and reading the prefix bound without an off-by-one error.

Elastic logo
Elastic
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given two integer arrays nums1 and nums2 and an integer k, find the minimum number of deletions required in nums2 so that it has no overlap with the first k elements of nums1.

A deletion removes one element from nums2. Return the minimum number of deletions needed so that no element of the modified nums2 appears among the first k elements of nums1.

Function Signature

def min_deletions(nums1: list[int], nums2: list[int], k: int) -> int:

Rules

  • The first k elements of nums1 are nums1[0] through nums1[k - 1] . A value that appears only later in nums1 is allowed in nums2 .
  • Repeated values in nums2 count separately. If a forbidden value appears three times in nums2 , all three copies must be deleted.
  • Repeated values inside the first k elements of nums1 change nothing; only which values appear there matters.

Constraints

  • 1 <= nums1.length <= 10^4
  • 1 <= nums2.length <= 10^4
  • 1 <= k <= nums1.length
  • -10^9 <= nums1[i], nums2[i] <= 10^9

Examples

Input: nums1 = [1,2,3,4,5], nums2 = [3,4,5,6,7], k = 3
Output: 1

The first 3 elements of nums1 are 1, 2 and 3. Only the 3 in nums2 is one of them. The 4 and 5 also appear in nums1, but after the first 3 positions, so they stay.

Input: nums1 = [10,20,30], nums2 = [10,20,30,40], k = 2
Output: 2

The forbidden values are 10 and 20, and both appear in nums2, so both are deleted. The 30 is in nums1 but outside its first 2 elements.

Input: nums1 = [5,5,8,2], nums2 = [5,2,5,8,9], k = 2
Output: 2

The first 2 elements of nums1 are both 5, so 5 is the only forbidden value. It appears twice in nums2, and each copy costs one deletion. The 2 and 8 come later in nums1 and stay.