2

I need to find a way, such that user has to input 2 numbers (int) and for every different value a single output (int preferably!) is returned. Say user enters 6, 8 it returns k when user enter anything else like 6,7 or 9,8 or any other input m, n except for 6, 8 (even if only one input is changed) a completely different output is produced. But the thing is, it should be unique for only that m, n so I cant use something like m*n because 6 X 4 = 24 but also, 12 X 2 = 24 so the output is not unique, so I need to find a way where for every different input, there is a totally different output that is not repeated for any other value.

EDIT: In response to Nicolas: the input range can be anything but will be less then 1000 (but more then 1 of course!)

EDIT 2: In response to Rawling, I can use long (Int64) but not preferably use float or doulbe, becuase this output will be used in a for loop, and float and double are terrible for for loop, you can check it here

Razort4x
  • 3,296
  • 10
  • 50
  • 88

5 Answers5

7

Since your two numbers are less than 1000, you can do k = (1000 * x1) + x2 to get a unique answer. The maximum value would be 999999, which is well within the range of a 32-bit int.

Chowlett
  • 45,935
  • 20
  • 116
  • 150
6

You can always return a long: from two integers a and b, return 2^|INT_SIZE|*a + b

It is easy to see from pigeonhole principle, that given two ints, one cannot return a unique int for every different input. Explanation: If you have 2 numbers, each containing n bits, then there are 2^n possibilities for each number, and thus there are (2^n)^2 possible pairs, so from piegeonhole principle - you need at least lg_2((2^n)^2) = 2n bits to represent them,

EDIT: Your edit mentions the range of your numbers is [1,1000] - thus the same idea can be applied: 1000*a + b will generate a unique int for each pairs.
Note that for the same reasons, the range of the resulting integer must be [1,1000000] - or you will get clashes.

amit
  • 175,853
  • 27
  • 231
  • 333
1

Because I don't have 50 posts to comment, I must say, there are functions called Pairing Functions.

Pairing functions such as Cantor's Pairing Function(Shown on the previous link) and Szudzik's Pairing Function which allows the inputs to be infinite and still be able to provide an unique and deterministic output.

Here is another similar question on stackoverflow. (Great, I need 10 reputation to post more than two links..)

(http://) stackoverflow.com/questions/919612/mapping-two-integers-to-one-in-a-unique-and-deterministic-way

EDIT: I'm late.

  • 2
    First of all, thank you for pointing out the duplicate target; I voted to close the question as duplicate. To improve the quality of this answer, you’d need to _include the relevant bits of the linked articles into the question_, such as Cantor's Pairing Function itself. Then, as a minimal answer, it would be mostly safe from being flagged. However, try to answer questions, that _aren’t duplicates_ and that are _clear and answerable_. Also, read the [tour](http://stackoverflow.com/tour) page. – Sebastian Simon Dec 31 '15 at 17:02
0

If you didn't have a hard upper bound, you could do the following:

int Unique (int x, int y)
{
    int n = x + y;
    int t = (n%2==0) ? ((n/2) * (n+1)) : (n * ((n+1)/2));
    return t + x;
}

Mathematically speaking, this will return a unique non negative integer for each (non-negative) pair of integers with no upper bound.

Programatically speaking, it will run into overflow problems, which could be overcome by using long instead of int for everything except the input variables.

Rawling
  • 49,248
  • 7
  • 89
  • 127
0

The canonical mathematical solution is to use prime powers. As every number can be decomposed uniquely into its prime factors, returning 2^n * 3^m will give you different results for every n and m.

This can be extended to 2^n * 3^m * 5^a * 7^b *11^c and so on; you only need to check that you do not run out of 32-bit integers. If there is a risk of overflowing, you can take the remainder after dividing by a prime larger than your input range, and you will still have uniqueness.

Joel in Gö
  • 7,460
  • 9
  • 47
  • 77
  • I would have said that Cantor's pairing function was closer to being the canonical solution for this problem ( since it is the canonical solution for the related problem of mapping the set of all pairs of integers to the set of integers ), as well as requiring less computation. – Pete Kirkham May 29 '12 at 19:17
  • Fair point; depends on whether you are coming from number theory or set theory I guess :) – Joel in Gö May 30 '12 at 07:20