📜  一个与原始数字之和等于另一个给定数字的数字的排列

📅  最后修改于: 2021-04-17 18:30:16             🧑  作者: Mango

给定两个整数AC ,任务是检查是否存在数字A的排列,以使数字A及其排列的总和等于C。

例子:

天真的方法:解决问题的最简单方法是生成数字A的所有排列,并将其与A的原始值相加。现在,检查它们的总和是否等于C。

时间复杂度: O((log 10 A)!)
辅助空间: O(1)

高效方法:为了优化上述方法,其思想是从C中减去A的值,并检查是否存在A的排列,该排列等于CA之差。请按照以下步骤解决问题:

  • C中减去A ,然后检查C是否小于0 。如果发现是真的,则打印“否”。
  • 否则,请执行以下步骤:
    • A转换为其等效的字符串表示形式,并将其存储在变量中,例如S。
    • C转换为其等效的字符串表示形式,并将其存储在变量中,例如K。
    • 对新生成的字符串SK进行排序。
    • 如果S等于K ,则在排列的同时打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if there
// exists a permutation of a number
// A whose sum with A results to C
void checkPermutation(int a, int c)
{
    // Subtract a from c
    c -= a;
 
    // Check if c is less than 0
    if (c <= 0) {
 
        // If true, print "No"
        cout << "No";
        return;
    }
 
    // Otherwise, convert a to its
    // equivalent string
    string s = to_string(a);
 
    // convert c to its
    // equivalent string
    string k = to_string(c);
 
    // Sort string s
    sort(s.begin(), s.end());
 
    // Sort string k
    sort(k.begin(), k.end());
 
    // If both strings are equal,
    // then print "Yes"
    if (k == s) {
        cout << "Yes\n"
             << c;
    }
 
    // Otherwise, print "No"
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    int A = 133, C = 446;
    checkPermutation(A, C);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to check if there
// exists a permutation of a number
// A whose sum with A results to C
static void checkPermutation(int a, int c)
{
     
    // Subtract a from c
    c -= a;
 
    // Check if c is less than 0
    if (c <= 0)
    {
         
        // If true, print "No"
        System.out.println("No");
        return;
    }
 
    // Otherwise, convert a to its
    // equivalent string
    String s = sortString(Integer.toString(a));
 
    // Convert c to its
    // equivalent string
    String k = sortString(Integer.toString(c));
 
    // If both strings are equal,
    // then print "Yes"
    if (k.equals(s))
    {
        System.out.println("Yes");
        System.out.println(c);
    }
 
    // Otherwise, print "No"
    else
    {
        System.out.println("No");
    }
}
 
// Method to sort a string alphabetically
public static String sortString(String inputString)
{
     
    // Convert input string to char array
    char tempArray[] = inputString.toCharArray();
       
    // sort tempArray
    Arrays.sort(tempArray);
       
    // Return new sorted string
    return new String(tempArray);
}
 
// Driver code
public static void main(String[] args)
{
    int A = 133, C = 446;
     
    checkPermutation(A, C);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to check if there
# exists a permutation of a number
# A whose sum with A results to C
def checkPermutation(a, c):
    # Subtract a from c
    c -= a
 
    # Check if c is less than 0
    if (c <= 0):
        # If true, print "No"
        print("No")
        return
 
    # Otherwise, convert a to its
    # equivalent string
    s = str(a)
 
    # convert c to its
    # equivalent string
    k = str(c)
 
    res = ''.join(sorted(s))
 
    # Sort string s
    s = str(res)
 
    # Sort string k
    res = ''.join(sorted(k))
    k = str(res)
 
    # If both strings are equal,
    # then print "Yes"
    if (k == s):
        print("Yes")
        print(c)
 
    # Otherwise, print "No"
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
    A = 133
    C = 446
    checkPermutation(A, C)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
class GFG
{
   
  // Function to check if there
  // exists a permutation of a number
  // A whose sum with A results to C
  static void checkPermutation(int a, int c)
  {
     
    // Subtract a from c
    c -= a;
 
    // Check if c is less than 0
    if (c <= 0) {
 
      // If true, print "No"
      Console.Write("No");
      return;
    }
 
    // Otherwise, convert a to its
    // equivalent string
    string s = a.ToString();
 
    // convert c to its
    // equivalent string
    string k = c.ToString();
 
    // Sort string s
    char[] arr = s.ToCharArray();
    Array.Sort(arr);
 
    // Sort string k
    char[] brr = k.ToCharArray();
    Array.Sort(brr);
 
    // If both strings are equal,
    // then print "Yes"
    if (String.Join("", brr) == String.Join("", arr)) {
      Console.Write("Yes\n" + c);
    }
 
    // Otherwise, print "No"
    else {
      Console.WriteLine("No");
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int A = 133, C = 446;
    checkPermutation(A, C);
  }
}
 
// This code is contributed by ukasp.


输出:
Yes
313

时间复杂度: O((log 10 A)* log(log 10 A))
辅助空间: O(log 10 A)