给定两个数组,我们必须检查是否可以通过交换A [i]和B [i]以严格的升序对两个数组进行排序。
例子:
Input : A[ ]={ 1, 4, 3, 5, 7}, B[ ]={ 2, 2, 5, 8, 9}
Output : True
After swapping A[1] and B[1], both the arrays are sorted.
Input : A[ ]={ 1, 4, 5, 5, 7}, B[ ]={ 2, 2, 5, 8, 9}
Output : False
It is not possible to make both the arrays sorted with any number of swaps.
我们得到了两个数组,我们可以将A [i]与B [i]交换,以便我们可以严格按照升序对两个数组进行排序,因此必须对数组进行排序,以使A [i] 我们将使用贪婪的方法来解决问题。
我们将获得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],我们将无法使用b [i]切换a [i],因为b [i]总是小于a [i]。现在让我们假设a [i + 1]不大于a [i],因此我们可以将b [i]的a [i]切换为a [i]> b [i],但当a [i]> b时[i]和a [i + 1]> b [i + 1]且a [i]> a [i + 1],因此a [i]永远不会小于b [i + 1],因此不可能转变。我们可以类似地证明b [i]。
因此,证明了当输出为“是”时,可能有更多可能的组合来排列数组,但是当输出为“否”时,则没有根据约束条件来排列数组的方式。
下面是上述方法的实现:
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
True
时间复杂度: O(N)