给定两个数组arr1[]和arr2[] ,大小均为N和整数X ,任务是检查在重新排列第二个数组后,对应索引处的两个数组的相同索引元素的总和是否最多为 K大批。如果可能,则打印“是”,否则打印“否” 。
例子:
Input: arr1[] = {1, 2, 3}, arr2[] = {1, 1, 2}, X = 4
Output: Yes
Explanation:
Rearranging the array B[] as {1, 2, 1}. Now the sum of corresponding indices are:
A[0] + B[0] = 1 + 1 = 2 ≤ 4
A[1] + B[1] = 2 + 2 = 4 ≤ 4
A[2] + B[2] = 3 + 1 = 4 ≤ 4
Input: arr1[] = {1, 2, 3, 4}, arr2[] = {1, 2, 3, 4}, X = 4
Output: No
Explanation: There is no way that the array B[] can be rearranged such that the condition A[i] + B[i] <= X is satisfied.
朴素方法:最简单的方法是生成数组B[] 的所有可能排列,如果任何排列满足给定条件,则打印Yes 。否则,打印No 。
时间复杂度: O(N!)
辅助空间: O(1)
高效的方法:为了优化上述方法,想法是使用排序。请按照以下步骤解决问题:
- 按升序对数组arr1[]进行排序,按降序对arr2[]进行排序。
- 现在,同时遍历两个数组,如果存在任何对使得arr1[i]和arr2[]的总和超过X ,则打印“No” 。否则,打印“是” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if elements
// of B[] can be rearranged
// such that A[i] + B[i] <= X
void rearrange(int A[], int B[],
int N, int X)
{
// Checks the given condition
bool flag = true;
// Sort A[] in ascending order
sort(A, A + N);
// Sort B[] in descending order
sort(B, B + N, greater());
// Traverse the arrays A[] and B[]
for (int i = 0; i < N; i++) {
// If A[i] + B[i] exceeds X
if (A[i] + B[i] > X) {
// Rearrangement not possible,
// set flag to false
flag = false;
break;
}
}
// If flag is true
if (flag)
cout << "Yes";
// Otherwise
else
cout << "No";
}
// Driver Code
int main()
{
int A[] = { 1, 2, 3 };
int B[] = { 1, 1, 2 };
int X = 4;
int N = sizeof(A) / sizeof(A[0]);
// Function Call
rearrange(A, B, N, X);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to check if elements
// of B[] can be rearranged
// such that A[i] + B[i] <= X
static void rearrange(int A[], int B[],
int N, int X)
{
// Checks the given condition
boolean flag = true;
// Sort A[] in ascending order
Arrays.sort(A);
// Sort B[] in descending order
Arrays.sort(B);
// Traverse the arrays A[] and B[]
for (int i = 0; i < N; i++)
{
// If A[i] + B[i] exceeds X
if (A[i] + B[N - 1 - i] > X)
{
// Rearrangement not possible,
// set flag to false
flag = false;
break;
}
}
// If flag is true
if (flag == true)
System.out.print("Yes");
// Otherwise
else
System.out.print("No");
}
// Driver Code
public static void main (String[] args)
{
int A[] = { 1, 2, 3 };
int B[] = { 1, 1, 2 };
int X = 4;
int N = A.length;
// Function Call
rearrange(A, B, N, X);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to check if elements
# of B can be rearranged
# such that A[i] + B[i] <= X
def rearrange(A, B, N, X):
# Checks the given condition
flag = True
# Sort A in ascending order
A = sorted(A)
# Sort B in descending order
B = sorted(B)[::-1]
# Traverse the arrays A and B
for i in range(N):
# If A[i] + B[i] exceeds X
if (A[i] + B[i] > X):
# Rearrangement not possible,
# set flag to false
flag = False
break
# If flag is true
if (flag):
print("Yes")
# Otherwise
else:
print("No")
# Driver Code
if __name__ == '__main__':
A = [ 1, 2, 3 ]
B = [ 1, 1, 2 ]
X = 4
N = len(A)
# Function Call
rearrange(A, B, N, X)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to check if elements
// of B[] can be rearranged
// such that A[i] + B[i] <= X
static void rearrange(int[] A, int[] B,
int N, int X)
{
// Checks the given condition
bool flag = true;
// Sort A[] in ascending order
Array.Sort(A);
// Sort B[] in descending order
Array.Sort(B);
// Traverse the arrays A[] and B[]
for (int i = 0; i < N; i++)
{
// If A[i] + B[i] exceeds X
if (A[i] + B[N - 1 - i] > X)
{
// Rearrangement not possible,
// set flag to false
flag = false;
break;
}
}
// If flag is true
if (flag == true)
Console.WriteLine("Yes");
// Otherwise
else
Console.WriteLine("No");
}
// Driver Code
public static void Main()
{
int []A = { 1, 2, 3 };
int []B = { 1, 1, 2 };
int X = 4;
int N = A.Length;
// Function Call
rearrange(A, B, N, X);
}
}
// This code is contributed by AnkThon
Javascript
Yes
时间复杂度: O(N*log N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。