给定两个大小分别为N和M 的数组arr1[]和arr2[] ,任务是计算两个数组之间所需的最小交换次数,以使数组arr1 [] 的总和大于arr2 [] .
例子:
Input: arr1[] = {1, 3, 2, 4}, arr2[] = {6, 7, 8}
Output: 1
Explanation:
Swapping arr1[0] with arr2[2] makes the sum of arr1[] equal to 17, which is greater than 14.
Therefore, the count of swaps required is 1.
Input: arr1[] = {2, 2}, arr2[] = {5, 5, 5}
Output: 2
方法:给定的问题可以通过对两个数组进行排序并执行交换以最大化arr1[]的总和来解决。请按照以下步骤解决问题:
- 对两个数组进行排序并分别计算数组的总和为sum1和sum2 。
- 将变量count初始化为0以存储所需的交换次数,将j初始化为(M – 1)以指向数组arr2[]的最后一个元素。
- 使用变量i遍历数组arr1[]并执行以下步骤:
- 检查sum1是否小于或等于sum2 ,然后将当前元素arr[i]与元素arr2[j]交换以最大化sum1 。
- 交换后,更新sum1 、 sum2的值,并递减j以指向倒数第二个元素并递增count 。
- 完成上述步骤后,打印count的值作为所需的最小交换次数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum count
// of swaps required between the two
// arrays to makee the sum of arr1[]
// greater than that of arr2[]
int maximumCount(int arr1[], int arr2[],
int s1, int s2)
{
// Stores the sum of the two arrays
int sum1 = 0, sum2 = 0;
// Calculate sum of arr1[]
for (int i = 0; i < s1; i++) {
sum1 += arr1[i];
}
// Calculate sum of arr2[]
for (int j = 0; j < s2; j++) {
sum2 += arr2[j];
}
int len = 0;
if (s1 >= s2) {
len = s2;
}
else {
len = s1;
}
// Sort the arrays arr1[] and arr2[]
sort(arr1, arr1 + s1);
sort(arr2, arr2 + s2);
int j = 0, k = s2 - 1, count = 0;
// Traverse the array arr[]
for (int i = 0; i < len; i++) {
// If the sum1 is less than
// or equal to sum2
if (sum1 <= sum2) {
// Swapping the elements
if (arr2[k] >= arr1[i]) {
// Update the sum1 and sum2
int dif1 = arr1[j], dif2 = arr2[k];
sum1 -= dif1;
sum1 += dif2;
sum2 -= dif2;
sum2 += dif1;
j++;
k--;
// Increment the count
count++;
}
else {
break;
}
}
else {
break;
}
}
// Return the final count
return count;
}
// Driver Code
int main()
{
int arr1[] = { 1, 3, 2, 4 };
int arr2[] = { 6, 7, 8 };
int N = sizeof(arr1) / sizeof(arr1[0]);
int M = sizeof(arr2) / sizeof(arr2[0]);
// Function Call
cout << maximumCount(arr1, arr2, N, M);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to find the minimum count
// of swaps required between the two
// arrays to makee the sum of arr1[]
// greater than that of arr2[]
static int maximumCount(int[] arr1, int[] arr2, int s1,
int s2)
{
// Stores the sum of the two arrays
int sum1 = 0, sum2 = 0;
// Calculate sum of arr1[]
for (int i = 0; i < s1; i++)
{
sum1 += arr1[i];
}
// Calculate sum of arr2[]
for (int j = 0; j < s2; j++)
{
sum2 += arr2[j];
}
int len = 0;
if (s1 >= s2)
{
len = s2;
}
else
{
len = s1;
}
// Sort the arrays arr1[] and arr2[]
Arrays.sort(arr1);
Arrays.sort(arr2);
int j = 0, k = s2 - 1, count = 0;
// Traverse the array arr[]
for (int i = 0; i < len; i++)
{
// If the sum1 is less than
// or equal to sum2
if (sum1 <= sum2)
{
// Swapping the elements
if (arr2[k] >= arr1[i])
{
// Update the sum1 and sum2
int dif1 = arr1[j], dif2 = arr2[k];
sum1 -= dif1;
sum1 += dif2;
sum2 -= dif2;
sum2 += dif1;
j++;
k--;
// Increment the count
count++;
}
else
{
break;
}
}
else
{
break;
}
}
// Return the final count
return count;
}
// Driver Code
public static void main(String[] args)
{
int[] arr1 = new int[] { 1, 3, 2, 4 };
int[] arr2 = new int[] { 6, 7, 8 };
int N = arr1.length;
int M = arr2.length;
// Function Call
System.out.println(maximumCount(arr1, arr2, N, M));
}
}
// This code is contributed by dharanendralv23
Python3
# Python3 program to implement
# the above approach
# Function to find the minimum count
# of swaps required between the two
# arrays to makee the sum of arr1[]
# greater than that of arr2[]
def maximumCount(arr1, arr2, s1, s2) :
# Stores the sum of the two arrays
sum1 = 0
sum2 = 0
# Calculate sum of arr1[]
for i in range(s1):
sum1 += arr1[i]
# Calculate sum of arr2[]
for j in range(s2):
sum2 += arr2[j]
len = 0
if (s1 >= s2) :
lenn = s2
else :
lenn = s1
# Sort the arrays arr1[] and arr2[]
arr1.sort();
arr2.sort();
j = 0
k = s2 - 1
count = 0
# Traverse the array arr[]
for i in range(lenn):
# If the sum1 is less than
# or equal to sum2
if (sum1 <= sum2) :
# Swapping the elements
if (arr2[k] >= arr1[i]) :
# Update the sum1 and sum2
dif1 = arr1[j]
dif2 = arr2[k]
sum1 -= dif1
sum1 += dif2
sum2 -= dif2
sum2 += dif1
j += 1
k -= 1
# Increment the count
count += 1
else :
break
else :
break
# Return the final count
return count
# Driver Code
arr1 = [ 1, 3, 2, 4 ]
arr2 = [ 6, 7, 8 ]
N = len(arr1)
M = len(arr2)
# Function Call
print(maximumCount(arr1, arr2, N, M))
# This code is contributed by sanjoy_62
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the minimum count
// of swaps required between the two
// arrays to makee the sum of arr1[]
// greater than that of arr2[]
static int maximumCount(int[] arr1, int[] arr2, int s1,
int s2)
{
// Stores the sum of the two arrays
int sum1 = 0, sum2 = 0;
// Calculate sum of arr1[]
for (int i = 0; i < s1; i++) {
sum1 += arr1[i];
}
// Calculate sum of arr2[]
for (int a = 0; a < s2; a++) {
sum2 += arr2[a];
}
int len = 0;
if (s1 >= s2) {
len = s2;
}
else {
len = s1;
}
// Sort the arrays arr1[] and arr2[]
Array.Sort(arr1);
Array.Sort(arr2);
int j = 0, k = s2 - 1, count = 0;
// Traverse the array arr[]
for (int i = 0; i < len; i++) {
// If the sum1 is less than
// or equal to sum2
if (sum1 <= sum2) {
// Swapping the elements
if (arr2[k] >= arr1[i]) {
// Update the sum1 and sum2
int dif1 = arr1[j], dif2 = arr2[k];
sum1 -= dif1;
sum1 += dif2;
sum2 -= dif2;
sum2 += dif1;
j++;
k--;
// Increment the count
count++;
}
else {
break;
}
}
else {
break;
}
}
// Return the final count
return count;
}
// Driver Code
static public void Main()
{
int[] arr1 = new int[] { 1, 3, 2, 4 };
int[] arr2 = new int[] { 6, 7, 8 };
int N = arr1.Length;
int M = arr2.Length;
// Function Call
Console.WriteLine(maximumCount(arr1, arr2, N, M));
}
}
// This code is contributed by dharanendralv23
Javascript
输出:
1
时间复杂度: O(N*log N + M*log M)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live