LeetCode Problem

How to Solve Find the Prefix Common Array of Two Arrays

To solve this problem, you need to determine how many numbers are common at each index in two given permutations. By scanning the arrays and using hash lookup, you can track the common elements up to each index and build the result incrementally. The approach is efficient, working in O(n) time with O(n) space complexity.

GhostInterview Help

Need help with Find the Prefix Common Array of Two Arrays without spending extra time grinding it?

GhostInterview can read Find the Prefix Common Array of Two Arrays from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #2657Array scanning plus hash lookupReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, you need to determine how many numbers are common at each index in two given permutations. By scanning the arrays and using hash lookup, you can track the common elements up to each index and build the result incrementally. The approach is efficient, working in O(n) time with O(n) space complexity.

Problem Statement

You are given two 0-indexed integer permutations A and B of length n. Your task is to return the prefix common array C of A and B. The array C should have the property that C[i] equals the count of numbers that appear at or before index i in both A and B.

For example, if A = [1, 3, 2, 4] and B = [3, 1, 2, 4], the output C will be [0, 2, 3, 4]. At each index i, count the common elements in A and B up to that point. Consider optimizing this using a hash table to track the occurrences efficiently.

Examples

Example 1

Input: A = [1,3,2,4], B = [3,1,2,4]

Output: [0,2,3,4]

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]

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.

Solution Approach

Array Scanning with Hash Lookup

To solve this problem, you can iterate over both arrays, using a hash table to track the occurrences of numbers in the arrays. As you traverse the arrays, update the count of common elements at each index by comparing the frequency of numbers from both arrays.

Efficient Count Tracking

Keep a frequency array to store the number of times each integer appears in both arrays up to index i. This allows for constant time lookups and helps you efficiently build the prefix common array without repeatedly checking the entire array.

Incremental Construction of the Result

At each step, update the prefix common array by incrementing the common count whenever a number appears in both A and B. This approach ensures that you only need to traverse each array once, maintaining an O(n) time complexity.

Complexity Analysis

MetricValue
TimeO(n)
SpaceO(n)

The time complexity is O(n) because we traverse each array only once, and the hash table lookup operations are O(1) on average. The space complexity is O(n) as we maintain the frequency counts for each number in the arrays up to index i.

What Interviewers Usually Probe

  • The candidate demonstrates an understanding of array scanning and hash lookup.
  • The candidate considers the importance of efficient tracking using a frequency array.
  • The candidate proposes an optimal solution with clear handling of edge cases.

Common Pitfalls or Variants

Common pitfalls

  • Failing to properly track the frequency of numbers in both arrays, leading to incorrect results.
  • Overcomplicating the solution with nested loops instead of using efficient hash lookups.
  • Not considering the constraints and trying to use less efficient approaches with higher time complexity.

Follow-up variants

  • What if the arrays were not permutations and contained duplicates? How would the approach change?
  • Can the algorithm be optimized further for larger array sizes? Consider techniques like early termination.
  • What if the problem were extended to find common prefixes for more than two arrays?

How GhostInterview Helps

  • GhostInterview breaks down the problem-solving process into clear steps, helping you implement array scanning plus hash lookup efficiently.
  • It guides you through the process of optimizing the algorithm, ensuring that you maintain both time and space efficiency.
  • GhostInterview identifies potential pitfalls early in the solution process, allowing you to avoid common mistakes and edge cases.

Topic Pages

FAQ

How does the array scanning approach work in this problem?

Array scanning helps you evaluate each index in both arrays, incrementally counting common elements. This approach ensures the solution is efficient and avoids unnecessary rechecking of elements.

What is the time complexity of the solution?

The time complexity of the solution is O(n), where n is the length of the input arrays. This is due to the single pass over the arrays and constant-time hash table lookups.

Why is using a hash table important in this problem?

A hash table is crucial for efficiently tracking the frequency of numbers in both arrays, allowing for constant time lookups and avoiding repeated iterations over the arrays.

Can this solution be optimized for larger arrays?

For very large arrays, optimizing space and time complexity can involve early termination or other advanced techniques, but for arrays of size n <= 50, this solution is efficient.

What are some common pitfalls in solving this problem?

Common pitfalls include failing to track the frequency of numbers correctly, overcomplicating the solution with unnecessary nested loops, or ignoring the problem's constraints.

GhostInterview Solver

Need direct help with Find the Prefix Common Array of Two Arrays instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Find the Prefix Common Array of Two Arrays from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.