给定两个N位正整数X和Y ,任务是通过以相同顺序重新排列两个整数的位数来找到两个整数之间的最小绝对差。
例子:
Input: X = 5181, Y = 3663
Output: 1482
Explanation:
Rearranging the digits of both the given integers in the order of (3, 2, 4, 1) modifies the value of X and Y as 8115 and 6633 respectively.
Therefore, the absolute difference between the two is (8115 – 6633) = 1482, which is the minimum possible difference.
Input: X = 37198, Y = 44911
Output: 1278
方法:可以通过找到具有相同数字重排顺序的X和Y的每个排列之间的绝对差来解决给定的问题。请按照以下步骤解决问题:
- 初始化一个二维数组arr [2] [N] ,该数组分别存储数字X和Y的数字。
- 初始化一个数组,例如P [] ,该数组存储数字[0,N – 1]的排列。
- 初始化一个变量,例如minDIff ,该变量存储X和Y之间的最小差异。
- 遍历P []的所有可能排列并执行以下步骤:
- 根据排列重新排列X和Y的数字,并将它们之间的绝对差存储在变量说差异中。
- 在上述步骤之后,更新为最小MINDIFF和差的MINDIFF的值。
- 完成上述步骤后,将minDIff的值打印为最小差异。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum difference
// between X and Y after rearranging
// their digits in the same order
int minDifference(int X, int Y)
{
// Stores the minimum difference
int minDiff = INT_MAX;
// Stores the number X as string
string x = to_string(X);
// Stores the number Y as string
string y = to_string(Y);
// Stores number of digits
int n = x.size();
// Store the digits
int a[2][n];
// Traverse the range [0, 1]
for (int i = 0; i < 2; i++) {
// Traverse the range [0, N - 1]
for (int j = 0; j < n; j++) {
// If the value of i is 0
if (i == 0)
a[i][j] = x[j] - '0';
// Otherwise
else
a[i][j] = y[j] - '0';
}
}
// Stores the permutation of [1, N]
int p[n];
// Initialize the permutation array
for (int i = 0; i < n; i++)
p[i] = i;
// Generate all possible permutation
do {
// Stores the maximum of X and Y
int xx = INT_MIN;
// Stores the minimum of X and Y
int yy = INT_MAX;
// Traverse the range [0, 1]
for (int i = 0; i < 2; i++) {
// Stores the number
// after rearranging
int num = 0;
// Traverse the range [0, N - 1]
for (int j = 0; j < n; j++)
// Update the value of num
num = num * 10 + a[i][p[j]];
// Update the value of xx
xx = max(xx, num);
// Update the value of yy
yy = min(yy, num);
}
// Update the value of minDiff
minDiff = min(minDiff, xx - yy);
} while (next_permutation(p, p + n));
// Return the minimum difference
return minDiff;
}
// Driver Code
int main()
{
int X = 37198, Y = 44911;
cout << minDifference(X, Y);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find minimum difference
// between X and Y after rearranging
// their digits in the same order
static int minDifference(int X, int Y)
{
// Stores the minimum difference
int minDiff = Integer.MAX_VALUE;
// Stores the number X as String
String x = String.valueOf(X);
// Stores the number Y as String
String y = String.valueOf(Y);
// Stores number of digits
int n = x.length();
// Store the digits
int [][]a = new int[2][n];
// Traverse the range [0, 1]
for (int i = 0; i < 2; i++) {
// Traverse the range [0, N - 1]
for (int j = 0; j < n; j++) {
// If the value of i is 0
if (i == 0)
a[i][j] = x.charAt(j) - '0';
// Otherwise
else
a[i][j] = y.charAt(j) - '0';
}
}
// Stores the permutation of [1, N]
int []p = new int[n];
// Initialize the permutation array
for (int i = 0; i < n; i++)
p[i] = i;
// Generate all possible permutation
do {
// Stores the maximum of X and Y
int xx = Integer.MIN_VALUE;
// Stores the minimum of X and Y
int yy = Integer.MAX_VALUE;
// Traverse the range [0, 1]
for (int i = 0; i < 2; i++) {
// Stores the number
// after rearranging
int num = 0;
// Traverse the range [0, N - 1]
for (int j = 0; j < n; j++)
// Update the value of num
num = num * 10 + a[i][p[j]];
// Update the value of xx
xx = Math.max(xx, num);
// Update the value of yy
yy = Math.min(yy, num);
}
// Update the value of minDiff
minDiff = Math.min(minDiff, xx - yy);
} while (next_permutation(p));
// Return the minimum difference
return minDiff;
}
static boolean next_permutation(int[] p) {
for (int a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (int b = p.length - 1;; --b)
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
// Driver Code
public static void main(String[] args)
{
int X = 37198, Y = 44911;
System.out.print(minDifference(X, Y));
}
}
// This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
class GFG {
// Function to find minimum difference
// between X and Y after rearranging
// their digits in the same order
static int minDifference(int X, int Y)
{
// Stores the minimum difference
int minDiff = Int32.MaxValue;
// Stores the number X as String
string x = X.ToString();
// Stores the number Y as String
string y = Y.ToString();
// Stores number of digits
int n = x.Length;
// Store the digits
int[, ] a = new int[2, n];
// Traverse the range [0, 1]
for (int i = 0; i < 2; i++) {
// Traverse the range [0, N - 1]
for (int j = 0; j < n; j++) {
// If the value of i is 0
if (i == 0)
a[i, j] = x[j] - '0';
// Otherwise
else
a[i, j] = y[j] - '0';
}
}
// Stores the permutation of [1, N]
int[] p = new int[n];
// Initialize the permutation array
for (int i = 0; i < n; i++)
p[i] = i;
// Generate all possible permutation
do {
// Stores the maximum of X and Y
int xx = Int32.MinValue;
// Stores the minimum of X and Y
int yy = Int32.MaxValue;
// Traverse the range [0, 1]
for (int i = 0; i < 2; i++) {
// Stores the number
// after rearranging
int num = 0;
// Traverse the range [0, N - 1]
for (int j = 0; j < n; j++)
// Update the value of num
num = num * 10 + a[i, p[j]];
// Update the value of xx
xx = Math.Max(xx, num);
// Update the value of yy
yy = Math.Min(yy, num);
}
// Update the value of minDiff
minDiff = Math.Min(minDiff, xx - yy);
} while (next_permutation(p));
// Return the minimum difference
return minDiff;
}
static bool next_permutation(int[] p)
{
for (int a = p.Length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (int b = p.Length - 1;; --b)
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.Length - 1; a < b;
++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
// Driver Code
public static void Main(string[] args)
{
int X = 37198, Y = 44911;
Console.WriteLine(minDifference(X, Y));
}
}
// This code is contributed by ukasp.
Javascript
输出:
1278
时间复杂度: O(N!)
辅助空间: O(1)