You are given two 0-indexed integer permutations A and B of length n.
A 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 <= 501 <= A[i], B[i] <= nIt is guaranteed that A and B are both a permutation of n integers.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The basic idea is to go through the arrays piece by piece, comparing them at each step. We build a new array to store the count of common numbers we've seen so far for each comparison.
Here's how the algorithm would work step-by-step:
def find_prefix_common_array(array_one, array_two): length_of_arrays = len(array_one)
prefix_common_array = []
for i in range(length_of_arrays):
common_count = 0
# Iterate through prefixes of both arrays
for j in range(i + 1):
# Check if element is present in both prefixes
if array_one[j] in array_two[:i + 1] and array_two[j] in array_one[:i + 1]:
common_count += 1
prefix_common_array.append(common_count)
return prefix_common_arrayTo efficiently find the common elements in the prefixes of two collections, we'll use a way to remember which items we've seen. This lets us quickly check for matches without re-examining everything repeatedly, leading to a much faster process.
Here's how the algorithm would work step-by-step:
def find_prefix_common_array(array_a, array_b):
prefix_common_count_array = []
seen_elements = set()
common_count = 0
for index in range(len(array_a)):
#Iterate through each element in both arrays.
element_b = array_b[index]
element_a = array_a[index]
if element_b in seen_elements:
# Found common element, increment
common_count += 1
seen_elements.add(element_a)
# Keep track of the number of common elements seen so far.
prefix_common_count_array.append(common_count)
return prefix_common_count_array| Case | How to Handle |
|---|---|
| Null or Empty A or B arrays | Return an empty list or throw an IllegalArgumentException as there's nothing to compare. |
| A and B arrays have different lengths | Throw an IllegalArgumentException or return an empty list, as the problem states they have the same length. |
| Arrays A and B are of length 1 | Check if A[0] equals B[0] and return [1] or [0] accordingly. |
| Arrays A and B contain large integers causing potential integer overflow | The prefix common array elements will not be affected by integer overflow in A and B themselves, only the count itself might overflow if the array length is sufficiently large and mostly common elements occur. |
| Arrays A and B contain duplicate values | The hash set or map based solution implicitly handles duplicates correctly as only the presence or absence of a value is important. |
| Arrays A and B are identical | The prefix common array C will be [1, 2, 3, ..., n], where n is the length of A (or B). |
| Arrays A and B have no common elements | The prefix common array C will be all zeros: [0, 0, 0, ..., 0]. |
| Arrays A and B contain negative numbers | Hash set or map solution handles negative numbers without any modification. |