A logic problem by Interliser 727

Logic Level 2

All of the digits from 0 through 9 are used to create two 5-digit positive integers, and then the two numbers are subtracted to find their positive difference: 72910 54836 = 18074 . 72910 - 54836 = {\color{#D61F06}18074}. What's the smallest positive difference we can get this way?


The answer is 247.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

18 solutions

Uros Stojkovic
Jun 4, 2018

Given two numbers a c e g i \overline{acegi} and b d f h j \overline{bdfhj} , we need to minimize: ( a b ) 1 0 4 + ( c d ) 1 0 3 + ( e f ) 1 0 2 + ( g h ) 10 + ( i j ) . (a-b)10^{4} + (c-d)10^{3} + (e-f)10^{2} + (g-h)10 + (i-j).

To have a minimal positive difference it has to be:

a b = 1 a - b = 1 and c d < e f < g h < i j c-d < e-f < g-h < i-j .

We can form pairs using this strategy, 0 9 < 1 8 < 2 7 < 3 6 0-9 < 1-8 < 2-7 < 3-6 . This leaves 5 5 and 4 4 for a a and b b . Thus, desired numbers are 50123 50123 and 49876 49876 and their difference is 50123 49876 = 247. 50123-49876 = \boxed{247.}

Simply brilliant

Biagio Distefano - 3 years ago

Isn't there one too many digit?

Maxence Seymat - 3 years ago

Log in to reply

You are right, I corrected it. Thanks!

Uros Stojkovic - 3 years ago

Note that a-b can never equal 0 because you cannot use the same number twice. a-b cannot be less than 1 because the final result cannot be negative. Therefore for the smallest difference a-b must equal 1, but this should be done while preserving numbers that can produce the smallest result possible for c-d (which is 0-9). It follows more clearly from here that 5 and 4 are reserved for a and b.

Sathrandur . - 3 years ago

I am so impressed.

Sarawut Positwinyu - 3 years ago

Essentially correct, though I would reword some things. It's not that you need c-d to be less than e-f so much as you want c-d to be as small as possible.
Minimize, in order: a-b (positive constraint), c-d, e-f, g-h, i-j. The logic is a little different but you end up in the same place.

Richard Desper - 3 years ago

Clever solution

Rehan Khan - 2 years, 12 months ago

Perhaps it needs to be stated that, given "a – b = 1" is necessary for a minimal difference, then "c – d" needs to be as large a negative number as possible. Once I'd grasped that the coefficients of the other powers of 10 should be negative, the explanation made sense.

Paul Cockburn - 2 years, 11 months ago
Michael Mendrin
May 27, 2018

50123 49876 = 247 50123-49876=247

Start with the idea of subtracting ( 50000 x ) (50000-x) from ( 50000 + y ) (50000+y) or 4 X X X X 4XXXX from 5 Y Y Y Y 5YYYY , and finding the largest possible X X X X XXXX and smallest possible Y Y Y Y YYYY , using all the digits except 4 , 5 4, 5 .

We have X X X X = 9876 XXXX=9876 and Y Y Y Y = 0123 YYYY=0123 , and we have the two numbers to find their smallest difference.

If we started with other variations such as ( 30000 x ) (30000-x) from ( 30000 + y ) (30000+y) , then we wouldn't get as large X X X X XXXX or small Y Y Y Y YYYY , such as X X X X = 9876 XXXX=9876 and Y Y Y Y = 0145 YYYY=0145 in this case, resulting in a difference of 269.

Easy but its level 3 algebra :/

Interliser 727 - 3 years ago

Log in to reply

I'd like to see other solutions

Michael Mendrin - 3 years ago

I thought this will in intermediate part.

Kelvin Hong - 3 years ago

Log in to reply

actually this is an Australia Mathematic Competition question for 6th grades.

Interliser 727 - 3 years ago

How to know to start with 5. I started with a 0and 1 as 1st digit

Sachin Chandwani - 3 years ago

Log in to reply

The answer is actually incorrect when it says to start with 4,5. The only conclusion you can make is higher number should have most significant digit as close to lower numbers most significant digit. BXXXX, AYYYY, such that B =A+1. Next you try to make BXXXX as small as possible and AYYYY as large as possible and you get B0123, AND A9876. Now you are left with 5 and 4 which will take place of B and A respectively.

Saurabh Khanduja - 3 years ago

Log in to reply

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from math import isinf
from itertools import permutations

def unique(x):
    d = {}

    for i in x:
        try:
            d[i] += 1
        except:
            d[i] = 1

    if max(d.items(), key = lambda x: x[1])[1] == 1:
        return True
    else:
        return False

diff = isinf
digits = ''.join([str(i) for i in range(10)])
for i in range (10000,100000):
    x = str(i)
    if unique(x):
        uncommon = [c for c in digits if c not in x]
        perm = permutations(uncommon, 5)        
        for j in list(perm):
            y = ''.join(j)
            if int(i) - int(y) < diff and int(i) > int(y):
                diff = int(i) - int(y)
                vals = [str(i), y, int(i) - int(y)]
                print vals

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
['12345', '06789', 5556]
['12345', '06798', 5547]
['12345', '06879', 5466]
['12345', '06897', 5448]
['12345', '06978', 5367]
['12345', '06987', 5358]
['12345', '07689', 4656]
['12345', '07698', 4647]
['12345', '07869', 4476]
['12345', '07896', 4449]
['12345', '07968', 4377]
['12345', '07986', 4359]
['12345', '08679', 3666]
['12345', '08697', 3648]
['12345', '08769', 3576]
['12345', '08796', 3549]
['12345', '08967', 3378]
['12345', '08976', 3369]
['12345', '09678', 2667]
['12345', '09687', 2658]
['12345', '09768', 2577]
['12345', '09786', 2559]
['12345', '09867', 2478]
['12345', '09876', 2469]
['20345', '17896', 2449]
['20345', '17968', 2377]
['20345', '17986', 2359]
['20345', '18679', 1666]
['20345', '18697', 1648]
['20345', '18769', 1576]
['20345', '18796', 1549]
['20345', '18967', 1378]
['20345', '18976', 1369]
['20345', '19678', 667]
['20345', '19687', 658]
['20345', '19768', 577]
['20345', '19786', 559]
['20345', '19867', 478]
['20345', '19876', 469]
['30145', '29678', 467]
['30145', '29687', 458]
['30145', '29768', 377]
['30145', '29786', 359]
['30145', '29867', 278]
['30145', '29876', 269]
['40125', '39867', 258]
['40125', '39876', 249]
['50123', '49876', 247]

Michael Fitzgerald - 3 years ago

We cant start with 0 since the number will not be significant

John Taberna - 3 years ago

0,1,2,3,9,8,7 are used another positions. So only 4 and 5 use in start

Shijil Anish Kumar - 3 years ago
Jochen Morent
Jun 3, 2018

The first digits of the resulting numbers may only differ by 1 to get the smallest possible difference.

So starting by ignoring the first digits let's pair up the other digits, so that the lowest and the highest, the second lowest and second highest, ... Get paired up.

This way the pairs.

0-9 1-8 2-7 3-6 4-5

Now use the lowest number of the first 4 pairs and make them the tail of the bigger number, while using the highest number of the first 4 pairs to make the tail of the smaller number.

The only pair that is left (and that has a difference of only 1) is 4 and 5.

To make the bigger number really bigger use the 5 as the first digit while using the 4 as the first digit of the smaller number.

This way the two numbers 50123 and 49876 are created.

Excellent and comprehensive solution.

Thomas Sutcliffe - 3 years ago

👏👏👏👏👏👍👍👍👍👍👍👏👏👏👏

larisa karabela - 3 years ago

Im too dumb for this

lol x - 2 years, 12 months ago
Steven Gottlieb
Jun 4, 2018

The 1st digits should differ by 1 as they can't differ by 0. Let's not worry about the 1st digits yet. Let the larger number (not including the 1st digit) be as small as possible and let the smaller number (not including the 1st digit) be as large as possible. So the last 4 digit of the larger number should be 0123 and the last 4 digits of the smaller number be 9876. We haven't used digits 5 and 4. So let the larger number be 50123 and the smaller number be 49876. Their difference is 247.

Nice and easy solution

Satyaki Mullick - 3 years ago

Log in to reply

Thank you!

Steven Gottlieb - 2 years, 12 months ago
Laszlo Kocsis
Jun 4, 2018

The difference between the first digits should be 1, the first being the higher. We don't need to worry about them for now, as only their difference is important. Let's put them aside for a while.

The remaining parts are two four digit numbers (allowing 0 in the starting position as it will be the second in the final five digit number). The smallest difference between them is when the first four digit number is the smallest possible (0123), and the second four digit number is the highest possible (9876). The remaining two digits are 4 and 5. Their difference is 1. Check.

50123-49876=247

Evan Huang
Jun 5, 2018

This is what I did: First I figured that the 10000th place had to be 1 away from one another to minimize the difference, so I figured that the 1000th through units digits for the larger number needed to be minimized, while the smaller number needed to be maximized to minimize the difference, so... n=10000th place

n 0123 n0123 and ( n 1 ) 9876 (n-1)9876 and the digits 4 and 5 are left so the numbers have to be 50123 50123 and 49876 49876 .

Subtract and you get 247 247 for the answer.

Ricardo Barbosa
Jun 7, 2018
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#python bruteforce by norsigma.com

import itertools

digits = [0,1,2,3,4,5,6,7,8,9]

x = list(itertools.permutations(digits))

min = 18074; #just for fun

for element in x: 

    a = "%d%d%d%d%d" % (element[0],element[1],element[2],element[3],element[4])

    b = "%d%d%d%d%d" % (element[5],element[6],element[7],element[8],element[9])

    c = int(a) - int(b)

    if c>0 and c < min: min = c

print(min)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from math import isinf
from itertools import permutations

def unique(x):
    d = {}

    for i in x:
        try:
            d[i] += 1
        except:
            d[i] = 1

    if max(d.items(), key = lambda x: x[1])[1] == 1:
        return True
    else:
        return False

diff = isinf
digits = ''.join([str(i) for i in range(10)])
for i in range (10000,100000):
    x = str(i)
    if unique(x):
        uncommon = [c for c in digits if c not in x]
        perm = permutations(uncommon, 5)        
        for j in list(perm):
            y = ''.join(j)
            if int(i) - int(y) < diff and int(i) > int(y):
                diff = int(i) - int(y)
                vals = [str(i), y, int(i) - int(y)]
                print vals

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
['12345', '06789', 5556]
['12345', '06798', 5547]
['12345', '06879', 5466]
['12345', '06897', 5448]
['12345', '06978', 5367]
['12345', '06987', 5358]
['12345', '07689', 4656]
['12345', '07698', 4647]
['12345', '07869', 4476]
['12345', '07896', 4449]
['12345', '07968', 4377]
['12345', '07986', 4359]
['12345', '08679', 3666]
['12345', '08697', 3648]
['12345', '08769', 3576]
['12345', '08796', 3549]
['12345', '08967', 3378]
['12345', '08976', 3369]
['12345', '09678', 2667]
['12345', '09687', 2658]
['12345', '09768', 2577]
['12345', '09786', 2559]
['12345', '09867', 2478]
['12345', '09876', 2469]
['20345', '17896', 2449]
['20345', '17968', 2377]
['20345', '17986', 2359]
['20345', '18679', 1666]
['20345', '18697', 1648]
['20345', '18769', 1576]
['20345', '18796', 1549]
['20345', '18967', 1378]
['20345', '18976', 1369]
['20345', '19678', 667]
['20345', '19687', 658]
['20345', '19768', 577]
['20345', '19786', 559]
['20345', '19867', 478]
['20345', '19876', 469]
['30145', '29678', 467]
['30145', '29687', 458]
['30145', '29768', 377]
['30145', '29786', 359]
['30145', '29867', 278]
['30145', '29876', 269]
['40125', '39867', 258]
['40125', '39876', 249]
['50123', '49876', 247]

Michael Fitzgerald - 3 years ago

Nice job! but this is somewhat faster

Michael Fitzgerald - 3 years ago
Anurag Pandey
Jun 7, 2018

It was intuitive that the difference in the ten thousand's place should be 1 and about the difference of the latter part should be smaller . So for the last four digits of the first number should be as smaller as it can be and the last four digits of the second number should be as big as it can be . Which gives us 0123 and 9876 respectively. and since the difference should be positive we get the answer as 50123 - 49876 = 247 .

James Carmody
Jun 6, 2018

Minimize your difference by choosing numbers in the ten thousands place close to each other, like 80### and 79###.

Keep the difference small by choosing the smallest digits available for the first number hundreds, tens, and ones, then choose the highest available digits for the amount you reduce. With our numbers, that will be 80123 and 79654.

Next, take the difference: 80123 - 79654 = 469

Continue with this strategy until your answers converge to the absolute minimum:

70123 - 69854 = 269

60123 - 59874 = 249

50123 - 49876 = 247

40125 - 39876 = 249

Aha! We start to increase after a 247 result, which means 247 is our absolute minimum!

247 is our smallest number.

We have 10 nrs, 2 pairs of 5, lets start by using the bigest and smalest pair of 4 nrs we can get, well 9876 is the bigest 4pair and 0123 the smalest leaving 4and 5 for our first digits and fits perfectly because 5less 4 is 1, so we put 5 infront of our small nr and 4 in front of our big one to get 50123 and 49876, resulting in 247

Pawan Sharma
Jun 5, 2018

The “smallest” “big” number you can make with the digits (i.e. something big enough to subtract from) is 50123 since you want to assign the smallest digits to the largest placeholders. The “biggest” “small” number you can make with the remaining digits is 49876 since you want to assign the largest digits to the largest placeholders. The difference is 247.

Lukas Henke
Jun 5, 2018

1st key Idea: \newline Let a a be the larger number, from which we subtract b b . Each is a 5-digit number starting with a 1 a_1 and b 1 b_1 respectively. To minimize the difference a b a-b the leading digits have to be as close as possible. Without repetition of digits this leads to a 1 = b 1 + 1 a_1 = b_1 +1 . \newline 2nd key idea: \newline After their leading digits you have to minimize the bigger number a a and maximize the smaller number b b . As the a i a_i is 10 times as important as a i + 1 a_{i+1} , the minimized expansion of a a should be a 1 0 1 2 3 a_1 || 0 || 1 || 2 || 3 . Similarly, maximizing b b leads to b 1 9 8 7 6 b_1 || 9 || 8 || 7 || 6 . \\ Finishing this up: \\ The remaining unused digits are { 4 ; 5 } \{4;5\} for { a 1 ; b 1 } \{a_1 ;b_1\} . As a 1 = b 1 + 1 a_1 = b_1 +1 was worked out earlier, we obtain a 1 = 5 ; b 1 = 4 a_1 = 5 ; b_1 = 4 . Therefore, a b = 50123 49876 = 247 a - b = 50123 - 49876 =\boxed{247} .

Paramananda Das
Jun 5, 2018

It asks of smallest positive difference so I took two numbers 50123(minimum) and 49876 (maximum). Hence the answer is 247.😊

Bayu Wahyuadi
Jun 4, 2018

Start by dumbing down the question into an easier problem. By progressing from 1 digit numbers to 4 digit numbers.

1 digit number is simple and there is lots of variations 1-0, 2-1, 3-2, 4-3, 5-4 etc. You wont have to worry about using the same number twice. The smallest difference possible is 1.

2 digit number is similar but there is an added constraint, 20-19, 30-29, 40-39, 50-49. notice that the first viable answer from 1 digit isnt viable anymore because it will result in using the same number twice (10-09). The smallest difference is still 1.

3 digit number still continue with previous pattern and another added constraint, 301-298, 401-398, 501-498. The sequence of numbers seems to follow a certain pattern. The smallest difference is 3.

4 digit number is still continuing the previous pattern, but the numbering sequence become more obvious. 4012-3987, 5012-4987. The pairing becomes more narrow and the sequence between the first number (ascending) and second number (descending) is more visible. The smallest difference is 25.

5 digit number can be solved using previous learned pattern which we can easily deduce to : 50123-49876. Unlike previous difficulties, the pairing has been narrowed down to only one possibility with the smallest difference being 247.

Vinod Kumar
Dec 31, 2020

Create two almost equal numbers using 0 to 9 digits to get minimum positive difference 50123-49876=247

Answer 247

Falnésio Borges
Jun 10, 2018

The first two digits of both numbers must be 1 apart. We can't have two double digits that start with the same value (ex. 21 and 20). Therefore the second digits of each must be 0 and 9 respectively (ex. X0 - X9).

-> X0XXX - X9XXX

The last three digits should make X0 and X9 the closest possible. Therefore the remaining digits of X0 should result in the smallest possible number, and X9 the highest.

From 1,2,3,4,5,6,7,8 we have 123 and 876.

-> X9123 - X0876

The two remaining numbers are 5 and 4 -> 59123 - 40876 = 247

Cornio Wassig
Jun 10, 2018

I did the worst possible thing and generated 2 random numbers and let the Computer substract them in hope the result is less then it was before: (In java)

import java.util.Arrays;
import java.util.Random;

public class smallestPositiveDifference {
    public static void main(String[] args) {
        /*
            All of the digits from 0 through 9 are used to create two 5-digit positive integers,
            and then the two numbers are subtracted to find their positive difference:
            What's the smallest positive difference we can get this way?
         */
        Random random = new Random();

         int[] blockA = {0,1,2,3,4,5,6,7,8,9};
         int[] blockB = {0,1,2,3,4,5,6,7,8,9};

         int x = 100000;

         while(true){
         shuffleArray(blockA);
         int[] a = Arrays.copyOfRange(blockA, 0, 5);
         int[] b = Arrays.copyOfRange(blockA, 5, 10);
         String d = a[0] + "" + a[1] + "" + a[2] + "" + a[3] + "" + a[4];
         String e = b[0] + "" + b[1] + "" + b[2] + "" + b[3] + "" + b[4];
         int y = Integer.parseInt(d);
         int z = Integer.parseInt(e);

         if(y-z >= 0 && y-z < x){
             x = y-z;
             System.out.println("which is greater than " + x);
         }
         }
    }

    private static void shuffleArray(int[] array)
        {
            int index, temp;
            Random random = new Random();
            for (int i = array.length - 1; i > 0; i--)
            {
                index = random.nextInt(i + 1);
                temp = array[index];
                array[index] = array[i];
                array[i] = temp;
            }
        }

}

May the brute force be with you

Geneveve Tudence
Jun 8, 2018

In order to find the positive smallest difference, the 1st digit of the SUBTRAHEND should be equal to the 1st digit of the MINUEND less than 1. Also the next digits of the MINUEND should be from the least to greatest, and the next digits of the SUBTRAHEND should be from the greatest to least. By an organized trial and error we can have the following:

(Note that the first digit of a number should not be zero.)

20345-19876=469

30145-29876=269

40125-39876=249

50123-49876=247

60123-59874=249

70123-69854=269

80123-79654=469

90123-87654=2469

Clearly, the smallest positive difference is 247.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...