Skip to content

2657. Find the Prefix Common Array of Two Arrays #1142

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can iterate over the two arrays A and B while keeping track of the numbers that have occurred at or before the current index in both arrays. Since both arrays are permutations of the same set of numbers, we can utilize two hash sets (or arrays) to store which numbers have appeared at or before the current index in both arrays. For each index, we can count the common numbers that have appeared in both arrays up to that point.

Solution Approach:

  1. Use two arrays to keep track of the occurrences of numbers in both A and B up to index i.
  2. For each index i, check if both A[i] and B[i] have been seen before. If so, increment the common count.
  3. Use a frequency array to track the presence of numb…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Jan 14, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jan 14, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants