📌  相关文章
📜  检查与给定子数组相同的非连续子序列是否存在

📅  最后修改于: 2021-09-07 02:25:55             🧑  作者: Mango

给定一个由N 个整数和两个整数值LR 组成的数组arr[] ,指示子数组的开始和结束索引,任务是检查是否存在与给定子数组相同的非连续子序列.如果发现是真的,打印“是” 。否则,打印“否”

例子:

朴素方法:最简单的方法是生成给定数组的所有可能子序列,并检查生成的任何子序列是否等于给定子数组。如果发现是真的,则打印“是” 。否则,打印“否”

时间复杂度: O(N*2 N )
辅助空间: O(1)

高效方法:为了优化上述方法,该想法基于以下关键观察:如果给定子数组的第一个元素在[0, L – 1]并且在[R + 1, N]范围内至少出现一次子数组的最后一个元素。

逻辑证明:

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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 prif 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


输出:
YES

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live