📌  相关文章
📜  通过按相同顺序重新排列两个给定数字的数字之间可能存在的最小差异

📅  最后修改于: 2021-09-03 03:30:12             🧑  作者: Mango

给定两个N位正整数XY ,任务是通过以相同顺序重新排列两个整数的数字来找到两个整数之间可能的最小绝对差。

例子:

方法:可以通过找到具有相同数字重排顺序的XY 的每个排列之间的绝对差异来解决给定的问题。请按照以下步骤解决问题:

  • 初始化一个二维数组arr[2][N] ,分别存储数字XY的数字。
  • 初始化一个数组,比如P[] ,它存储数字 [0, N – 1] 的排列。
  • 初始化一个变量,比如minDIff ,它存储XY之间的最小差异。
  • 遍历P[]的所有可能排列并执行以下步骤:
    • 根据排列重新排列XY的数字,并将它们之间的绝对差值存储在一个变量中,例如difference
    • 完成上述步骤后,将minDIff的值更新为minDiffdifference 中的最小值。
  • 完成以上步骤后,打印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)