Check whether an Array is Subarray of another Array

Given two arrays A[] and B[] consisting of n and m integers. The task is to check whether the array B[] is a subarray of the array A[] or not.

Examples:

Input : A[] = {2, 3, 0, 5, 1, 1, 2}, B[] = {3, 0, 5, 1}
Output : Yes

Input : A[] = {1, 2, 3, 4, 5}, B[] = {2, 5, 6}
Output : No



Source : Visa Interview Experience

Simple Approach: A simple approach is to run two nested loops and generate all subarrays of the array A[] and use one more loop to check if any of the subarray of A[] is equal to the array B[].

Efficient Approach : An efficient approach is to use two pointers to traverse both the array simultaneously. Keep the pointer of array B[] still and if any element of A[] matches with the first element of B[] then increase the pointer of both the array else increase the pointer of A and reset the pointer of B to 0. If all the elements of B are matched then print YES otherwise print NO.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if an array is
// subarray of another array
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if an array is
// subarray of another array
bool isSubArray(int A[], int B[], int n, int m)
{
    // Two pointers to traverse the arrays
    int i = 0, j = 0, startedSearchFrom = -1;
  
    // Traverse both arrays simultaneously
    while (i < n && j < m) {
  
        // If element matches
        // increment both pointers
        if (A[i] == B[j]) {
            if(startedSearchFrom != -1) {
              startedSearchFrom = i;
            }
            i++;
            j++;
  
            // If array B is completely
            // traversed
            if (j == m)
                return true;
        }
        // If not,
        // increment i and reset j
        else {
            i = ++startedSearchFrom;
            startedSearchFrom = -1;
            j = 0;
        }
    }
  
    return false;
}
  
// Driver Code
int main()
{
    int n = 7, m = 4;
  
    int A[] = { 2, 3, 0, 5, 1, 1, 2 };
    int B[] = { 3, 0, 5, 1 };
  
    if (isSubArray(A, B, n, m))
        cout << "YES\n";
    else
        cout << "NO\n";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if an array is
// subarray of another array
  
import java.io.*;
  
class GFG {
      
  
  
// Function to check if an array is
// subarray of another array
static boolean isSubArray(int A[], int B[], int n, int m)
{
    // Two pointers to traverse the arrays
    int i = 0, j = 0;
  
    // Traverse both arrays simultaneously
    while (i < n && j < m) {
  
        // If element matches
        // increment both pointers
        if (A[i] == B[j]) {
            i++;
            j++;
  
            // If array B is completely
            // traversed
            if (j == m)
                return true;
        }
        // If not,
        // increment i and reset j
        else {
            i++;
            j = 0;
        }
    }
  
    return false;
}
  
// Driver Code
  
  
    public static void main (String[] args) {
        int n = 7;
        int m = 4;
  
    int A[] = { 2, 3, 0, 5, 1, 1, 2 };
    int B[] = { 3, 0, 5, 1 };
  
    if (isSubArray(A, B, n, m))
        System.out.println("YES\n");
    else
        System.out.println("NO\n");    
  
  
    }
}
// This code is contributed by shs

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if an array is 
// subarray of another array 
  
using System;
  
class GFG { 
  
    // Function to check if an array is 
    // subarray of another array 
    static bool isSubArray(int []A, int []B, int n, int m) 
    
        // Two pointers to traverse the arrays 
        int i = 0, j = 0; 
      
        // Traverse both arrays simultaneously 
        while (i < n && j < m) { 
      
            // If element matches 
            // increment both pointers 
            if (A[i] == B[j]) { 
                i++; 
                j++; 
      
                // If array B is completely 
                // traversed 
                if (j == m) 
                    return true
            
            // If not, 
            // increment i and reset j 
            else
                i++; 
                j = 0; 
            
        
      
        return false
    
  
    // Driver Code 
    static void Main () { 
        int n = 7; 
        int m = 4; 
  
    int []A = { 2, 3, 0, 5, 1, 1, 2 }; 
    int []B = { 3, 0, 5, 1 }; 
  
    if (isSubArray(A, B, n, m)) 
        Console.WriteLine("YES"); 
    else
        Console.WriteLine("NO");     
  
    
    // This code is contributed by Ryuga 

chevron_right


Output:

YES

Time Complexity: O(N)
Auxiliary Space: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : Shashank12, Abhi Reddy



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.