Find All Occurrences of Subarray in Array
Last Updated : 26 Nov, 2024
Given two arrays a[] and b[], the task is to find all starting indices of b[] as a subarray in a[].
Examples:
Input: a[] = [2, 3, 0, 3, 0, 3, 0], b[] = [3, 0, 3, 0]
Output: [1, 3]
Explanation: The subarray a[1...4] = b[] and subarray a[3...6] = b[].
Input : a[] = [1, 2, 3, 4, 5], b[] = [2, 5, 6]
Output: []
Explanation: No subarray of a[] matches with b[].
[Naive Approach] Comparing All Subarrays - O(n*m) Time and O(1) Space
The idea is to check for all possible indices in a[] as starting index of subarray b[]. For each index, compare the subarray of a[] with b[] using a nested loop. If all the elements match, store the starting index in result. If any element does not match, break and check for next starting index.
C++ // C++ Program to search for subarray by matching // with every possible subarray #include <iostream> #include <vector> using namespace std; vector<int> search(vector<int> &a, vector<int> &b) { int n = a.size(), m = b.size(); vector<int> res; // Iterate over all possible starting indices for(int i = 0; i < n - m + 1; i++) { bool isSame = true; for(int j = 0; j < m; j++) { // If any character does not match, break // and begin from the next starting index if(a[i + j] != b[j]) { isSame = false; break; } } // If all characters are matched, store the // starting index if(isSame) res.push_back(i); } return res; } int main() { vector<int> a = {2, 3, 0, 3, 0, 3, 0}; vector<int> b = {3, 0, 3, 0}; vector<int> res = search(a, b); for(int idx: res) cout << idx << " "; }
Java // Java Program to search for subarray by matching // with every possible subarray import java.util.ArrayList; import java.util.List; class GfG { static List<Integer> search(int[] a, int[] b) { int n = a.length, m = b.length; List<Integer> res = new ArrayList<>(); // Iterate over all possible starting indices for (int i = 0; i < n - m + 1; i++) { boolean isSame = true; // If any character does not match, break // and begin from the next starting index for (int j = 0; j < m; j++) { if (a[i + j] != b[j]) { isSame = false; break; } } // If all characters are matched, store // the starting index if (isSame) res.add(i); } return res; } public static void main(String[] args) { int[] a = {2, 3, 0, 3, 0, 3, 0}; int[] b = {3, 0, 3, 0}; List<Integer> res = search(a, b); for (int idx : res) System.out.print(idx + " "); } }
Python # Python Program to search for subarray by matching # with every possible subarray def search(a, b): n = len(a) m = len(b) res = [] # Iterate over all possible starting indices for i in range(n - m + 1): isSame = True for j in range(m): # If any character does not match, break # and begin from the next starting index if a[i + j] != b[j]: isSame = False break # If all characters are matched, store the starting index if isSame: res.append(i) return res if __name__ == "__main__": a = [2, 3, 0, 3, 0, 3, 0] b = [3, 0, 3, 0] res = search(a, b) for idx in res: print(idx, end=" ")
C# // C# Program to search for subarray by matching // with every possible subarray using System; using System.Collections.Generic; class GfG { static List<int> Search(int[] a, int[] b) { int n = a.Length, m = b.Length; List<int> res = new List<int>(); // Iterate over all possible starting indices for (int i = 0; i < n - m + 1; i++) { bool isSame = true; for (int j = 0; j < m; j++) { // If any character does not match, break // and begin from the next starting index if (a[i + j] != b[j]) { isSame = false; break; } } // If all characters are matched, store the starting index if (isSame) res.Add(i); } return res; } static void Main() { int[] a = { 2, 3, 0, 3, 0, 3, 0 }; int[] b = { 3, 0, 3, 0 }; List<int> res = Search(a, b); foreach (int idx in res) { Console.Write(idx + " "); } } }
JavaScript // JavaScript Program to search for subarray by matching // with every possible subarray function search(a, b) { let n = a.length, m = b.length; let res = []; // Iterate over all possible starting indices for (let i = 0; i < n - m + 1; i++) { let isSame = true; for (let j = 0; j < m; j++) { // If any character does not match, break // and begin from the next starting index if (a[i + j] !== b[j]) { isSame = false; break; } } // If all characters are matched, store the starting index if (isSame) res.push(i); } return res; } // Driver code let a = [2, 3, 0, 3, 0, 3, 0]; let b = [3, 0, 3, 0]; let res = search(a, b); for (let idx of res) { console.log(idx + " "); }
Time Complexity: O(n*m), where n and m are the sizes of the arrays a[] and b[], respectively.
Space Complexity: O(1) as we are not using any additional space to store the arrays or any other variables.
[Expected Approach] Using KMP Algorithm - O(n+m) Time and O(m) Space
The idea is to use KMP Algorithm with a[] as the text and b[] as the pattern. So, instead of comparing characters, we can compare numbers of the array to construct the lps[] array and find all occurrences of b[] in a[].
C++ // C++ Program to search for subarray using KMP Algorithm #include <iostream> #include <vector> using namespace std; void constructLps(vector<int> &pat, vector<int> &lps) { // len stores the length of longest prefix which // is also a suffix for the previous index int len = 0; // lps[0] is always 0 lps[0] = 0; int i = 1; while (i < pat.size()) { // If numbers match, increment the size of lps if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } // If there is a mismatch else { if (len != 0) { // Update len to the previous lps value // to avoid reduntant comparisons len = lps[len - 1]; } else { // If no matching prefix found, set lps[i] to 0 lps[i] = 0; i++; } } } } vector<int> search(vector<int> &a, vector<int> &b) { int n = a.size(); int m = b.size(); vector<int> lps(m); vector<int> res; constructLps(b, lps); // Pointers i and j, for traversing a[] and b[] int i = 0; int j = 0; while (i < n) { // If elements match, move both pointers forward if (a[i] == b[j]) { i++; j++; // If all elements of b[] are matched // store the start index in result if (j == m) { res.push_back(i - j); // Use LPS of previous index to // skip unnecessary comparisons j = lps[j - 1]; } } // If there is a mismatch else { // Use lps value of previous index // to avoid redundant comparisons if (j != 0) j = lps[j - 1]; else i++; } } return res; } int main() { vector<int> a = {2, 3, 0, 3, 0, 3, 0}; vector<int> b = {3, 0, 3, 0}; vector<int> res = search(a, b); for(int idx: res) cout << idx << " "; }
Java // Java Program to search for subarray using KMP Algorithm import java.util.ArrayList; import java.util.List; class GfG { // Function to construct LPS array static void constructLps(int[] pat, int[] lps) { // len stores the length of longest prefix which // is also a suffix for the previous index int len = 0; // lps[0] is always 0 lps[0] = 0; int i = 1; while (i < pat.length) { // If numbers match, increment the size of lps if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } // If there is a mismatch else { if (len != 0) { // Update len to the previous lps value // to avoid redundant comparisons len = lps[len - 1]; } else { // If no matching prefix found, set lps[i] to 0 lps[i] = 0; i++; } } } } // Function to search for the subarray using KMP algorithm static List<Integer> search(int[] a, int[] b) { int n = a.length; int m = b.length; int[] lps = new int[m]; List<Integer> res = new ArrayList<>(); constructLps(b, lps); // Pointers i and j, for traversing a[] and b[] int i = 0; int j = 0; while (i < n) { // If elements match, move both pointers forward if (a[i] == b[j]) { i++; j++; // If all elements of b[] are matched // store the start index in result if (j == m) { res.add(i - j); // Use LPS of previous index to // skip unnecessary comparisons j = lps[j - 1]; } } // If there is a mismatch else { // Use lps value of previous index // to avoid redundant comparisons if (j != 0) j = lps[j - 1]; else i++; } } return res; } public static void main(String[] args) { int[] a = {2, 3, 0, 3, 0, 3, 0}; int[] b = {3, 0, 3, 0}; List<Integer> res = search(a, b); for (int idx : res) System.out.print(idx + " "); } }
Python # Python Program to search for subarray using KMP Algorithm def constructLps(pat, lps): # len stores the length of longest prefix which # is also a suffix for the previous index length = 0 # lps[0] is always 0 lps[0] = 0 i = 1 while i < len(pat): # If numbers match, increment the size of lps if pat[i] == pat[length]: length += 1 lps[i] = length i += 1 # If there is a mismatch else: if length != 0: # Update length to the previous lps value # to avoid redundant comparisons length = lps[length - 1] else: # If no matching prefix found, set lps[i] to 0 lps[i] = 0 i += 1 def search(a, b): n = len(a) m = len(b) lps = [0] * m res = [] constructLps(b, lps) # Pointers i and j, for traversing a[] and b[] i = 0 j = 0 while i < n: # If elements match, move both pointers forward if a[i] == b[j]: i += 1 j += 1 # If all elements of b[] are matched # store the start index in result if j == m: res.append(i - j) # Use LPS of previous index to # skip unnecessary comparisons j = lps[j - 1] else: # If there is a mismatch # Use lps value of previous index # to avoid redundant comparisons if j != 0: j = lps[j - 1] else: i += 1 return res if __name__ == "__main__": a = [2, 3, 0, 3, 0, 3, 0] b = [3, 0, 3, 0] res = search(a, b) for idx in res: print(idx, end=" ")
C# // C# Program to search for subarray using KMP Algorithm using System; using System.Collections.Generic; class GfG { // Function to construct the LPS array (Longest Prefix Suffix) static void ConstructLps(int[] pat, int[] lps) { // len stores the length of the longest prefix which // is also a suffix for the previous index int len = 0; // lps[0] is always 0 lps[0] = 0; int i = 1; while (i < pat.Length) { // If numbers match, increment the size of lps if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } // If there is a mismatch else { if (len != 0) { // Update len to the previous lps value // to avoid redundant comparisons len = lps[len - 1]; } else { // If no matching prefix found, set lps[i] to 0 lps[i] = 0; i++; } } } } // Function to search for the subarray static List<int> Search(int[] a, int[] b) { int n = a.Length; int m = b.Length; int[] lps = new int[m]; List<int> res = new List<int>(); ConstructLps(b, lps); // Pointers i and j, for traversing a[] and b[] int i = 0; int j = 0; while (i < n) { // If elements match, move both pointers forward if (a[i] == b[j]) { i++; j++; // If all elements of b[] are matched // store the start index in result if (j == m) { res.Add(i - j); // Use LPS of previous index to // skip unnecessary comparisons j = lps[j - 1]; } } // If there is a mismatch else { // Use lps value of previous index // to avoid redundant comparisons if (j != 0) j = lps[j - 1]; else i++; } } // Convert the List<int> to an int[] before returning return res; } static void Main() { int[] a = { 2, 3, 0, 3, 0, 3, 0 }; int[] b = { 3, 0, 3, 0 }; List<int> res = Search(a, b); foreach (int idx in res) { Console.Write(idx + " "); } } }
JavaScript // JavaScript Program to search for subarray using KMP Algorithm function constructLps(pat, lps) { // len stores the length of longest prefix which // is also a suffix for the previous index let len = 0; // lps[0] is always 0 lps[0] = 0; let i = 1; while (i < pat.length) { // If numbers match, increment the size of lps if (pat[i] === pat[len]) { len++; lps[i] = len; i++; } // If there is a mismatch else { if (len !== 0) { // Update len to the previous lps value // to avoid redundant comparisons len = lps[len - 1]; } else { // If no matching prefix found, set lps[i] to 0 lps[i] = 0; i++; } } } } function search(a, b) { let n = a.length; let m = b.length; let lps = new Array(m); let res = []; constructLps(b, lps); // Pointers i and j, for traversing a[] and b[] let i = 0; let j = 0; while (i < n) { // If elements match, move both pointers forward if (a[i] === b[j]) { i++; j++; // If all elements of b[] are matched // store the start index in result if (j === m) { res.push(i - j); // Use LPS of previous index to // skip unnecessary comparisons j = lps[j - 1]; } } // If there is a mismatch else { // Use lps value of previous index // to avoid redundant comparisons if (j !== 0) j = lps[j - 1]; else i++; } } return res; } // Driver Code let a = [2, 3, 0, 3, 0, 3, 0]; let b = [3, 0, 3, 0]; let res = search(a, b); for (let idx of res) { console.log(idx + " "); }
Time Complexity: O(n+m), where n and m are the sizes of the arrays a[] and b[], respectively.
Auxiliary Space: O(m), for lps[] array.
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read