I need to find the smallest number formed from the same digits of a given natural number n, using arrays.

Dani AI

Generated

Three practical approaches were discussed in the thread: digit-frequency counting (the working solution posted by ), repeated-selection (the idea from ), and in-place sorting (what was trying). Trade-offs: counting is O(d) with fixed extra space (d = digit count), sorting a string is simplest and fine for typical inputs (O(d log d)), and repeated-selection is O(d^2) and usually unnecessary.

Why zero the array? A local int a[10]; starts with indeterminate values. Incrementing a[digit]++ without initializing reads and updates garbage, producing wrong counts. Safe alternatives: initialize on declaration (int a[10] = {0};), use std::vector<int> a(10);, or call std::fill_n. The important part is that the array is zeroed before any counting happens.

What is j doing? It’s just a repetition counter: for each digit value i, that inner loop emits digit i exactly a[i] times into the result. The same logic can be written more readably as while (a[i]-- > 0) { append digit i } or for (int k=0; k<a[i]; ++k) { ... }.

A few practical notes and fixes from the thread: ’s arrange loop compares list[i] with list[i+1] while iterating up to i < length, which accesses list[length] (out of bounds); use i < length-1 or simply std::sort. For debugging, printing the array first (as suggested) reveals uninitialized data quickly. Also consider large inputs: converting back to an integer may overflow — for very large numbers it’s safer to return or print the arranged digits as a string.

Example (string+sort approach):

std::string s = std::to_string(n);
std::sort(s.begin(), s.end());
if (s.front() == '0') {
    auto it = std::find_if(s.begin(), s.end(), [](char c){ return c != '0'; });
    if (it != s.end()) std::swap(s.front(), *it);
}
std::cout << s << '\n';

This handles leading zeros, is concise, and avoids manual indexing pitfalls.

Recommended Answers

All 6 Replies

#include <fstream>
#include <math.h>


int *list;
bool arrange(int length){

  for(int i=0;i<length;i++)
  {
	if(list[i]>list[i+1])
		swap(list[i],list[i+1]);

	

    }

  for(int i=0;i<length;i++)cout<<"my new list contains "<<list [i]<<endl;


return true;
}
bool file(char* Filename, int length)
{

	

	ifstream file;
	file.open(Filename);



	list=new int[length];
  for(int i=0;i<length;i++) 
  {
	file>>list[i];
	cout<<"the list contain: "<<list[i]<<endl;

    }
	arrange(length);

return true;
}

Say you have the number: 251 The smallest number from those digits will be: 125 Therefore: digit1 = smallest number, digit2 = next smallest, etc.

Seperate the number into individual digits using an array. Find the smallest value and put this in the first index of a new array. Repeat this, removing the number you use each time, until you have no numbers left in the original array. Put all of the new array's values into a string and convert that to an int. This is your new number!

EDIT: You may want to use lists not an array.
EDIT EDIT: Oops got beaten too it. Ah well :)

I've found a code that works very well:

#include<iostream>
using namespace std;
int main()
{
    int n,a[10],i,j;
    for(i=0;i<=9;i++) a[i]=0;
    cin>>n;
    while(n>0)
    {
        a[n%10]++;
        n=n/10;
    }
    i=1;
    while(a[i]==0) i++;
    n=i;
    a[i]--;
    for(i=0;i<=9;i++)
        for(j=1;j<=a[i];j++)
            n=n*10+i;
    cout<<n;
    return 0;
}

I think it's still difficult for me to understand why for(i=0;i<=9;i++) a[i]=0;
before cin>>n and what is the significance of j.
Otherwise, the code looks like you are trying to explain.

Next time, please press the code button before you paste your code.

for (i=0; i<=9; i++){}

Ever done a while loop before? Consider a loop which is while (i<=9){} . At the end of the loop, you would have i++; to increment the value of i .
Basically (in a for loop): i=0 makes a new variable called i which is set to 0 . i<=9 means that we keep looping while i is less than or equal to 9. i++ means that we increment the value of i once each loop.

I didn't understand why for(...) at the beginning, a has to be 0.

I didn't understand why for(...) at the beginning, a has to be 0.

Who says it has to be 0? Test it. First thing in the program just print out the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.