给定两个大小相等的数组n和一个整数k 。任务是对两个数组进行置换,以使其对应元素的总和大于或等于k,即a [i] + b [i]> = k。如果存在任何这样的排列,则任务打印“是”,否则打印“否”。
例子 :
Input : a[] = {2, 1, 3},
b[] = { 7, 8, 9 },
k = 10.
Output : Yes
Permutation a[] = { 1, 2, 3 } and b[] = { 9, 8, 7 }
satisfied the condition a[i] + b[i] >= K.
Input : a[] = {1, 2, 2, 1},
b[] = { 3, 3, 3, 4 },
k = 5.
Output : No
想法是按升序对一个数组进行排序,对另一个数组按降序进行排序,如果任何索引不满足条件a [i] + b [i]> = K,则打印“否”,否则打印“是”。
如果条件在排序数组上失败,则不存在可以满足不等式的数组排列。证明,
假设sort []以升序排序a [], b sort []以降序排序b []。
通过交换b sort []的任意两个索引i,j来创建新置换b [],
- 情况1: i
排序[j]。
现在,排序[i] + b排序[j]排序[i]> b排序[j] as b []是按降序排序的,我们知道排序[i] + b排序[i] ] - 情况2: i> j,b [i]处的元素现在是b排序[j]。
现在,排序[j] + b排序[i]排序[i]>排序[j] as a []的顺序递增,并且我们知道排序[i] + b排序[i] ] - 情况2: i> j,b [i]处的元素现在是b排序[j]。
下面是这种方法的实现:
C++
// C++ program to check whether permutation of two
// arrays satisfy the condition a[i] + b[i] >= k.
#include
using namespace std;
// Check whether any permutation exists which
// satisfy the condition.
bool isPossible(int a[], int b[], int n, int k)
{
// Sort the array a[] in decreasing order.
sort(a, a + n);
// Sort the array b[] in increasing order.
sort(b, b + n, greater());
// Checking condition on each index.
for (int i = 0; i < n; i++)
if (a[i] + b[i] < k)
return false;
return true;
}
// Driven Program
int main()
{
int a[] = { 2, 1, 3 };
int b[] = { 7, 8, 9 };
int k = 10;
int n = sizeof(a)/sizeof(a[0]);
isPossible(a, b, n, k) ? cout << "Yes" :
cout << "No";
return 0;
}
Java
// Java program to check whether
// permutation of two arrays satisfy
// the condition a[i] + b[i] >= k.
import java.util.*;
class GFG
{
// Check whether any permutation
// exists which satisfy the condition.
static boolean isPossible(Integer a[], int b[],
int n, int k)
{
// Sort the array a[] in decreasing order.
Arrays.sort(a, Collections.reverseOrder());
// Sort the array b[] in increasing order.
Arrays.sort(b);
// Checking condition on each index.
for (int i = 0; i < n; i++)
if (a[i] + b[i] < k)
return false;
return true;
}
// Driver code
public static void main(String[] args) {
Integer a[] = {2, 1, 3};
int b[] = {7, 8, 9};
int k = 10;
int n = a.length;
if (isPossible(a, b, n, k))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to check
# whether permutation of two
# arrays satisfy the condition
# a[i] + b[i] >= k.
# Check whether any
# permutation exists which
# satisfy the condition.
def isPossible(a,b,n,k):
# Sort the array a[]
# in decreasing order.
a.sort(reverse=True)
# Sort the array b[]
# in increasing order.
b.sort()
# Checking condition
# on each index.
for i in range(n):
if (a[i] + b[i] < k):
return False
return True
# Driver code
a = [ 2, 1, 3]
b = [7, 8, 9]
k = 10
n =len(a)
if(isPossible(a, b, n, k)):
print("Yes")
else:
print("No")
# This code is contributed
# by Anant Agarwal.
C#
// C# program to check whether
// permutation of two arrays satisfy
// the condition a[i] + b[i] >= k.
using System;
class GFG
{
// Check whether any permutation
// exists which satisfy the condition.
static bool isPossible(int []a, int []b,
int n, int k)
{
// Sort the array a[]
// in decreasing order.
Array.Sort(a);
// Sort the array b[]
// in increasing order.
Array.Reverse(b);
// Checking condition on each index.
for (int i = 0; i < n; i++)
if (a[i] + b[i] < k)
return false;
return true;
}
// Driver code
public static void Main()
{
int []a = {2, 1, 3};
int []b = {7, 8, 9};
int k = 10;
int n = a.Length;
if (isPossible(a, b, n, k))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by anuj_67.
PHP
= k.
// Check whether any permutation
// exists which satisfy the condition.
function isPossible( $a, $b, $n, $k)
{
// Sort the array a[] in
// decreasing order.
sort($a);
// Sort the array b[] in
// increasing order.
rsort($b);
// Checking condition on each
// index.
for ( $i = 0; $i < $n; $i++)
if ($a[$i] + $b[$i] < $k)
return false;
return true;
}
// Driven Program
$a = array( 2, 1, 3 );
$b = array( 7, 8, 9 );
$k = 10;
$n = count($a);
if(isPossible($a, $b, $n, $k))
echo "Yes" ;
else
echo "No";
// This code is contributed by
// anuj_67.
?>
输出:
Yes
时间复杂度: O(n log n)。