Check if a non | C ++ program for the above approach ; Utility Function to check whether a subsequence same as the given subarray exists or not ; Check if first element of the subarray is also present before ; Check if last element of the subarray is also present later ; If above two conditions are not satisfied , then no such subsequence exists ; Function to check and print if a subsequence which is same as the given subarray is present or not ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE 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 STRNEWLINE " ; } else { cout << " NO STRNEWLINE " ; } } 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 ) ; }