3468. Find the Number of Copy Arrays LeetCode Solution

In this guide, you will get 3468. Find the Number of Copy Arrays LeetCode Solution with the best time and space complexity. The solution to Find the Number of Copy Arrays problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Number of Copy Arrays solution in C++
  4. Find the Number of Copy Arrays solution in Java
  5. Find the Number of Copy Arrays solution in Python
  6. Additional Resources
3468. Find the Number of Copy Arrays LeetCode Solution image

Problem Statement of Find the Number of Copy Arrays

You are given an array original of length n and a 2D array bounds of length n x 2, where bounds[i] = [ui, vi].
You need to find the number of possible arrays copy of length n such that:

(copy[i] – copy[i – 1]) == (original[i] – original[i – 1]) for 1 <= i <= n – 1.
ui <= copy[i] <= vi for 0 <= i <= n – 1.

Return the number of such arrays.

Example 1:

Input: original = [1,2,3,4], bounds = [[1,2],[2,3],[3,4],[4,5]]
Output: 2
Explanation:
The possible arrays are:

[1, 2, 3, 4]
[2, 3, 4, 5]

Example 2:

Input: original = [1,2,3,4], bounds = [[1,10],[2,9],[3,8],[4,7]]
Output: 4
Explanation:
The possible arrays are:

[1, 2, 3, 4]
[2, 3, 4, 5]
[3, 4, 5, 6]
[4, 5, 6, 7]

Example 3:

Input: original = [1,2,1,2], bounds = [[1,1],[2,3],[3,3],[2,3]]
Output: 0
Explanation:
No array is possible.

Constraints:

2 <= n == original.length <= 105
1 <= original[i] <= 109
bounds.length == n
bounds[i].length == 2
1 <= bounds[i][0] <= bounds[i][1] <= 109

See also  3090. Maximum Length Substring With Two Occurrences LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3468. Find the Number of Copy Arrays LeetCode Solution in C++

class Solution {
 public:
  int countArrays(vector<int>& original, vector<vector<int>>& bounds) {
    int mn = bounds[0][0];
    int mx = bounds[0][1];

    for (int i = 1; i < original.size(); ++i) {
      const int diff = original[i] - original[i - 1];
      mn = max(mn + diff, bounds[i][0]);
      mx = min(mx + diff, bounds[i][1]);
    }

    return max(0, mx - mn + 1);
  }
};
/* code provided by PROGIEZ */

3468. Find the Number of Copy Arrays LeetCode Solution in Java

class Solution {
  public int countArrays(int[] original, int[][] bounds) {
    int mn = bounds[0][0];
    int mx = bounds[0][1];

    for (int i = 1; i < original.length; ++i) {
      final int diff = original[i] - original[i - 1];
      mn = Math.max(mn + diff, bounds[i][0]);
      mx = Math.min(mx + diff, bounds[i][1]);
    }

    return Math.max(0, mx - mn + 1);
  }
}
// code provided by PROGIEZ

3468. Find the Number of Copy Arrays LeetCode Solution in Python

class Solution:
  def countArrays(self, original: List[int], bounds: List[List[int]]) -> int:
    mn, mx = bounds[0]

    for i in range(1, len(original)):
      diff = original[i] - original[i - 1]
      mn = max(mn + diff, bounds[i][0])
      mx = min(mx + diff, bounds[i][1])

    return max(0, mx - mn + 1)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.