📌  相关文章
📜  从具有相同位数的给定大数中找出可能的最小数

📅  最后修改于: 2021-10-28 01:45:24             🧑  作者: Mango

给定一个长度为N的数字K ,任务是通过任意次数交换数字,找到可以由 N 个数字中的 K 个组成的最小可能数字。
例子:

方法:想法是使用哈希。为了实现散列,创建了一个大小为 10 的数组arr[] 。迭代给定的数字,并将每个数字的出现次数存储在相应索引处的散列中。然后迭代哈希数组并根据其频率打印第i 个数字。输出将是所需的最小 N 位数。
下面是上述方法的实现:

CPP
// C++ implementation of the above approach
 
#include 
using namespace std;
 
// Function for finding the smallest
// possible number after swapping
// the digits any number of times
string smallestPoss(string s, int n)
{
    // Variable to store the final answer
    string ans = "";
 
    // Array to store the count of
    // occurrence of each digit
    int arr[10] = { 0 };
 
    // Loop to calculate the number
    // of occurrences of every digit
    for (int i = 0; i < n; i++) {
        arr[s[i] - 48]++;
    }
 
    // Loop to get smallest number
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < arr[i]; j++)
            ans = ans + to_string(i);
    }
 
    // Returning the answer
    return ans;
}
 
// Driver code
int main()
{
    int N = 15;
    string K = "325343273113434";
 
    cout << smallestPoss(K, N);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG
{
 
// Function for finding the smallest
// possible number after swapping
// the digits any number of times
static String smallestPoss(String s, int n)
{
    // Variable to store the final answer
    String ans = "";
 
    // Array to store the count of
    // occurrence of each digit
    int arr[] = new int[10];
 
    // Loop to calculate the number
    // of occurrences of every digit
    for (int i = 0; i < n; i++)
    {
        arr[s.charAt(i) - 48]++;
    }
 
    // Loop to get smallest number
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < arr[i]; j++)
            ans = ans + String.valueOf(i);
    }
 
    // Returning the answer
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 15;
    String K = "325343273113434";
 
    System.out.print(smallestPoss(K, N));
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the above approach
 
# Function for finding the smallest
# possible number after swapping
# the digits any number of times
def smallestPoss(s, n):
     
    # Variable to store the final answer
    ans = "";
 
    # Array to store the count of
    # occurrence of each digit
    arr = [0]*10;
 
    # Loop to calculate the number
    # of occurrences of every digit
    for i in range(n):
        arr[ord(s[i]) - 48] += 1;
     
    # Loop to get smallest number
    for i in range(10):
        for j in range(arr[i]):
            ans = ans + str(i);
     
    # Returning the answer
    return ans;
 
# Driver code
if __name__ == '__main__':
    N = 15;
    K = "325343273113434";
 
    print(smallestPoss(K, N));
 
# This code is contributed by 29AjayKumar


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
// Function for finding the smallest
// possible number after swapping
// the digits any number of times
static String smallestPoss(String s, int n)
{
    // Variable to store the readonly answer
    String ans = "";
 
    // Array to store the count of
    // occurrence of each digit
    int []arr = new int[10];
 
    // Loop to calculate the number
    // of occurrences of every digit
    for (int i = 0; i < n; i++)
    {
        arr[s[i] - 48]++;
    }
 
    // Loop to get smallest number
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < arr[i]; j++)
            ans = ans + String.Join("",i);
    }
 
    // Returning the answer
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 15;
    String K = "325343273113434";
 
    Console.Write(smallestPoss(K, N));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
112233333344457

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程