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

📅  最后修改于: 2021-04-22 04:09:43             🧑  作者: 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


输出:
YES

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