给定两个自然数N1和N2 ,任务是在它们之间交换一位数后找到可能的最大和。
例子:
Input: N1 = 984788, N2 = 706
Output: 988194
Explanation:
Swapping 4 from N1 with 7 from N2, we get N1 = 987788 and N2 = 406
Sum = 988194
Input: N1 = 9987, N2 = 123
Output: 10740
Explanation:
Swapping 8 from N1 with 1 from N2, we get N1 = 9917 and N2 = 823
Sum = 10740
方法:
- 比较N1和N2 ,将两者中较大的数字分别存储在数组arr1中,将较小的数字分别存储在arr2 中。
- 如果两个数字的长度不同,则在arr2 中找到最大元素的索引,在arr1 中找到最重要的索引,并交换它们以最大化总和。
- 如果两个数字的长度相同,
- 同时迭代数组arr1和arr2 。
- 对于两个数组中索引i处的每个数字,找出当前数字与索引“i”左侧的最大数字之间的差值。
- 比较差值,找出最重要的数字和最重要的索引,它们的值需要交换。
- 从arr1和arr2恢复新数字并计算最大化总和。
下面的代码是上述方法的实现:
C++
// C++ program to maximise the sum of two
// Numbers using at most one swap between them
#include
using namespace std;
#define MAX 100
// Function to maximize the sum
// by swapping only one digit
void findMaxSum(int n1, int n2)
{
int arr1[MAX] = { 0 }, arr2[MAX] = { 0 };
int l1 = 0, l2 = 0;
int max1 = max(n1, n2);
int min1 = min(n1, n2);
// Store digits of max(n1, n2)
for (int i = max1; i > 0; i /= 10)
arr1[l1++] = (i % 10);
// Store digits of min(n1, n2)
for (int i = min1; i > 0; i /= 10)
arr2[l2++] = (i % 10);
int f = 0;
// If length of the two numbers
// are unequal
if (l1 != l2) {
// Find the most significant number
// and the most significant index
// for swapping
int index = (max_element(arr2, arr2 + l2) - arr2);
for (int i = l1 - 1; i > (l2 - 1); i--) {
if (arr1[i] < arr2[index]) {
swap(arr1[i], arr2[index]);
f = 1;
break;
}
}
}
// If both numbers are
// of equal length
if (f != 1) {
int index1 = 0, index2 = 0;
int diff1 = 0, diff2 = 0;
for (int i = l2 - 1; i >= 0; i--) {
// Fetch the index of current maximum
// digit present in both the arrays
index1 = (max_element(arr1, arr1 + i) - arr1);
index2 = (max_element(arr2, arr2 + i) - arr2);
// Compute the difference
diff1 = (arr2[index2] - arr1[i]);
diff2 = (arr1[index1] - arr2[i]);
// Find the most significant index
// and the most significant digit
// to be swapped
if (diff1 > 0 || diff2 > 0) {
if (diff1 > diff2) {
swap(arr1[i], arr2[index2]);
break;
}
else if (diff2 > diff1) {
swap(arr2[i], arr1[index1]);
break;
}
else if (diff1 == diff2) {
if (index1 <= index2) {
swap(arr2[i], arr1[index1]);
break;
}
else if (index2 <= index1) {
swap(arr1[i], arr2[index2]);
break;
}
}
}
}
}
// Restore the new numbers
int f_n1 = 0, f_n2 = 0;
for (int i = l1 - 1; i >= 0; i--) {
f_n1 = (f_n1 * 10) + arr1[i];
f_n2 = (f_n2 * 10) + arr2[i];
}
// Display the maximized sum
cout << (f_n1 + f_n2) << "\n";
}
// Driver function
int main()
{
int N1 = 9987;
int N2 = 123;
findMaxSum(N1, N2);
return 0;
}
Java
// Java program to maximise the sum of two
// Numbers using at most one swap between them
import java.util.*;
class GFG{
static int MAX = 100;
static int max_element(int arr[], int pos)
{
int tmp = arr[0];
int ind = 0;
for(int i = 1; i < pos; i++)
{
if (tmp < arr[i])
{
tmp = arr[i];
ind = i;
}
}
return ind;
}
// Function to maximize the sum
// by swapping only one digit
static void findMaxSum(int n1, int n2)
{
int []arr1 = new int[MAX];
int []arr2 = new int[MAX];
int l1 = 0, l2 = 0;
int max1 = Math.max(n1, n2);
int min1 = Math.min(n1, n2);
// Store digits of max(n1, n2)
for(int i = max1; i > 0; i /= 10)
arr1[l1++] = (i % 10);
// Store digits of min(n1, n2)
for(int i = min1; i > 0; i /= 10)
arr2[l2++] = (i % 10);
int f = 0;
// If length of the two numbers
// are unequal
if (l1 != l2)
{
// Find the most significant number
// and the most significant index
// for swapping
int index = (max_element(arr2, l2));
for(int i = l1 - 1; i > (l2 - 1); i--)
{
if (arr1[i] < arr2[index])
{
int tmp = arr1[i];
arr1[i] = arr2[index];
arr2[index] = tmp;
f = 1;
break;
}
}
}
// If both numbers are
// of equal length
if (f != 1)
{
int index1 = 0, index2 = 0;
int diff1 = 0, diff2 = 0;
for(int i = l2 - 1; i >= 0; i--)
{
// Fetch the index of current maximum
// digit present in both the arrays
index1 = (max_element(arr1, i));
index2 = (max_element(arr2, i));
// Compute the difference
diff1 = (arr2[index2] - arr1[i]);
diff2 = (arr1[index1] - arr2[i]);
// Find the most significant index
// and the most significant digit
// to be swapped
if (diff1 > 0 || diff2 > 0)
{
if (diff1 > diff2)
{
int tmp = arr1[i];
arr1[i] = arr2[index2];
arr2[index2] = tmp;
break;
}
else if (diff2 > diff1)
{
int tmp = arr1[index1];
arr1[index1] = arr2[i];
arr2[i] = tmp;
break;
}
else if (diff1 == diff2)
{
if (index1 <= index2)
{
int tmp = arr1[index1];
arr1[index1] = arr2[i];
arr2[i] = tmp;
break;
}
else if (index2 <= index1)
{
int tmp = arr1[i];
arr1[i] = arr2[index2];
arr2[index2] = tmp;
break;
}
}
}
}
}
// Restore the new numbers
int f_n1 = 0, f_n2 = 0;
for(int i = l1 - 1; i >= 0; i--)
{
f_n1 = (f_n1 * 10) + arr1[i];
f_n2 = (f_n2 * 10) + arr2[i];
}
// Display the maximized sum
System.out.println(f_n1 + f_n2);
}
// Driver code
public static void main(String[] args)
{
int N1 = 9987;
int N2 = 123;
findMaxSum(N1, N2);
}
}
// This code is contributed by grand_master
Python3
# Python program to maximise the sum of two
# Numbers using at most one swap between them
MAX = 100
# Function to maximize the sum
# by swapping only one digit
def findMaxSum(n1, n2):
arr1 = [0]*(MAX)
arr2 = [0]*(MAX)
l1 = 0
l2 = 0
max1 = max(n1, n2);
min1 = min(n1, n2);
# Store digits of max(n1, n2)
i = max1
while i > 0:
arr1[l1] = (i % 10)
l1 += 1
i //= 10
# Store digits of min(n1, n2)
i = min1
while i > 0:
arr2[l2] = (i % 10)
l2 += 1
i //= 10
f = 0
# If length of the two numbers
# are unequal
if (l1 != l2):
# Find the most significant number
# and the most significant index
# for swapping
index = arr2.index(max(arr2))
for i in range ( l1 - 1, (l2 - 1), -1):
if (arr1[i] < arr2[index]):
(arr1[i], arr2[index]) = (arr2[index],arr1[i])
f = 1
break
# If both numbers are
# of equal length
if (f != 1):
index1 = 0
index2 = 0
diff1 = 0
diff2 = 0
for i in range( l2 - 1, -1,-1):
# Fetch the index of current maximum
# digit present in both the arrays
index1 = arr1.index(max(arr1[:i]))
index2 = arr2.index(max(arr2[:i]))
# Compute the difference
diff1 = (arr2[index2] - arr1[i]);
diff2 = (arr1[index1] - arr2[i]);
# Find the most significant index
# and the most significant digit
# to be swapped
if (diff1 > 0 or diff2 > 0):
if (diff1 > diff2):
arr1[i], arr2[index2] = arr2[index2],arr1[i]
break
elif (diff2 > diff1):
arr2[i], arr1[index1] = arr1[index1],arr2[i]
break
elif (diff1 == diff2):
if (index1 <= index2):
arr2[i], arr1[index1] = arr1[index1],arr2[i]
break
elif (index2 <= index1):
arr1[i], arr2[index2] = arr2[index2],arr1[i]
break;
# Restore the new numbers
f_n1 = 0
f_n2 = 0
for i in range (l1 - 1, -1,-1):
f_n1 = (f_n1 * 10) + arr1[i]
f_n2 = (f_n2 * 10) + arr2[i]
# Display the maximized sum
print(f_n1 + f_n2)
# Driver function
N1 = 9987
N2 = 123
findMaxSum(N1, N2)
# This code is contributed by ANKITKUMAR34
C#
// C# program to maximise the sum of two
// Numbers using at most one swap between them
using System;
class GFG{
static int MAX = 100;
static int max_element(int []arr, int pos)
{
int tmp = arr[0];
int ind = 0;
for(int i = 1; i < pos; i++)
{
if (tmp < arr[i])
{
tmp = arr[i];
ind = i;
}
}
return ind;
}
// Function to maximize the sum
// by swapping only one digit
static void findMaxSum(int n1, int n2)
{
int []arr1 = new int[MAX];
int []arr2 = new int[MAX];
int l1 = 0, l2 = 0;
int max1 = Math.Max(n1, n2);
int min1 = Math.Min(n1, n2);
// Store digits of max(n1, n2)
for(int i = max1; i > 0; i /= 10)
arr1[l1++] = (i % 10);
// Store digits of min(n1, n2)
for(int i = min1; i > 0; i /= 10)
arr2[l2++] = (i % 10);
int f = 0;
// If length of the two numbers
// are unequal
if (l1 != l2)
{
// Find the most significant number
// and the most significant index
// for swapping
int index = (max_element(arr2,l2));
for(int i = l1 - 1; i > (l2 - 1); i--)
{
if (arr1[i] < arr2[index])
{
int tmp = arr1[i];
arr1[i] = arr2[index];
arr2[index] = tmp;
f = 1;
break;
}
}
}
// If both numbers are
// of equal length
if (f != 1)
{
int index1 = 0, index2 = 0;
int diff1 = 0, diff2 = 0;
for(int i = l2 - 1; i >= 0; i--)
{
// Fetch the index of current maximum
// digit present in both the arrays
index1 = (max_element(arr1, i));
index2 = (max_element(arr2, i));
// Compute the difference
diff1 = (arr2[index2] - arr1[i]);
diff2 = (arr1[index1] - arr2[i]);
// Find the most significant index
// and the most significant digit
// to be swapped
if (diff1 > 0 || diff2 > 0)
{
if (diff1 > diff2)
{
int tmp = arr1[i];
arr1[i] = arr2[index2];
arr2[index2] = tmp;
break;
}
else if (diff2 > diff1)
{
int tmp = arr1[index1];
arr1[index1] = arr2[i];
arr2[i] = tmp;
break;
}
else if (diff1 == diff2)
{
if (index1 <= index2)
{
int tmp = arr1[index1];
arr1[index1] = arr2[i];
arr2[i] = tmp;
break;
}
else if (index2 <= index1)
{
int tmp = arr1[i];
arr1[i] = arr2[index2];
arr2[index2] = tmp;
break;
}
}
}
}
}
// Restore the new numbers
int f_n1 = 0, f_n2 = 0;
for(int i = l1 - 1; i >= 0; i--)
{
f_n1 = (f_n1 * 10) + arr1[i];
f_n2 = (f_n2 * 10) + arr2[i];
}
// Display the maximized sum
Console.Write(f_n1 + f_n2);
}
// Driver code
public static void Main(string[] args)
{
int N1 = 9987;
int N2 = 123;
findMaxSum(N1, N2);
}
}
// This code is contributed by rutvik_56
Javascript
输出:
10740
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live