📌  相关文章
📜  通过交换给定字符串中包含“1”的索引中的相邻元素来对数组进行排序

📅  最后修改于: 2022-05-13 01:56:08.678000             🧑  作者: Mango

通过交换给定字符串中包含“1”的索引中的相邻元素来对数组进行排序

给定一个大小为N的数组arr[]和一个二进制字符串S ,任务是检查是否可以通过交换相邻数组元素来对数组arr[]进行排序,例如arr[i]arr[i + 1]如果S[i]等于“1”。如果可能,则打印“是”。否则,打印“否”

例子 :

方法:查看数组arr[]中的某些对(i, j)使得i < j和初始arr[i] > arr[j] 。这意味着应该允许从ij - 1的所有交换。然后很容易注意到只检查 i 和 i + 1 就足够了,因为可以从中扣除任何其他对。请按照以下步骤解决问题:

  • 迭代范围[0, N – 1]并执行以下步骤:
    • 如果S[i]'1',则初始化一个变量,比如j,使其等于i。
    • 迭代范围 直到s[j]等于 ' 1'并且j小于字符串s的长度。将j的值增加1
    • 将数组arr[]中的子数组从i排序到j+1。
  • 迭代范围[0, N – 2]并执行以下步骤:
    • 如果arr[i]的值大于arr[i + 1],则打印No并中断循环并返回。
  • 打印Yes ,因为数组是通过交换允许的索引进行排序的。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if it is possible
// to sort the array arr[] by swapping
// array elements from indices containing
// adjacent pairs of 1s in the string s
void checkIfPossibleToSort(int n, int arr[],
                           string s)
{
    // Sort the parts of array
    // where swaps are allowed
    for (int i = 0; i < n - 1; i++) {
 
        if (s[i] == '1') {
            int j = i;
 
            // Iterate over the range
            // till s[j] is equal to '1'
            while (s[j] == '1') {
                j++;
            }
 
            // Sort the subarray
            sort(arr + i, arr + j + 1);
 
            i = j;
        }
    }
 
    // Check if the array remains unsorted
    for (int i = 0; i < n - 1; i++) {
 
        // If found to be true, then it is
        // not possible to sort the array
        if (arr[i] > arr[i + 1]) {
            printf("No\n");
            return;
        }
    }
 
    printf("Yes\n");
}
 
// Driver Code
int main()
{
    // Given Input
    int n = 6;
    int arr[] = { 2, 5, 3, 4, 6 };
    string s = "01110";
 
    // Function Call
    checkIfPossibleToSort(n, arr, s);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.Arrays;
 
class GFG{
 
// Function to check if it is possible
// to sort the array arr[] by swapping
// array elements from indices containing
// adjacent pairs of 1s in the string s
public static void checkIfPossibleToSort(int n, int arr[],
                                         String s)
{
     
    // Sort the parts of array
    // where swaps are allowed
    for(int i = 0; i < n - 1; i++)
    {
        if (s.charAt(i) == '1')
        {
            int j = i;
 
            // Iterate over the range
            // till s[j] is equal to '1'
            while (s.charAt(j) == '1')
            {
                j++;
            }
 
            // Sort the subarray
            Arrays.sort(arr, i, j);
 
            i = j;
        }
    }
 
    // Check if the array remains unsorted
    for(int i = 0; i < n - 2; i++)
    {
         
        // If found to be true, then it is
        // not possible to sort the array
        if (arr[i] > arr[i + 1])
        {
            System.out.println("No");
            return;
        }
    }
    System.out.println("Yes");
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given Input
    int n = 6;
    int arr[] = { 2, 5, 3, 4, 6 };
    String s = "01110";
 
    // Function Call
    checkIfPossibleToSort(n, arr, s);
}
}
 
// This code is contributed by gfgking


Python3
# Python3 program for the above approach
 
# Function to check if it is possible
# to sort the array arr[] by swapping
# array elements from indices containing
# adjacent pairs of 1s in the string s
def checkIfPossibleToSort(n, arr, s):
   
   # Sort the parts of array
    # where swaps are allowed
    for i in range(n-1):
        if s[i] == '1':
            j = i
             
            # Iterate over the range
            # till s[j] is equal to '1'
            while s[j] == '1':
                j += 1
                 
                # sort the array
            arr = arr[:i] + sorted(arr[i:j+1]) + arr[j+1:]
            i = j
             
     # Check if the array remains unsorted
    for i in range(n-2):
       
      # If found to be true, then it is
       # not possible to sort the array
        if arr[i] > arr[i+1]:
            print("No")
            return
    print("Yes")
 
 
    # Driver code
     
    # Given input
n = 6
arr = [2, 5, 3, 4, 6]
s = "01110"
 
# function call
checkIfPossibleToSort(n, arr, s)
 
# This code is contributed by Parth Manchanda


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if it is possible
// to sort the array arr[] by swapping
// array elements from indices containing
// adjacent pairs of 1s in the string s
public static void checkIfPossibleToSort(int n, int []arr, String s)
{
     
    // Sort the parts of array
    // where swaps are allowed
    for(int i = 0; i < n - 1; i++)
    {
        if (s[i] == '1')
        {
            int j = i;
 
            // Iterate over the range
            // till s[j] is equal to '1'
            while (s[j] == '1')
            {
                j++;
            }
 
            // Sort the subarray
            Array.Sort(arr, i, j);
 
            i = j;
        }
    }
 
    // Check if the array remains unsorted
    for(int i = 0; i < n - 2; i++)
    {
         
        // If found to be true, then it is
        // not possible to sort the array
        if (arr[i] > arr[i + 1])
        {
            Console.Write("No");
            return;
        }
    }
    Console.Write("Yes");
}
 
// Driver Code
public static void Main(String []args)
{
     
    // Given Input
    int n = 6;
    int []arr = { 2, 5, 3, 4, 6 };
    String s = "01110";
 
    // Function Call
    checkIfPossibleToSort(n, arr, s);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
Yes

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