Ctrl K

Shifted Copies

Medium
You are given an array profile of length n and an array limits of the same length, where limits[i] = [lo, hi]. An integer array copy of length n is a copy if it has the same steps between neighbours as profile, i.e. copy[i] - copy[i - 1] == profile[i] - profile[i - 1] for every i from 1 to n - 1, and every element fits its range: lo ≤ copy[i] ≤ hi where limits[i] = [lo, hi]. Return the number of different copies.
Example 1
profile=[5, 3, 6], limits=[[1, 6], [2, 4], [4, 9]] → 3

A copy looks like [x, x - 2, x + 1]. The limits give 1 ≤ x ≤ 6, 4 ≤ x ≤ 6 and 3 ≤ x ≤ 8, so x ∈ {4, 5, 6}: three copies.

Example 2
profile=[2, 2], limits=[[1, 3], [5, 7]] → 0

Both numbers of the copy must be equal, yet the ranges [1, 3] and [5, 7] do not overlap.

Constraints
  • 2 ≤ n = len(profile) ≤ 10⁵
  • 1 ≤ profile[i] ≤ 10⁹
  • len(limits) = n
  • 1 ≤ limits[i][0] ≤ limits[i][1] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTurn a Square Clockwise MediumPostfix Calculator MediumRoll an Array to the Right MediumPrimes Below a Bound MediumNth Number Built From Given Primes Medium
loading Python…
Input
profile=[5, 3, 6], limits=[[1, 6], [2, 4], [4, 9]]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.