LeetCode 2657: Find the Prefix Common Array of Two Arrays – Java Hashing Solution Explained

Solve LeetCode 2657 (Prefix Common Array) in Java. Learn Brute Force, HashMap, HashSet, and frequency counting techniques. Includes optimized code, dry run, and interview tips.

Krishna Shrivastava
7 views
LinkedInGithubX
0
0
LeetCode 2657: Find the Prefix Common Array of Two Arrays – Java Hashing Solution Explained
Listen to articleAudio version
Ad
Advertisement

Introduction

LeetCode 2657 – Find the Prefix Common Array of Two Arrays is an interesting prefix and hashing problem that tests your understanding of:

  1. Prefix processing
  2. Hashing
  3. Frequency counting
  4. Set operations
  5. Array traversal

At first glance, the problem may look confusing because of the term:

Prefix Common Array

But once you understand the meaning of prefixes and common elements, the problem becomes straightforward.

This problem is useful for improving:

  1. Prefix-based thinking
  2. Hashing intuition
  3. Optimization skills
  4. Interview problem-solving ability

Problem Link

🔗 Find the prefix Common Array of Two Arrays

Problem Statement

You are given two permutations:

A and B

Both arrays contain numbers:

1 to n

exactly once.

You need to create an array:

C

where:

C[i]

represents:

Count of numbers present in both arrays from index 0 to i.

Understanding Prefix Common Array

Suppose:

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

Prefix at Index 0

A Prefix = [1]
B Prefix = [3]

Common numbers:

None

So:

C[0] = 0

Prefix at Index 1

A Prefix = [1,3]
B Prefix = [3,1]

Common numbers:

1, 3

So:

C[1] = 2

Final Output

[0,2,3,4]

Key Observation

Both arrays are permutations.

This means:

  1. Every number appears exactly once.
  2. Once a number appears in both prefixes, it remains common forever.

This simplifies the logic significantly.

Brute Force Approach

Intuition

For every index:

  1. Build prefixes
  2. Compare elements
  3. Count common numbers

Brute Force Algorithm

For each index:

  1. Traverse all previous elements
  2. Check whether numbers exist in both prefixes
  3. Count matches

Brute Force Complexity

Time Complexity

O(N²)

because for every index we may scan previous elements.

Space Complexity

O(N)

Understanding Approach

This approach uses:

  1. HashMap
  2. Prefix tracking
  3. Counting common values

The idea is:

  1. Store prefix elements from B
  2. Traverse A prefix
  3. Count matching numbers

This works because prefixes gradually expand.

Java Solution

class Solution {

public int[] findThePrefixCommonArray(int[] A, int[] B) {

int j = 0;

int[] ans = new int[A.length];

HashMap<Integer, Integer> map = new HashMap<>();

for(int i = 0; i < A.length; i++) {

map.put(B[i], i);

int counter = 0;

int c = 0;

for(int a : map.keySet()) {

if(map.containsKey(A[c])) {
counter++;
}

c++;
}

ans[j] = counter;

j++;
}

return ans;
}
}

Better Optimized Approach

We can solve this more cleanly using:

HashSet

or frequency counting.

Optimized Intuition

At every index:

  1. Add A[i]
  2. Add B[i]
  3. Track which numbers appeared
  4. If a number appears in both arrays, increase common count

Best Optimized Approach Using Frequency Array

Because values are from:

1 to n

we can use a frequency array.

Optimized Java Solution

class Solution {

public int[] findThePrefixCommonArray(int[] A, int[] B) {

int n = A.length;

int[] ans = new int[n];

int[] freq = new int[n + 1];

int common = 0;

for(int i = 0; i < n; i++) {

freq[A[i]]++;

if(freq[A[i]] == 2)
common++;

freq[B[i]]++;

if(freq[B[i]] == 2)
common++;

ans[i] = common;
}

return ans;
}
}

Why Does This Work?

Every number appears once in A and once in B.

So:

  1. First appearance → frequency becomes 1
  2. Second appearance → frequency becomes 2

When frequency becomes:

2

it means the number has appeared in both prefixes.

So we increase:

common

Dry Run

Input

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

Step 1

Index:

0

Add:

1 and 3

Frequencies:

1 → 1
3 → 1

No common elements.

ans[0] = 0

Step 2

Add:

3 and 1

Frequencies:

1 → 2
3 → 2

Two common elements found.

ans[1] = 2

Step 3

Add:

2 and 2

Frequency:

2 → 2

Common becomes:

3
ans[2] = 3

Step 4

Add:

4 and 4

Frequency:

4 → 2

Common becomes:

4
ans[3] = 4

Final Output

[0,2,3,4]

Time Complexity Analysis

Time Complexity

O(N²)

Nested traversal inside loop.

Space Complexity

O(N)

Optimized Frequency Approach

Time Complexity

O(N)

Single traversal.

Space Complexity

O(N)

Frequency array.

HashMap vs Frequency Array

ApproachTime ComplexitySpace Complexity
HashMapO(N²)O(N)
Frequency ArrayO(N)O(N)

Interview Explanation

In interviews, explain:

Since both arrays are permutations, every number appears exactly twice overall — once in A and once in B. Using frequency counting, whenever a number’s frequency becomes 2, it means it has appeared in both prefixes.

This demonstrates:

  1. Prefix understanding
  2. Optimization thinking
  3. Hashing skills

Common Mistakes

1. Recalculating Common Elements Every Time

This causes:

O(N²)

complexity.

2. Forgetting Arrays Are Permutations

This special condition allows frequency optimization.

3. Incorrect Prefix Logic

Remember:

Prefix means elements from 0 to i.

FAQs

Q1. Why is this called Prefix Common Array?

Because:

C[i]

stores common elements between prefixes ending at index:

i

Q2. Why does frequency 2 mean common?

Because every number appears once in each array.

Q3. Which approach is best?

Frequency array approach is the most optimized.

Q4. Is this problem important for interviews?

Yes.

It tests:

  1. Prefix logic
  2. Hashing
  3. Optimization
  4. Array traversal

Related Problems

After mastering this problem, practice:

  1. Intersection of Two Arrays
  2. Intersection of Two Arrays II
  3. Contains Duplicate
  4. Subarray Sum Equals K
  5. Prefix Sum
  6. Find the Difference of Two Arrays

Conclusion

LeetCode 2657 is an excellent prefix and hashing problem.

It teaches:

  1. Prefix processing
  2. Frequency counting
  3. Optimization techniques
  4. Hashing fundamentals

The key insight is:

A number becomes common exactly when its frequency becomes 2.

Once you understand this observation, the optimized solution becomes very simple and efficient.

Ai Assistant Kas