Gauri Shankar Iyer

Greenhorn

Posts: 6

posted 7 years ago

Say I have a number, 12345678.

I want to retrieve all the sub-numbers (if I may call it that) from this number, in forward order, without re-arranging or repeating digits.

E.g.,

[1,2,3,4,5,6,7,8] is valid.

[12,23,34,45,56,67,78] is valid.

[12,13,14,15,16,17,123,1345,1678,3456,378] are all valid.

[1234567,2348] are valid.

But 112, 213,5674 are not.

Any suggestions?

I want to retrieve all the sub-numbers (if I may call it that) from this number, in forward order, without re-arranging or repeating digits.

E.g.,

[1,2,3,4,5,6,7,8] is valid.

[12,23,34,45,56,67,78] is valid.

[12,13,14,15,16,17,123,1345,1678,3456,378] are all valid.

[1234567,2348] are valid.

But 112, 213,5674 are not.

Any suggestions?

Campbell Ritchie

Sheriff

Posts: 53742

127

posted 7 years ago

Go through the methods of the String class and you will find methods which allow you to take sub-strings. You may find it easier to turn 12345678 into "12345678".

Remind yourself of the five basic arithmetic operators. Remember there are not four, but five. The fifth is probably useful here.

Get yourself a List to put the resultant numbers into.

Remind yourself of the five basic arithmetic operators. Remember there are not four, but five. The fifth is probably useful here.

Get yourself a List to put the resultant numbers into.

Embla Tingeling

Ranch Hand

Posts: 237

posted 7 years ago

So the initial number defines the allowed order among digits? And you're supposed to form all subnumbers which preserve the allowed order among digits? That's simpler than it looks really.

Say the initial number is 123. The first step is to split the initial number into individual digits and store them in an int array. To just print the digits will generate all subnumbers of length 1,

But how to generate all valid subnumbers of length 2? That can be done by introducing a second nested loop.

As you can see the nested loop starts from the index of the outer loop plus 1. This means it will only consider digits that safely can be added to the current number of the outer loop.

Now you can easily generate all valid 3 digit numbers by introducing yet another nested loop following the same pattern. (There should be one such number only by the way, namely the initial number).

That's all fine but what about if you don't know how many digits there are in the initial number? How can you have a variable number of nested loops? The answer is recursion. The above can be reformulated as a recursive method where each recursive call represents one step deeper down the loop nesting.

Gauri Shankar Iyer wrote:

Any suggestions?

So the initial number defines the allowed order among digits? And you're supposed to form all subnumbers which preserve the allowed order among digits? That's simpler than it looks really.

Say the initial number is 123. The first step is to split the initial number into individual digits and store them in an int array. To just print the digits will generate all subnumbers of length 1,

But how to generate all valid subnumbers of length 2? That can be done by introducing a second nested loop.

As you can see the nested loop starts from the index of the outer loop plus 1. This means it will only consider digits that safely can be added to the current number of the outer loop.

Now you can easily generate all valid 3 digit numbers by introducing yet another nested loop following the same pattern. (There should be one such number only by the way, namely the initial number).

That's all fine but what about if you don't know how many digits there are in the initial number? How can you have a variable number of nested loops? The answer is recursion. The above can be reformulated as a recursive method where each recursive call represents one step deeper down the loop nesting.

It is sorta covered in the JavaRanch Style Guide. |