📌  相关文章
📜  检查删除非相邻元素的子序列是否使数组排序

📅  最后修改于: 2021-10-26 06:03:37             🧑  作者: Mango

给定一个大小为N的二进制数组arr[] ,任务是检查数组arr[] 是否可以通过删除非相邻数组元素的任何子序列来排序。如果数组可以排序,则打印“Yes” 。否则,打印“否”

例子:

朴素方法:解决给定问题的最简单方法是生成非相邻元素的所有可能子序列,如果存在任何子序列,其删除对给定数组进行排序,则打印“是” 。否则,打印“否”

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

高效方法:上述方法还可以基于观察该阵列不能如果和仅排序当存在两个连续的1秒存在于相邻的索引ii + 1和两个连续的0存在于相邻当优化索引jj + 1使得(j > i) 。请按照以下步骤解决问题:

  • 初始化一个变量,比如idx-1 ,如果数组中有两个连续的1则存储索引。
  • 遍历给定的数组,如果数组中存在任何两个连续的1 ,则将该索引存储在变量idx 中并退出循环。否则,从数组中删除所有1并使数组排序,并打印“是”
  • 如果idx 的值为“-1” ,则打印“Yes”,因为总是从数组中删除所有1使数组排序。
  • 否则,从索引idx再次遍历数组,如果存在两个连续的0 ,则打印并跳出循环。否则,从数组中删除所有0并使数组排序,并打印“是”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if it is possible
// to sort the array or not
void isPossibleToSort(int arr[], int N)
{
    // Stores the index if there are two
    // consecutive 1's in the array
    int idx = -1;
 
    // Traverse the given array
    for (int i = 1; i < N; i++) {
 
        // Check adjacent same elements
        // having values 1s
        if (arr[i] == 1
            && arr[i - 1] == 1) {
            idx = i;
            break;
        }
    }
 
    // If there are no two consecutive
    // 1s, then always remove all the
    // 1s from array & make it sorted
    if (idx == -1) {
        cout << "YES";
        return;
    }
 
    for (int i = idx + 1; i < N; i++) {
 
        // If two consecutive 0's are
        // present after two conseutive
        // 1's then array can't be sorted
        if (arr[i] == 0 && arr[i - 1] == 0) {
            cout << "NO";
            return;
        }
    }
 
    // Otherwise, print Yes
    cout << "YES";
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
    isPossibleToSort(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to check if it is possible
    // to sort the array or not
    static void isPossibleToSort(int arr[], int N)
    {
        // Stores the index if there are two
        // consecutive 1's in the array
        int idx = -1;
 
        // Traverse the given array
        for (int i = 1; i < N; i++) {
 
            // Check adjacent same elements
            // having values 1s
            if (arr[i] == 1 && arr[i - 1] == 1) {
                idx = i;
                break;
            }
        }
 
        // If there are no two consecutive
        // 1s, then always remove all the
        // 1s from array & make it sorted
        if (idx == -1) {
            System.out.println("YES");
            return;
        }
 
        for (int i = idx + 1; i < N; i++) {
 
            // If two consecutive 0's are
            // present after two conseutive
            // 1's then array can't be sorted
            if (arr[i] == 0 && arr[i - 1] == 0) {
                System.out.println("NO");
                return;
            }
        }
 
        // Otherwise, print Yes
        System.out.println("YES");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
        int N = arr.length;
        isPossibleToSort(arr, N);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function to check if it is possible
# to sort the array or not
def isPossibleToSort(arr, N):
     
    # Stores the index if there are two
    # consecutive 1's in the array
    idx = -1
 
    # Traverse the given array
    for i in range(1, N):
         
        # Check adjacent same elements
        # having values 1s
        if (arr[i] == 1 and arr[i - 1] == 1):
            idx = i
            break
 
    # If there are no two consecutive
    # 1s, then always remove all the
    # 1s from array & make it sorted
    if (idx == -1):
        print("YES")
        return
 
    for i in range(idx + 1, N, 1):
         
        # If two consecutive 0's are
        # present after two conseutive
        # 1's then array can't be sorted
        if (arr[i] == 0 and arr[i - 1] == 0):
            print("NO")
            return
 
    # Otherwise, print Yes
    print("YES")
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 0, 1, 0, 1, 1, 0 ]
    N = len(arr)
     
    isPossibleToSort(arr, N)
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# for the above approach
using System.IO;
using System;
 
class GFG{
     
// Function to check if it is possible
// to sort the array or not
static void isPossibleToSort(int[] arr, int N)
{
     
    // Stores the index if there are two
    // consecutive 1's in the array
    int idx = -1;
 
    // Traverse the given array
    for(int i = 1; i < N; i++)
    {
         
        // Check adjacent same elements
        // having values 1s
        if (arr[i] == 1 && arr[i - 1] == 1)
        {
            idx = i;
            break;
        }
    }
 
    // If there are no two consecutive
    // 1s, then always remove all the
    // 1s from array & make it sorted
    if (idx == -1)
    {
        Console.WriteLine("YES");
        return;
    }
 
    for(int i = idx + 1; i < N; i++)
    {
         
        // If two consecutive 0's are
        // present after two conseutive
        // 1's then array can't be sorted
        if (arr[i] == 0 && arr[i - 1] == 0)
        {
            Console.WriteLine("NO");
            return;
        }
    }
 
    // Otherwise, print Yes
    Console.WriteLine("YES");
}
 
// Driver code
static void Main()
{
    int[] arr = { 1, 0, 1, 0, 1, 1, 0 };
    int N = arr.Length;
     
    isPossibleToSort(arr, N);
}
}
 
// This code is contributed by abhinavjain194


Javascript


输出:
YES

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程