Longest subarray of an array which is a subsequence in another array

Given two arrays arr1[] and arr2[], the task is to find the longest subarray of arr1[] which is a subsequence of arr2[].
Examples:
Input: arr1[] = {4, 2, 3, 1, 5, 6}, arr2[] = {3, 1, 4, 6, 5, 2}
Output: 3
Explanation: The longest subarray of arr1[] which is a subsequence in arr2[] is {3, 1, 5}Input: arr1[] = {3, 2, 4, 7, 1, 5, 6, 8, 10, 9}, arr2[] = {9, 2, 4, 3, 1, 5, 6, 8, 10, 7}
Output: 5
Explanation: The longest subarray in arr1[] which is a subsequence in arr2[] is {1, 5, 6, 8, 10}.
Approach: The idea is to use Dynamic Programming to solve this problem. Follow the steps below to solve the problem:
- Initialize a DP[][] table, where DP[i][j] stores the length of the longest subarray up to the ith index in arr1[] which is a subsequence in arr2[] up to the jth index.
- Now, traverse over both the arrays and perform the following:
- Case 1: If arr1[i] and arr2[j] are equal, add 1 to DP[i ā 1][j ā 1] as arr1[i] and arr2[j] contribute to the required length of the longest subarray.
- Case 2: If arr1[i] and arr2[j] are not equal, set DP[i][j] = DP[i ā 1][j].
- Finally, print the maximum value present in DP[][] table as the required answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Ā
Ā Ā Ā Ā // Function to find the length of theĀ Ā Ā Ā // longest subarray in arr1[] whichĀ Ā Ā Ā // is a subsequence in arr2[]Ā Ā Ā Ā int LongSubarrSeq(int arr1[], int arr2[], int M, int N)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // Length of the array arr1[]Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Length of the requiredĀ Ā Ā Ā Ā Ā Ā Ā // longest subarrayĀ Ā Ā Ā Ā Ā Ā Ā int maxL = 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Initialize DP[]arrayĀ Ā Ā Ā Ā Ā Ā Ā int DP[M + 1][N + 1];Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse array arr1[]Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= M; i++)Ā Ā Ā Ā Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Traverse array arr2[]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 1; j <= N; j++)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr1[i - 1] == arr2[j - 1])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // arr1[i - 1] contributes toĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the length of the subarrayĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i][j] = 1 + DP[i - 1][j - 1];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // OtherwiseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i][j] = DP[i][j - 1];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Find the maximum valueĀ Ā Ā Ā Ā Ā Ā Ā // present in DP[][]Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= M; i++) Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 1; j <= N; j++)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxL = max(maxL, DP[i][j]);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return the resultĀ Ā Ā Ā Ā Ā Ā Ā return maxL;Ā Ā Ā Ā }Ā
Ā
// Driver Codeint main(){Ā Ā Ā Ā int arr1[] = { 4, 2, 3, 1, 5, 6 };Ā Ā Ā Ā int M = sizeof(arr1) / sizeof(arr1[0]);Ā Ā Ā Ā Ā Ā Ā Ā Ā int arr2[] = { 3, 1, 4, 6, 5, 2 };Ā Ā Ā Ā int N = sizeof(arr2) / sizeof(arr2[0]);Ā
Ā Ā Ā Ā // Function call to find the lengthĀ Ā Ā Ā // of the longest required subarrayĀ Ā Ā Ā cout << LongSubarrSeq(arr1, arr2, M, N) <<endl;Ā Ā Ā Ā return 0;}Ā
// This code is contributed by code_hunt. |
Java
// Java program// for the above approachĀ
import java.io.*;Ā
class GFG {Ā
Ā Ā Ā Ā // Function to find the length of theĀ Ā Ā Ā // longest subarray in arr1[] whichĀ Ā Ā Ā // is a subsequence in arr2[]Ā Ā Ā Ā private static int LongSubarrSeq(Ā Ā Ā Ā Ā Ā Ā Ā int[] arr1, int[] arr2)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // Length of the array arr1[]Ā Ā Ā Ā Ā Ā Ā Ā int M = arr1.length;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Length of the array arr2[]Ā Ā Ā Ā Ā Ā Ā Ā int N = arr2.length;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Length of the requiredĀ Ā Ā Ā Ā Ā Ā Ā // longest subarrayĀ Ā Ā Ā Ā Ā Ā Ā int maxL = 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Initialize DP[]arrayĀ Ā Ā Ā Ā Ā Ā Ā int[][] DP = new int[M + 1][N + 1];Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse array arr1[]Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= M; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Traverse array arr2[]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 1; j <= N; j++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr1[i - 1] == arr2[j - 1]) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // arr1[i - 1] contributes toĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the length of the subarrayĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i][j] = 1 + DP[i - 1][j - 1];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // OtherwiseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i][j] = DP[i][j - 1];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Find the maximum valueĀ Ā Ā Ā Ā Ā Ā Ā // present in DP[][]Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= M; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 1; j <= N; j++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxL = Math.max(maxL, DP[i][j]);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return the resultĀ Ā Ā Ā Ā Ā Ā Ā return maxL;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver CodeĀ Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int[] arr1 = { 4, 2, 3, 1, 5, 6 };Ā Ā Ā Ā Ā Ā Ā Ā int[] arr2 = { 3, 1, 4, 6, 5, 2 };Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Function call to find the lengthĀ Ā Ā Ā Ā Ā Ā Ā // of the longest required subarrayĀ Ā Ā Ā Ā Ā Ā Ā System.out.println(LongSubarrSeq(arr1, arr2));Ā Ā Ā Ā }} |
Python3
# Python program# for the above approachĀ
# Function to find the length of the# longest subarray in arr1 which# is a subsequence in arr2def LongSubarrSeq(arr1, arr2):Ā Ā Ā Ā Ā Ā Ā # Length of the array arr1Ā Ā Ā Ā M = len(arr1);Ā
Ā Ā Ā Ā # Length of the array arr2Ā Ā Ā Ā N = len(arr2);Ā
Ā Ā Ā Ā # Length of the requiredĀ Ā Ā Ā # longest subarrayĀ Ā Ā Ā maxL = 0;Ā
Ā Ā Ā Ā # Initialize DParrayĀ Ā Ā Ā DP = [[0 for i in range(N + 1)] for j in range(M + 1)];Ā
Ā Ā Ā Ā # Traverse array arr1Ā Ā Ā Ā for i in range(1, M + 1):Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Traverse array arr2Ā Ā Ā Ā Ā Ā Ā Ā for j in range(1, N + 1):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr1[i - 1] == arr2[j - 1]):Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # arr1[i - 1] contributes toĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # the length of the subarrayĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i][j] = 1 + DP[i - 1][j - 1];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # OtherwiseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i][j] = DP[i][j - 1];Ā
Ā Ā Ā Ā # Find the maximum valueĀ Ā Ā Ā # present in DPĀ Ā Ā Ā for i in range(M + 1):Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Traverse array arr2Ā Ā Ā Ā Ā Ā Ā Ā for j in range(1, N + 1):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxL = max(maxL, DP[i][j]);Ā
Ā Ā Ā Ā # Return the resultĀ Ā Ā Ā return maxL;Ā
# Driver Codeif __name__ == '__main__':Ā Ā Ā Ā arr1 = [4, 2, 3, 1, 5, 6];Ā Ā Ā Ā arr2 = [3, 1, 4, 6, 5, 2];Ā
Ā Ā Ā Ā # Function call to find the lengthĀ Ā Ā Ā # of the longest required subarrayĀ Ā Ā Ā print(LongSubarrSeq(arr1, arr2));Ā
Ā Ā Ā Ā # This code contributed by shikhasingrajput |
C#
// C# program for the above approachusing System;Ā
class GFG{Ā
// Function to find the length of the// longest subarray in arr1[] which// is a subsequence in arr2[]private static int LongSubarrSeq(int[] arr1, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int[] arr2){Ā Ā Ā Ā Ā Ā Ā Ā Ā // Length of the array arr1[]Ā Ā Ā Ā int M = arr1.Length;Ā Ā Ā Ā Ā Ā Ā Ā Ā // Length of the array arr2[]Ā Ā Ā Ā int N = arr2.Length;Ā Ā Ā Ā Ā Ā Ā Ā Ā // Length of the requiredĀ Ā Ā Ā // longest subarrayĀ Ā Ā Ā int maxL = 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā // Initialize DP[]arrayĀ Ā Ā Ā int[,] DP = new int[M + 1, N + 1];Ā
Ā Ā Ā Ā // Traverse array arr1[]Ā Ā Ā Ā for(int i = 1; i <= M; i++) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Traverse array arr2[]Ā Ā Ā Ā Ā Ā Ā Ā for(int j = 1; j <= N; j++) Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr1[i - 1] == arr2[j - 1])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // arr1[i - 1] contributes toĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the length of the subarrayĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i, j] = 1 + DP[i - 1, j - 1];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // OtherwiseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā elseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i, j] = DP[i, j - 1];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Find the maximum valueĀ Ā Ā Ā // present in DP[][]Ā Ā Ā Ā for(int i = 1; i <= M; i++) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā for(int j = 1; j <= N; j++) Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxL = Math.Max(maxL, DP[i, j]);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā // Return the resultĀ Ā Ā Ā return maxL;}Ā
// Driver Codestatic public void Main(){Ā Ā Ā Ā int[] arr1 = { 4, 2, 3, 1, 5, 6 };Ā Ā Ā Ā int[] arr2 = { 3, 1, 4, 6, 5, 2 };Ā Ā Ā Ā Ā Ā Ā Ā Ā // Function call to find the lengthĀ Ā Ā Ā // of the longest required subarrayĀ Ā Ā Ā Console.WriteLine(LongSubarrSeq(arr1, arr2));}}Ā
// This code is contributed by susmitakundugoaldanga |
Javascript
<script>// javascript program of the above approachĀ
Ā Ā Ā Ā // Function to find the length of theĀ Ā Ā Ā // longest subarray in arr1[] whichĀ Ā Ā Ā // is a subsequence in arr2[]Ā Ā Ā Ā function LongSubarrSeq(Ā Ā Ā Ā Ā Ā Ā Ā arr1, arr2)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // Length of the array arr1[]Ā Ā Ā Ā Ā Ā Ā Ā let M = arr1.length;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Length of the array arr2[]Ā Ā Ā Ā Ā Ā Ā Ā let N = arr2.length;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Length of the requiredĀ Ā Ā Ā Ā Ā Ā Ā // longest subarrayĀ Ā Ā Ā Ā Ā Ā Ā let maxL = 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Initialize DP[]arrayĀ Ā Ā Ā Ā Ā Ā Ā let DP = new Array(M + 1);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Loop to create 2D array using 1D arrayĀ Ā Ā Ā Ā Ā Ā Ā for (var i = 0; i < DP.length; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i] = new Array(2);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (var i = 0; i < DP.length; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (var j = 0; j < DP.length; j++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i][j] = 0;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse array arr1[]Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 1; i <= M; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Traverse array arr2[]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let j = 1; j <= N; j++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr1[i - 1] == arr2[j - 1]) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // arr1[i - 1] contributes toĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // the length of the subarrayĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i][j] = 1 + DP[i - 1][j - 1];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // OtherwiseĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[i][j] = DP[i][j - 1];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Find the maximum valueĀ Ā Ā Ā Ā Ā Ā Ā // present in DP[][]Ā Ā Ā Ā Ā Ā Ā Ā for (let i = 1; i <= M; i++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let j = 1; j <= N; j++) {Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxL = Math.max(maxL, DP[i][j]);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return the resultĀ Ā Ā Ā Ā Ā Ā Ā return maxL;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver CodeĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let arr1 = [ 4, 2, 3, 1, 5, 6 ];Ā Ā Ā Ā Ā Ā Ā Ā let arr2 = [ 3, 1, 4, 6, 5, 2 ];Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Function call to find the lengthĀ Ā Ā Ā Ā Ā Ā Ā // of the longest required subarrayĀ Ā Ā Ā Ā Ā Ā Ā document.write(LongSubarrSeq(arr1, arr2));Ā
</script> |
3
Time Complexity: O(M * N)
Auxiliary Space: O(M * N)
Efficient Approach: Space optimization: In the previous approach the current value dp[i][j] only depends upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.
Implementation steps:
- Create a 1D vector dp of size n+1.
- Initialize a variable maxi to store the final answer and update it by iterating through the Dp.
- Set a base case by initializing the values of DP.
- Now iterate over subproblems with the help of a nested loop and get the current value from previous computations.
- Now Create variables prev, temp used to store the previous values from previous computations.
- After every iteration assign the value of temp to temp for further iteration.
- At last return and print the final answer stored in maxi.
Implementation:Ā
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Ā
// Function to find the length of the// longest subarray in arr1[]// which is a subsequence in arr2[]int LongSubarrSeq(int arr1[], int arr2[], int M, int N){Ā
Ā Ā Ā Ā // to store final resultĀ Ā Ā Ā int maxL = 0;Ā
Ā Ā Ā Ā // to store previous computationsĀ Ā Ā Ā // Initialize DP[] vectorĀ Ā Ā Ā vector<int> DP(N + 1, 0);Ā
Ā Ā Ā Ā // iterate over subproblems to get theĀ Ā Ā Ā // current value from previous computationsĀ Ā Ā Ā for (int i = 1; i <= M; i++) {Ā Ā Ā Ā Ā Ā Ā Ā int prev = 0;Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 1; j <= N; j++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Store current DP[j] valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int temp = DP[j];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr1[i - 1] == arr2[j - 1]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[j] = 1 + prev;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxL = max(maxL, DP[j]);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[j] = DP[j - 1];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update previous DP[j] valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā prev = temp;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return final answerĀ Ā Ā Ā return maxL;}Ā
// Driver Codeint main(){Ā Ā Ā Ā int arr1[] = { 4, 2, 3, 1, 5, 6 };Ā Ā Ā Ā int M = sizeof(arr1) / sizeof(arr1[0]);Ā
Ā Ā Ā Ā int arr2[] = { 3, 1, 4, 6, 5, 2 };Ā Ā Ā Ā int N = sizeof(arr2) / sizeof(arr2[0]);Ā
Ā Ā Ā Ā cout << LongSubarrSeq(arr1, arr2, M, N);Ā Ā Ā Ā return 0;} |
Java
// Java program for the above approachimport java.util.*;Ā
public class Main {Ā Ā // Function to find the length of the// longest subarray in arr1[]// which is a subsequence in arr2[]static int LongSubarrSeq(int arr1[], int arr2[], int M, int N){Ā
Ā Ā Ā Ā // to store final resultĀ Ā Ā Ā int maxL = 0;Ā
Ā Ā Ā Ā // to store previous computationsĀ Ā Ā Ā // Initialize DP[] vectorĀ Ā Ā Ā int DP[] = new int[N + 1];Ā
Ā Ā Ā Ā Arrays.fill(DP, 0);Ā
Ā Ā Ā Ā // iterate over subproblems to get theĀ Ā Ā Ā // current value from previous computationsĀ Ā Ā Ā for (int i = 1; i <= M; i++) {Ā Ā Ā Ā Ā Ā Ā Ā int prev = 0;Ā Ā Ā Ā Ā Ā Ā Ā for (int j = 1; j <= N; j++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Store current DP[j] valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int temp = DP[j];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (arr1[i - 1] == arr2[j - 1]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[j] = 1 + prev;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxL = Math.max(maxL, DP[j]);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[j] = DP[j - 1];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update previous DP[j] valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā prev = temp;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Return final answerĀ Ā Ā Ā return maxL;}Ā
// Driver Codepublic static void main(String[] args){Ā Ā Ā Ā int arr1[] = { 4, 2, 3, 1, 5, 6 };Ā Ā Ā Ā int M = arr1.length;Ā
Ā Ā Ā Ā int arr2[] = { 3, 1, 4, 6, 5, 2 };Ā Ā Ā Ā int N = arr2.length;Ā
Ā Ā Ā Ā System.out.print(LongSubarrSeq(arr1, arr2, M, N));}} |
Python3
# Function to find the length of the# longest subarray in arr1[]# which is a subsequence in arr2[]def LongSubarrSeq(arr1, arr2, M, N):Ā Ā Ā Ā # To store the final resultĀ Ā Ā Ā maxL = 0Ā Ā Ā Ā Ā Ā Ā Ā Ā # To store previous computationsĀ Ā Ā Ā # Initialize DP[] listĀ Ā Ā Ā DP = [0] * (N + 1)Ā
Ā Ā Ā Ā # Iterate over subproblems to get theĀ Ā Ā Ā # current value from previous computationsĀ Ā Ā Ā for i in range(1, M + 1):Ā Ā Ā Ā Ā Ā Ā Ā prev = 0Ā Ā Ā Ā Ā Ā Ā Ā for j in range(1, N + 1):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Store current DP[j] valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = DP[j]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if arr1[i - 1] == arr2[j - 1]:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[j] = 1 + prevĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā maxL = max(maxL, DP[j])Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā DP[j] = DP[j - 1]Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Update previous DP[j] valueĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā prev = tempĀ
Ā Ā Ā Ā # Return the final answerĀ Ā Ā Ā return maxLĀ
# Driver Codeif __name__ == "__main__":Ā Ā Ā Ā arr1 = [4, 2, 3, 1, 5, 6]Ā Ā Ā Ā M = len(arr1)Ā
Ā Ā Ā Ā arr2 = [3, 1, 4, 6, 5, 2]Ā Ā Ā Ā N = len(arr2)Ā
Ā Ā Ā Ā print(LongSubarrSeq(arr1, arr2, M, N)) |
3
Time Complexity: O(M * N)
Auxiliary Space: O(N)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


