In this post, we will break down LeetCode problem 2657: Find the Prefix Common Array of Two Arrays step by step. Whether you're new to coding or a seasoned problem-solver, I'll guide you through all possible approaches, from the brute force method to an optimized solution. By the end, you'll understand the problem thoroughly and know the best way to solve it efficiently.
Problem Description
You are given two 0-indexed integer permutations, A
and B
, of length n
. A permutation means that both arrays contain all integers from 1
to n
exactly once.
The task is to return the prefix common array C
such that:
C[i]
is the count of numbers that are present at or before indexi
in both arraysA
andB
.
Examples
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 inA
andB
. Hence,C[0] = 0
.At
i = 1
, the common numbers are1
and3
. Thus,C[1] = 2
.At
i = 2
,1
,2
, and3
are common. So,C[2] = 3
.At
i = 3
, all numbers (1
,2
,3
,4
) are common, makingC[3] = 4
.
Example 2:
Input: A = [2,3,1], B = [3,1,2]
Output: [0,1,3]
Constraints:
1 ≤ A.length == B.length == n ≤ 50
1 ≤ A[i], B[i] ≤ n
Approach 1: Brute Force
Steps:
For every index
i
inA
andB
:Traverse all elements from
0
toi
in both arrays.Count how many numbers are common between the two prefixes.
Append this count to the result array
C
.
Code Implementation:
Complexity Analysis:
Time Complexity:
O(n^2)
For each index
i
, we loop up toi
, leading to a quadratic runtime.
Space Complexity:
O(n)
The
unordered_set
uses space proportional to the size of the prefix.
Approach 2: Using Two Hash Maps
Instead of recalculating the common elements for each prefix, we maintain two hash maps:
One for
A
to track the count of each element up to the current index.One for
B
for the same purpose.
Steps:
Traverse the arrays, updating the hash maps for each element at index
i
.Count common elements by comparing the keys in both hash maps.
Code Implementation:
Complexity Analysis:
Time Complexity:
O(n^2)
For each index, we compare keys in the hash maps.
Space Complexity:
O(n)
Two hash maps store up to
n
elements.
Approach 3: Optimized Solution Using One Array
We can track how many times each number has appeared in both arrays using a single frequency array v
.
Increment the frequency when a number appears in
A
orB
.If the frequency of a number becomes
2
, it means it is common in both arrays.
Steps:
Initialize a frequency array
v
of sizen+1
with all values set to0
.Traverse both arrays simultaneously.
Update the frequency for each number in
A
andB
.Increment a
count
variable when a number becomes common.Store the count in the result array
C
.
Code Implementation:
Complexity Analysis:
Time Complexity:
O(n)
We traverse the arrays once.
Space Complexity:
O(n)
The frequency array
v
usesO(n)
space.
All Approaches Time Complexity
Brute Force: Simple but inefficient with
O(n^2)
time complexity.Two Hash Maps: Improves clarity but still
O(n^2)
time complexity.Optimized Single Array: Most efficient with
O(n)
time complexity andO(n)
space.
When solving competitive programming problems, always aim for the most optimized solution that meets the constraints. The third approach is the clear winner here, making it the best choice for this problem.
Some Tags:-
- LeetCode 2657 solution
- Prefix Common Array explanation
- LeetCode Prefix Common Array problem
- Find the Prefix Common Array solution
- Optimized approach for Prefix Common Array
- Prefix Common Array brute force method
- LeetCode array problems explained
- Coding solutions for LeetCode problems
- Competitive programming array questions
- C++ solution for LeetCode 2657
- Prefix Common Array time complexity analysis
- Programming tutorials for LeetCode
- Data structure problems in C++