Given two arrays A and B of the same length N, filled with a permutation of natural numbers from 1 to N, the task is to count the number of common subarrays in A and B.
Input: A = [1, 2, 3], B = [2, 3, 1]
The common subarrays are , , , [2, 3]
Hence, total count = 4
Input: A = [1, 2, 3, 4, 5], B = [2, 3, 1, 4, 5]
The common subarrays are , , , , , [2, 3], [4, 5]
Hence, total count = 7
The idea is to generate all subarrays of A and B separately, which would take O(N2) for each array. Now, compare all subarrays of A with all subarrays of B and count common subarrays. It would take O(N4).
The idea is to use Hashing to solve this problem efficiently.
- Create a Hash array H of size N+1.
- Represent all elements of A by their respective indices:
Element Representaion A 0 A 1 A 2 . . and so on.
- Use array H to store this representation, H[ A[ i ] ] = i
- Update the elements of B according to this new representation using H, B[ i ] = H[ B[ i ] ]
- Now, array A can be represented as [0, 1, 2, ..N], so simply count number of subarrays in B which have consecutive elements. Once we get length K of subarray of consecutive elements, count total possible subarray using following relation:
Total number of subarrays = (K * (K + 1)) / 2
Look at this example to understand this approach in detail:
A = [4, 3, 1, 2, 5]
B = [3, 1, 2, 4, 5]
Common subarrays are , , , , , [3, 1], [1, 2], [3, 1, 2] = 8
1. Represent A[i] as i, and store in H as H[A[i]] = i, Now array H from index 1 to N is,
H = [2, 3, 1, 0, 4]
2. Update B according to H, B[i] = H[B[i]]
B = [1, 2, 3, 0, 4]
3. Look for subarray in B with consecutive elements,
Subarray from index 0 to 2 is [1, 2, 3], consisting of consecutive elements with length K = 3
Element at index 3 forms a subarray  of length K = 1
Element at index 4 forms a subarray  of length K = 1
4. Total number of common subarrays =
(3*(3+1))/2 + (1*(1+1))/2 + (1*(1+1))/2 = 6 + 1 + 1 = 8
Below is the implementation of the above approach:
Time complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Longest common subsequence with permutations allowed
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Count the number of special permutations
- Count permutations that are first decreasing then increasing.
- Count permutations that produce positive result
- Count of K-size substrings having palindromic permutations
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Count of subarrays with sum at least K
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Count subarrays with Prime sum
- Count subarrays with non-zero sum in the given Array
- Count subarrays with sum equal to its XOR value
- Count the number of subarrays having a given XOR
- Count subarrays with same even and odd elements
- Count of all possible bitonic subarrays
- Count of subarrays whose sum is a perfect square
- Count subarrays with all elements greater than K
- Count of subarrays which contains a given number exactly K times
- Count of all possible reverse bitonic subarrays
- Count of longest possible subarrays with sum not divisible by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.