Find minimum length sub-array which has given sub-sequence in it

Given an array arr[] of N elements, the task is to find the length of the smallest sub-array which has the sequence {0, 1, 2, 3, 4} as a sub-sequence in it.
Â
Examples:Â Â
Input: arr[] = {0, 1, 2, 3, 4, 2, 0, 3, 4}Â
Output: 5Â
The required Subarray is {0, 1, 2, 3, 4} with minimum length.Â
The entire array also includes the sequenceÂ
but it is not minimum in length.Input: arr[] = {0, 1, 1, 0, 1, 2, 0, 3, 4}Â
Output: 6
Approach:Â
- Maintain an array pref[] of size 5 (equal to the size of the sequence) where pref[i] stores the count of i in the given array till now.
- We can increase the count of pref for any number only if pref[Array[i] – 1] > 0. This is because, in order to have the complete sequence as a sub-sequence of the array, all the previous elements of the sequence must occur before the current. Also, store the indices of these elements found so far.
- Whenever we witness 4 i.e., the possible end of the sub-sequence and pref[3] > 0 implies that we have found the sequence in our array. Now mark that index as the end as well as the start point and for all other numbers in sequence from 3 to 0. Apply binary search to find the closest index to the next element of the sequence, which will give us the size of the current valid sub-array.
- The answer is the minimum size of all the valid sub-arrays found in the previous step.
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
#define MAX_INT 1000000Â
// Function to return the minimum length// of a sub-array which contains// {0, 1, 2, 3, 4} as a sub-sequenceint solve(int Array[], int N){    // To store the indices where 0, 1, 2,    // 3 and 4 are present    vector<int> pos[5];Â
    // To store if there exist a valid prefix    // of sequence in array    int pref[5] = { 0 };Â
    // Base Case    if (Array[0] == 0) {        pref[0] = 1;        pos[0].push_back(0);    }Â
    int ans = MAX_INT;Â
    for (int i = 1; i < N; i++) {Â
        // If current element is 0        if (Array[i] == 0) {Â
            // Update the count of 0s till now            pref[0]++;Â
            // Push the index of the new 0            pos[0].push_back(i);        }        else {Â
            // To check if previous element of the            // given sequence is found till now            if (pref[Array[i] - 1] > 0) {                pref[Array[i]]++;                pos[Array[i]].push_back(i);Â
                // If it is the end of sequence                if (Array[i] == 4) {                    int end = i;                    int start = i;Â
                    // Iterate for other elements of the                    // sequence                    for (int j = 3; j >= 0; j--) {                        int s = 0;                        int e = pos[j].size() - 1;                        int temp = -1;Â
                        // Binary Search to find closest                        // occurrence less than equal to                        // starting point                        while (s <= e) {                            int m = (s + e) / 2;                            if (pos[j][m] <= start) {                                temp = pos[j][m];                                s = m + 1;                            }                            else {                                e = m - 1;                            }                        }Â
                        // Update the starting point                        start = temp;                    }Â
                    ans = min(ans, end - start + 1);                }            }        }    }Â
    return ans;}Â
// Driver codeint main(){Â Â Â Â int Array[] = { 0, 1, 2, 3, 4, 2, 0, 3, 4 };Â Â Â Â int N = sizeof(Array) / sizeof(Array[0]);Â
    cout << solve(Array, N);Â
    return 0;} |
Java
// Java implementation of the approachclass GFG {Â Â Â Â static int MAX_INT = 1000000;Â
    // Function to return the minimum length    // of a sub-array which contains    // {0, 1, 2, 3, 4} as a sub-sequence    static int solve(int[] array, int N)    {Â
        // To store the indices where 0, 1, 2,        // 3 and 4 are present        int[][] pos = new int[5][10000];Â
        // To store if there exist a valid prefix        // of sequence in array        int[] pref = new int[5];Â
        // Base Case        if (array[0] == 0) {            pref[0] = 1;            pos[0][pos[0].length - 1] = 0;        }Â
        int ans = MAX_INT;Â
        for (int i = 1; i < N; i++) {Â
            // If current element is 0            if (array[i] == 0) {Â
                // Update the count of 0s till now                pref[0]++;Â
                // Push the index of the new 0                pos[0][pos[0].length - 1] = i;            }Â
            else {Â
                // To check if previous element of the                // given sequence is found till now                if (pref[array[i] - 1] > 0) {                    pref[array[i]]++;                    pos[array[i]][pos[array[i]].length - 1]                        = i;Â
                    // If it is the end of sequence                    if (array[i] == 4) {                        int end = i;                        int start = i;Â
                        // Iterate for other elements of the                        // sequence                        for (int j = 3; j >= 0; j--) {                            int s = 0;                            int e = pos[j].length - 1;                            int temp = -1;Â
                            // Binary Search to find closest                            // occurrence less than equal to                            // starting point                            while (s <= e) {                                int m = (s + e) / 2;                                if (pos[j][m] <= start) {                                    temp = pos[j][m];                                    s = m + 1;                                }                                else                                    e = m - 1;                            }Â
                            // Update the starting point                            start = temp;                        }                        ans = Math.min(ans,                                       end - start + 1);                    }                }            }        }        return ans;    }Â
    // Driver Code    public static void main(String[] args)    {        int[] array = { 0, 1, 2, 3, 4, 2, 0, 3, 4 };        int N = array.length;Â
        System.out.println(solve(array, N));    }}Â
// This code is contributed by// sanjeev2552 |
Python3
# Python3 implementation of the approachÂ
MAX_INT = 1000000Â
# Function to return the minimum length# of a sub-array which contains# 0, 1, 2, 3, 4 as a sub-sequenceÂ
Â
def solve(Array, N):Â
    # To store the indices where 0, 1, 2,    # 3 and 4 are present    pos = [[] for i in range(5)]Â
    # To store if there exist a valid prefix    # of sequence in array    pref = [0 for i in range(5)]Â
    # Base Case    if (Array[0] == 0):        pref[0] = 1        pos[0].append(0)Â
    ans = MAX_INTÂ
    for i in range(N):Â
        # If current element is 0        if (Array[i] == 0):Â
            # Update the count of 0s till now            pref[0] += 1Â
            # Push the index of the new 0            pos[0].append(i)Â
        else:Â
            # To check if previous element of the            # given sequence is found till now            if (pref[Array[i] - 1] > 0):                pref[Array[i]] += 1                pos[Array[i]].append(i)Â
                # If it is the end of sequence                if (Array[i] == 4):                    end = i                    start = iÂ
                    # Iterate for other elements of the sequence                    for j in range(3, -1, -1):                        s = 0                        e = len(pos[j]) - 1                        temp = -1Â
                        # Binary Search to find closest occurrence                        # less than equal to starting point                        while (s <= e):                            m = (s + e) // 2                            if (pos[j][m] <= start):                                temp = pos[j][m]                                s = m + 1Â
                            else:                                e = m - 1Â
                        # Update the starting point                        start = tempÂ
                    ans = min(ans, end - start + 1)Â
    return ansÂ
# Driver codeÂ
Â
Array = [0, 1, 2, 3, 4, 2, 0, 3, 4]N = len(Array)Â
print(solve(Array, N))Â
# This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approachusing System;Â
class GFG {Â Â Â Â static int MAX_INT = 1000000;Â
    // Function to return the minimum length    // of a sub-array which contains    // {0, 1, 2, 3, 4} as a sub-sequence    static int solve(int[] array, int N)    {Â
        // To store the indices where 0, 1, 2,        // 3 and 4 are present        int[, ] pos = new int[5, 10000];Â
        // To store if there exist a valid prefix        // of sequence in array        int[] pref = new int[5];Â
        // Base Case        if (array[0] == 0) {            pref[0] = 1;            pos[0, pos.GetLength(0) - 1] = 0;        }Â
        int ans = MAX_INT;Â
        for (int i = 1; i < N; i++) {Â
            // If current element is 0            if (array[i] == 0) {Â
                // Update the count of 0s till now                pref[0]++;Â
                // Push the index of the new 0                pos[0, pos.GetLength(0) - 1] = i;            }Â
            else {Â
                // To check if previous element of the                // given sequence is found till now                if (pref[array[i] - 1] > 0) {                    pref[array[i]]++;                    pos[array[i], pos.GetLength(1) - 1] = i;Â
                    // If it is the end of sequence                    if (array[i] == 4) {                        int end = i;                        int start = i;Â
                        // Iterate for other elements of the                        // sequence                        for (int j = 3; j >= 0; j--) {                            int s = 0;                            int e = pos.GetLength(1) - 1;                            int temp = -1;Â
                            // Binary Search to find closest                            // occurrence less than equal to                            // starting point                            while (s <= e) {                                int m = (s + e) / 2;                                if (pos[j, m] <= start) {                                    temp = pos[j, m];                                    s = m + 1;                                }                                else                                    e = m - 1;                            }Â
                            // Update the starting point                            start = temp;                        }                        ans = Math.Min(ans,                                       end - start + 1);                    }                }            }        }        return ans;    }Â
    // Driver Code    public static void Main(String[] args)    {        int[] array = { 0, 1, 2, 3, 4, 2, 0, 3, 4 };        int N = array.Length;Â
        Console.WriteLine(solve(array, N));    }}Â
// This code is contributed by PrinciRaj1992 |
Javascript
<script>// Javascript implementation of the approachÂ
let MAX_INT = 1000000;Â
// Function to return the minimum length// of a sub-array which contains// {0, 1, 2, 3, 4} as a sub-sequencefunction solve(array,N){    // To store the indices where 0, 1, 2,    // 3 and 4 are present    let pos = new Array(5);    for(let i=0;i<5;i++)    {        pos[i]=new Array(10000);        for(let j=0;j<10000;j++)        {            pos[i][j]=0;        }    }       // To store if there exist a valid prefix    // of sequence in array    let pref = new Array(5);      for(let i=0;i<5;i++)    {        pref[i]=0;    }         // Base Case    if (array[0] == 0)    {        pref[0] = 1;        pos[0][pos[0].length - 1] = 0;    }       let ans = MAX_INT;       for (let i = 1; i < N; i++)     {           // If current element is 0        if (array[i] == 0)         {               // Update the count of 0s till now            pref[0]++;               // Push the index of the new 0            pos[0][pos[0].length - 1] = i;        }                    else        {               // To check if previous element of the            // given sequence is found till now            if (pref[array[i] - 1] > 0)             {                pref[array[i]]++;                pos[array[i]][pos[array[i]].length - 1] = i;                   // If it is the end of sequence                if (array[i] == 4)                {                    let end = i;                    let start = i;                       // Iterate for other elements of the sequence                    for (let j = 3; j >= 0; j--)                     {                        let s = 0;                        let e = pos[j].length - 1;                        let temp = -1;                           // Binary Search to find closest occurrence                        // less than equal to starting point                        while (s <= e)                         {                            let m = Math.floor((s + e) / 2);                            if (pos[j][m] <= start)                             {                                temp = pos[j][m];                                s = m + 1;                            }                             else                                e = m - 1;                        }                           // Update the starting point                        start = temp;                    }                    ans = Math.min(ans, end - start + 1);                }            }        }    }    return ans;}Â
// Driver Codelet array=[0, 1, 2, 3, 4, 2, 0, 3, 4];let N = array.length;document.write(solve(array, N));Â
Â
// This code is contributed by patel2127</script> |
Output:Â
5
Â
Time Complexity: O(n.log n)
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



