2657. Find the Prefix Common Array of Two Arrays

You are given two 0-indexed integerpermutations A and B of length n.

prefix common array of A and B is an array C such that C[i] is equal to the count of numbers that are present at or before the index i in both A and B.

Return the prefix common array of A and B.

A sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.

Example 1:

Input: A = [1,3,2,4], B = [3,1,2,4]
Output: [0,2,3,4]
Explanation: At i = 0: no number is common, so C[0] = 0.
At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.

Example 2:

Input: A = [2,3,1], B = [3,1,2]
Output: [0,1,3]
Explanation: At i = 0: no number is common, so C[0] = 0.
At i = 1: only 3 is common in A and B, so C[1] = 1.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.

Constraints:

  • 1 <= A.length == B.length == n <= 50
  • 1 <= A[i], B[i] <= n
  • It is guaranteed that A and B are both a permutation of n integers.

Approach 01:

#include <vector>
#include <unordered_set>
#include <algorithm> // for set_intersection
using namespace std;

class Solution {
public:
    vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) {
        int n = A.size();
        vector<int> commonPrefixCount;

        // Iterate through each index and compute common elements in prefixes
        for (int i = 0; i < n; ++i) {
            unordered_set<int> prefixA(A.begin(), A.begin() + i + 1);
            unordered_set<int> prefixB(B.begin(), B.begin() + i + 1);
            
            // Calculate the intersection of the sets
            unordered_set<int> intersection;
            for (const auto& elem : prefixA) {
                if (prefixB.find(elem) != prefixB.end()) {
                    intersection.insert(elem);
                }
            }
            
            // Add the size of the intersection to the result
            commonPrefixCount.push_back(intersection.size());
        }

        return commonPrefixCount;
    }
};

Time Complexity:

  • Prefix Set Construction:For each index \( i \), we construct two sets (prefixA and prefixB) of size \( O(i) \), which takes \( O(i) \) time. So for all indices, the total time for set construction is \( O(1 + 2 + \ldots + n) = O(n^2) \), where \( n \) is the size of the array.
  • Set Intersection:For each index \( i \), we perform an intersection between the two sets, which can take up to \( O(i) \) time. The total time for all intersections is \( O(1 + 2 + \ldots + n) = O(n^2) \).
  • Overall Time Complexity:Since both the set construction and intersection take \( O(n^2) \), the overall time complexity is \( O(n^2) \).

Space Complexity:

  • Set Space:For each index \( i \), we store two sets of size \( O(i) \), leading to a total space usage of \( O(n) \) for each set. The space complexity is \( O(n) \) for the sets used in each iteration.
  • Overall Space Complexity:Since we are using sets and the final result array, the total space complexity is \( O(n) \).
class Solution:
    def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
        n = len(A)
        commonPrefixCount = []

        # Iterate through each index and compute common elements in prefixes
        for i in range(n):
            prefixA = set(A[:i+1])
            prefixB = set(B[:i+1])
            commonPrefixCount.append(len(prefixA & prefixB))

        return commonPrefixCount

Time Complexity:

  • Prefix Set Construction:

    For each index \( i \), we construct two sets (prefixA and prefixB) of size \( O(i) \), which takes \( O(i) \) time. So for all indices, the total time for set construction is \( O(1 + 2 + \ldots + n) = O(n^2) \), where \( n \) is the size of the array.

  • Set Intersection:

    For each index \( i \), we perform an intersection between the two sets, which can take up to \( O(i) \) time. The total time for all intersections is \( O(1 + 2 + \ldots + n) = O(n^2) \).

  • Overall Time Complexity:

    Since both the set construction and intersection take \( O(n^2) \), the overall time complexity is \( O(n^2) \).

Space Complexity:

  • Set Space:

    For each index \( i \), we store two sets of size \( O(i) \), leading to a total space usage of \( O(n) \) for each set. The space complexity is \( O(n) \) for the sets used in each iteration.

  • Overall Space Complexity:

    Since we are using sets and the final result array, the total space complexity is \( O(n) \).

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top