Check if a non-contiguous subsequence same as the given subarray exists or not
Last Updated :
04 Mar, 2022
Given an array arr[] consisting of N integers and two integer values L and R, indicating the starting and ending indices of a subarray, the task is to check if there exists a non-contiguous subsequence which is same as the given subarray or not. If found to be true, print "Yes". Otherwise, print "No".
A non-contiguous subsequence contains at least two consecutive characters from non-consecutive indices.
Examples:
Input: arr[] = {1, 7, 12, 1, 7, 5, 10, 11, 42}, L = 3, R = 6
Output: Yes
Explanation: The non-contiguous subsequence {arr[0], arr[1], arr[5], arr[6]} is same as the subarray {arr[3], .., arr[6]}.
Input: arr[] = {0, 1, 2, -2, 5, 10}, L = 1, R = 3
Naive Approach: The simplest approach is to generate all possible subsequences of the given array and check if any subsequence generated is equal to the given subarray or not. If found to be true, then print "Yes". Otherwise, print "No".
Time Complexity: O(N*2N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the key observation that there will always be a non-contiguous subsequence if there is at least one occurrence of the first element of the given subarray in the range [0, L - 1] and at least one occurrence of the last element of a subarray in the range [R + 1, N].
Proof of Logic:
If the 1st element of the subarray {arr[L], ... arr[R]} also occurs at any index K (K < L), then one such non-contiguous subsequence can be {arr[K], arr[L + 1], ...., arr[R]}.
If the last element of the subarray {arr[L], ... arr[R]} also occurs at any index K (K > R), then one such non-contiguous subsequence can be strong>{arr[L], arr[L + 1], ...., arr[R - 1], arr[K]}.
If none of the above two conditions are satisfied, then no such non-contiguous subsequence exists.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Utility Function to check whether
// a subsequence same as the given
// subarray exists or not
bool checkSubsequenceUtil(
int arr[], int L, int R, int N)
{
// Check if first element of the
// subarray is also present before
for (int i = 0; i < L; i++)
if (arr[i] == arr[L])
return true;
// Check if last element of the
// subarray is also present later
for (int i = R + 1; i < N; i++)
if (arr[i] == arr[R])
return true;
// If above two conditions are
// not satisfied, then no such
// subsequence exists
return false;
}
// Function to check and print if a
// subsequence which is same as the
// given subarray is present or not
void checkSubsequence(int arr[], int L,
int R, int N)
{
if (checkSubsequenceUtil(arr, L,
R, N)) {
cout << "YES\n";
}
else {
cout << "NO\n";
}
}
// Driver Code
int main()
{
int arr[] = { 1, 7, 12, 1, 7,
5, 10, 11, 42 };
int N = sizeof(arr) / sizeof(arr[0]);
int L = 3, R = 6;
// Function Call
checkSubsequence(arr, L, R, N);
}
Java
// Java program for the above approach
class GFG{
// Utility Function to check whether
// a subsequence same as the given
// subarray exists or not
static boolean checkSubsequenceUtil(int arr[], int L,
int R, int N)
{
// Check if first element of the
// subarray is also present before
for(int i = 0; i < L; i++)
if (arr[i] == arr[L])
return true;
// Check if last element of the
// subarray is also present later
for(int i = R + 1; i < N; i++)
if (arr[i] == arr[R])
return true;
// If above two conditions are
// not satisfied, then no such
// subsequence exists
return false;
}
// Function to check and print if a
// subsequence which is same as the
// given subarray is present or not
static void checkSubsequence(int arr[], int L,
int R, int N)
{
if (checkSubsequenceUtil(arr, L,
R, N))
{
System.out.print("YES\n");
}
else
{
System.out.print("NO\n");
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 7, 12, 1, 7,
5, 10, 11, 42 };
int N = arr.length;
int L = 3, R = 6;
// Function Call
checkSubsequence(arr, L, R, N);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Utility Function to check whether
# a subsequence same as the given
# subarray exists or not
def checkSubsequenceUtil(arr, L, R, N):
# Check if first element of the
# subarray is also present before
for i in range(L):
if (arr[i] == arr[L]):
return True
# Check if last element of the
# subarray is also present later
for i in range(R + 1, N, 1):
if (arr[i] == arr[R]):
return True
# If above two conditions are
# not satisfied, then no such
# subsequence exists
return False
# Function to check and print if a
# subsequence which is same as the
# given subarray is present or not
def checkSubsequence(arr, L, R, N):
if (checkSubsequenceUtil(arr, L,R, N)):
print("YES")
else:
print("NO")
# Driver Code
arr = [ 1, 7, 12, 1, 7,
5, 10, 11, 42 ]
N = len(arr)
L = 3
R = 6
# Function Call
checkSubsequence(arr, L, R, N)
# This code is contributed by susmitakundugoaldanga
C#
// C# program for the above approach
using System;
class GFG{
// Utility Function to check whether
// a subsequence same as the given
// subarray exists or not
static bool checkSubsequenceUtil(int[] arr, int L,
int R, int N)
{
// Check if first element of the
// subarray is also present before
for(int i = 0; i < L; i++)
if (arr[i] == arr[L])
return true;
// Check if last element of the
// subarray is also present later
for(int i = R + 1; i < N; i++)
if (arr[i] == arr[R])
return true;
// If above two conditions are
// not satisfied, then no such
// subsequence exists
return false;
}
// Function to check and print if a
// subsequence which is same as the
// given subarray is present or not
static void checkSubsequence(int[] arr, int L,
int R, int N)
{
if (checkSubsequenceUtil(arr, L,
R, N))
{
Console.Write("YES\n");
}
else
{
Console.Write("NO\n");
}
}
// Driver code
public static void Main()
{
int[] arr = { 1, 7, 12, 1, 7,
5, 10, 11, 42 };
int N = arr.Length;
int L = 3, R = 6;
// Function Call
checkSubsequence(arr, L, R, N);
}
}
// This code is contributed by code_hunt
JavaScript
<script>
// javascript program to implement
// the above approach
// Utility Function to check whether
// a subsequence same as the given
// subarray exists or not
function checkSubsequenceUtil(arr, L,
R, N)
{
// Check if first element of the
// subarray is also present before
for(let i = 0; i < L; i++)
if (arr[i] == arr[L])
return true;
// Check if last element of the
// subarray is also present later
for(let i = R + 1; i < N; i++)
if (arr[i] == arr[R])
return true;
// If above two conditions are
// not satisfied, then no such
// subsequence exists
return false;
}
// Function to check and print if a
// subsequence which is same as the
// given subarray is present or not
function checkSubsequence(arr, L,
R, N)
{
if (checkSubsequenceUtil(arr, L,
R, N))
{
document.write("YES\n");
}
else
{
document.write("NO\n");
}
}
// Driver code
let arr = [ 1, 7, 12, 1, 7,
5, 10, 11, 42 ];
let N = arr.length;
let L = 3, R = 6;
// Function Call
checkSubsequence(arr, L, R, N);
// This code is contributed by souravghosh0416.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Length of the longest increasing subsequence which does not contain a given sequence as Subarray Given two arrays arr[] and arr1[] of lengths N and M respectively, the task is to find the longest increasing subsequence of array arr[] such that it does not contain array arr1[] as subarray. Examples: Input: arr[] = {5, 3, 9, 3, 4, 7}, arr1[] = {3, 3, 7}Output: 4Explanation: Required longest incre
14 min read
Check if there exists any subarray with the given conditions Given two integers N and X. Then the task is to return YES or NO by checking whether there exists a subarray in any permutation of length N such that it contains a subarray, where A*B is equal to the X. Here A and B denote the number of elements in sub-array and the first element of sorted subarray
5 min read
Check if all subarrays contains at least one unique element Given an array arr[] consisting of N integers, the task is to check if all subarrays of the array have at least one unique element in it or not. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: arr[] = {1, 2, 1}Output: YesExplanation:For Subarrays of size 1: {1}, {2}, {
11 min read
Check if two same sub-sequences exist in a string or not Given a string, the task is to check if there exist two equal sub-sequences in the given string. Two sub-sequences are said to be equal if they have the same characters arranged in the same lexicographical order but the position of characters differs from that in the original string. Examples: Input
5 min read
Count of possible subarrays and subsequences using given length of Array Given an integer N which denotes the length of an array, the task is to count the number of subarray and subsequence possible with the given length of the array.Examples: Input: N = 5 Output: Count of subarray = 15 Count of subsequence = 32Input: N = 3 Output: Count of subarray = 6 Count of subseque
3 min read