📌  相关文章
📜  检查我们是否可以通过交换 A[i] 和 B[i] 对两个数组进行排序

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

给定两个数组,我们必须通过交换 A[i] 和 B[i] 来检查是否可以严格按升序对两个数组进行排序。

例子:

给定两个数组,我们可以将 A[i] 与 B[i] 交换,以便我们可以严格按照升序对数组进行排序,因此我们必须以 A[i] < A[i] 的方式对数组进行排序+1] 和 B[i] < B[i+1]。
我们将使用贪婪的方法来解决问题。
我们将获得 A[i] 和 B[i] 的最小值和最大值,并将最小值分配给 B[i],将最大值分配给 A[i]。
现在,我们将检查数组 A 和数组 B 是否严格递增。
让我们考虑一下我们的做法是错误的(有可能安排但我们的做法是错误的),这意味着任何一个或多个位置都被转换了。

这意味着 a[i-1] 不小于 a[i] 或 a[i+1] 不大于 a[i] 。现在,如果 a[i] 不大于 a[i-1],我们不能将 a[i] 与 b[i] 切换,因为 b[i] 总是小于 a[i]。现在让我们取 a[i+1] 不大于 a[i],所以我们可以将 a[i] 与 b[i] 切换为 a[i] > b[i],但是作为 a[i] > b [i] and a[i+1]> b[i+1] and a[i]>a[i+1] 所以 a[i] 永远不会小于 b[i+1] 所以不可能转变。我们可以类似地证明 b[i]。

因此证明,当输出为YES时,排列数组可能有更多可能的组合,但当输出为NO时,没有可能根据约束排列数组的方法。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to check whether both the array can be
// sorted in (strictly increasing ) ascending order
bool IsSorted(int A[], int B[], int n)
{
    // Traverse through the array
    // and find out the min and max
    // variable at each position
    // make one array of min variables
    // and another of maximum variable
    for (int i = 0; i < n; i++) {
        int x, y;
 
        // Maximum and minimum variable
        x = max(A[i], B[i]);
        y = min(A[i], B[i]);
 
        // Assign min value to
        // B[i] and max value to A[i]
        A[i] = x;
        B[i] = y;
    }
 
    // Now check whether the array is
    // sorted or not
    for (int i = 1; i < n; i++) {
        if (A[i] <= A[i - 1] || B[i] <= B[i - 1])
            return false;
    }
 
    return true;
}
 
// Driver code
int main()
{
    int A[] = { 1, 4, 3, 5, 7 };
    int B[] = { 2, 2, 5, 8, 9 };
    int n = sizeof(A) / sizeof(int);
 
    cout << (IsSorted(A, B, n) ? "True" : "False");
 
    return 0;
}


Java
// Java implementation of above approach
import java.io.*;
 
class GFG
{
         
// Function to check whether both the array can be
// sorted in (strictly increasing ) ascending order
static boolean IsSorted(int []A, int []B, int n)
{
    // Traverse through the array
    // and find out the min and max
    // variable at each position
    // make one array of min variables
    // and another of maximum variable
    for (int i = 0; i < n; i++)
    {
        int x, y;
 
        // Maximum and minimum variable
        x = Math.max(A[i], B[i]);
        y = Math.min(A[i], B[i]);
 
        // Assign min value to
        // B[i] and max value to A[i]
        A[i] = x;
        B[i] = y;
    }
 
    // Now check whether the array is
    // sorted or not
    for (int i = 1; i < n; i++)
    {
        if (A[i] <= A[i - 1] || B[i] <= B[i - 1])
            return false;
    }
 
    return true;
}
 
// Driver code
public static void main (String[] args)
{
 
    int []A = { 1, 4, 3, 5, 7 };
    int []B = { 2, 2, 5, 8, 9 };
    int n = A.length;
 
    if(IsSorted(A, B, n) == true)
    {
        System.out.println("True");
    }
    else
    {
        System.out.println("False");
    }
}
}
 
// This code is contributed by ajit


Python3
# Python3 implementation of above approach
 
# Function to check whether both the array can be
# sorted in (strictly increasing ) ascending order
def IsSorted(A, B, n) :
 
    # Traverse through the array
    # and find out the min and max
    # variable at each position
    # make one array of min variables
    # and another of maximum variable
    for i in range(n) :
         
        # Maximum and minimum variable
        x = max(A[i], B[i]);
        y = min(A[i], B[i]);
 
        # Assign min value to
        # B[i] and max value to A[i]
        A[i] = x;
        B[i] = y;
     
    # Now check whether the array is
    # sorted or not
    for i in range(1, n) :
        if (A[i] <= A[i - 1] or B[i] <= B[i - 1]) :
            return False;
 
    return True;
 
 
# Driver code
if __name__ == "__main__" :
     
    A = [ 1, 4, 3, 5, 7 ];
    B = [ 2, 2, 5, 8, 9 ];
     
    n = len(A);
 
    if (IsSorted(A, B, n)) :
        print(True)
    else :
        print(False)
 
# This code is contributed by AnkitRai01


C#
// C# implementation of above approach
using System;
 
class GFG
{
     
// Function to check whether both the array can be
// sorted in (strictly increasing ) ascending order
static bool IsSorted(int []A, int []B, int n)
{
    // Traverse through the array
    // and find out the min and max
    // variable at each position
    // make one array of min variables
    // and another of maximum variable
    for (int i = 0; i < n; i++) {
        int x, y;
 
        // Maximum and minimum variable
        x = Math.Max(A[i], B[i]);
        y = Math.Min(A[i], B[i]);
 
        // Assign min value to
        // B[i] and max value to A[i]
        A[i] = x;
        B[i] = y;
    }
 
    // Now check whether the array is
    // sorted or not
    for (int i = 1; i < n; i++) {
        if (A[i] <= A[i - 1] || B[i] <= B[i - 1])
            return false;
    }
 
    return true;
}
 
// Driver code
public static void Main()
{
    int []A = { 1, 4, 3, 5, 7 };
    int []B = { 2, 2, 5, 8, 9 };
    int n = A.Length;
 
    if(IsSorted(A, B, n) == true)
    {
        Console.Write("True");
    }
    else
    {
        Console.Write("False");
    }
}
}
 
// This code is contributed
// by Akanksha Rai


Javascript


输出:
True

时间复杂度: O(N)

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