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++
#include <bits/stdc++.h>
using namespace std;
bool checkSubsequenceUtil(
int arr[], int L, int R, int N)
{
for ( int i = 0; i < L; i++)
if (arr[i] == arr[L])
return true ;
for ( int i = R + 1; i < N; i++)
if (arr[i] == arr[R])
return true ;
return false ;
}
void checkSubsequence( int arr[], int L,
int R, int N)
{
if (checkSubsequenceUtil(arr, L,
R, N)) {
cout << "YES\n" ;
}
else {
cout << "NO\n" ;
}
}
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;
checkSubsequence(arr, L, R, N);
}
|
Java
class GFG{
static boolean checkSubsequenceUtil( int arr[], int L,
int R, int N)
{
for ( int i = 0 ; i < L; i++)
if (arr[i] == arr[L])
return true ;
for ( int i = R + 1 ; i < N; i++)
if (arr[i] == arr[R])
return true ;
return false ;
}
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" );
}
}
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 ;
checkSubsequence(arr, L, R, N);
}
}
|
Python3
def checkSubsequenceUtil(arr, L, R, N):
for i in range (L):
if (arr[i] = = arr[L]):
return True
for i in range (R + 1 , N, 1 ):
if (arr[i] = = arr[R]):
return True
return False
def checkSubsequence(arr, L, R, N):
if (checkSubsequenceUtil(arr, L,R, N)):
print ( "YES" )
else :
print ( "NO" )
arr = [ 1 , 7 , 12 , 1 , 7 ,
5 , 10 , 11 , 42 ]
N = len (arr)
L = 3
R = 6
checkSubsequence(arr, L, R, N)
|
C#
using System;
class GFG{
static bool checkSubsequenceUtil( int [] arr, int L,
int R, int N)
{
for ( int i = 0; i < L; i++)
if (arr[i] == arr[L])
return true ;
for ( int i = R + 1; i < N; i++)
if (arr[i] == arr[R])
return true ;
return false ;
}
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" );
}
}
public static void Main()
{
int [] arr = { 1, 7, 12, 1, 7,
5, 10, 11, 42 };
int N = arr.Length;
int L = 3, R = 6;
checkSubsequence(arr, L, R, N);
}
}
|
Javascript
<script>
function checkSubsequenceUtil(arr, L,
R, N)
{
for (let i = 0; i < L; i++)
if (arr[i] == arr[L])
return true ;
for (let i = R + 1; i < N; i++)
if (arr[i] == arr[R])
return true ;
return false ;
}
function checkSubsequence(arr, L,
R, N)
{
if (checkSubsequenceUtil(arr, L,
R, N))
{
document.write( "YES\n" );
}
else
{
document.write( "NO\n" );
}
}
let arr = [ 1, 7, 12, 1, 7,
5, 10, 11, 42 ];
let N = arr.length;
let L = 3, R = 6;
checkSubsequence(arr, L, R, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
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 geeks!
Last Updated :
04 Mar, 2022
Like Article
Save Article