-1

In n-dimensional grid (Max. 10^7 dimensions) are two points. They have imaginary sensors on every axis. I need algorithm what will calculate all possible options when these two points can spot themselves.

Formal written from my task document (translated to english):
Let A with the coordinates (a1, a2, ..., an) and B with the coordinates (b1, b2, ..., bn) are two points in n-dimensional space and exists i ∈ 1, 2, ..., n such that ai = bi then A and B sees each other

Example:
In 1-dimensional space with length 10 is total 45 combinations how to put 2 points when they see each other (They see each other every time). It is easy combination 10C2 (10 of 2) = 45

How to calculate it in 2,3,4,...,10^7 dimensions by program (I prefer C#)?

Proper test data what i have:
Input:
1
10
Output: 45

Input:
2
5 8
Output: 220

Input:
3
8 12 11
Output: 14784

More explanation:
Output is number of combinations when two points in space see each other (are on same axis). In 1 dimensional space is only one axis so they see each other always. In 2 dimensional space are 2 axis, so they can see each other only in some case

This image example explaining more than text i think

  • 1
    Take a look at [ask]. – PJvG Mar 24 '17 at 14:54
  • 1
    Please give me same example with proper data and result. – Nayan Patel Mar 24 '17 at 14:55
  • I added test inputs and outputs to question –  Mar 24 '17 at 15:14
  • OK this good but please add answer with logic because why answer 45,220 and 14784. – Nayan Patel Mar 24 '17 at 15:20
  • when they do not see each other? do they have some kind of range of view or something? – Arkadiusz Raszeja Mar 24 '17 at 15:27
  • @NayanDhamsaniya I only can imagine it in 1 and 2 dimensional space. Look at the image it describes problem more than text. 45 is because output is number of combinations when two points in space see each other (are on same axis). In 1 dimensional space is only one axis so they see each other always. In 2 dimensional space are 2 axis, so they can see each other only in some case –  Mar 24 '17 at 15:29
  • @ArkadiuszRaszeja no they dont have they "see" to all in all directions to the end of space –  Mar 24 '17 at 15:30
  • So they se each other only if they are on the same axis, right? If so, on two dimentions of lenghs of 5 and 8 they have 5!/2!3! + 8!/2!6! = 38 ways – Arkadiusz Raszeja Mar 24 '17 at 15:34
  • @ArkadiuszRaszeja yes sure, they cant see point on other position. They "see" only on axis but range is unlimited. –  Mar 24 '17 at 15:35
  • @PJvG it is correct. Combination 10C2 = 45 –  Mar 24 '17 at 15:36
  • Formal written from my task: Let A with the coordinates (a1, a2, ..., an) and B with the coordinates (b1, b2, ..., bn) are two points in n-dimensional space and exists i ∈ 1, 2, ..., n such that ai = bi , Points A and B sees each other –  Mar 24 '17 at 15:39
  • Are you sure your second example is correct? They can meet on first axis with lenght of 5, so on 10 ways and because we have 8 axis like this it is 80 possibilities to being on the same line in one dimention. Than we have second dimention of lenght of 8 so 29 possibilities times 5 lines like that so 145. In total its 80+145 = 225 ways to be on the same axis – Arkadiusz Raszeja Mar 24 '17 at 15:46
  • @ArkadiuszRaszeja it is absolutely correct. This is a document from college as the correspondence seminar –  Mar 24 '17 at 15:52
  • You think you need to use combinations, but really your problem should be solved using permutations. The number of permutations of a 1 dimension grid with 10 positions is 10*9 = 90. – PJvG Mar 24 '17 at 15:56
  • yes i got it for why result 45. i will try and post of this after completed. – Nayan Patel Mar 24 '17 at 15:56
  • @PJvG but result must be 45. I get it why you think it will be 90 but it is all combinations with repeats i think. i need without repeats –  Mar 24 '17 at 16:02
  • Could someone explain second example? When I'm trying to count it i get 225 and second I get correctly (14784) – Arkadiusz Raszeja Mar 24 '17 at 16:07
  • @ArkadiuszRaszeja you must count bad. check if you dont have doubles. I've gradually got 45,40,35,30,25,20,15,10 = 220 (Every row number of combinations is lower X times and there is Y rows). It makes sense –  Mar 24 '17 at 16:25
  • Why is this question closed for seeking debugging help? This is an [algorithm] question which doesn't require code - OP just states that he would prefer an algorithm outlined in C# instead of pseudocode. – le_m Mar 25 '17 at 17:43
  • @le_m I can program it myself, I only needed some kind of logic like Arkadiusz Raszeja did –  Mar 26 '17 at 18:46

2 Answers2

1

I'm sure it's correct.
C(x,y) is combination x of y.
Lets say we have one dimention, lets call it X of lenght 8. There are C(8,2) = 8*7/2 = 28 possibilities to see eachother.
When we add second dimention, named Y, of lenght 12 now we have 12 lines parallel to X. So we have 12*28=336 possibilities to being found on all the lines parallel to X. Now, on Y dimention we have C(12,2) = 66 possibilities. And there are 8 lines like that so 66*8=528.
In total: 336 +528= 846 possibilities
Now lets add another dimention, labeled as Z with lenght of 11. There are C(1,2) = 11*10/2 = 55 in one line and (atention) we have 8*12 lines like that. So is it's 55*8*12 = 5280 possibilities!
Now in total we have:
Paralel to X axis: C(8,2)*11*12 = 3696
Parallel to Y axis C(12,2)*8*11 = 5808
Parallel to Z axis C(11,2)*8*12 = 5280
TOTAL = 14784

In general the formula for n dimentions with n1,n2... nk lenghts is:
Sum of C(ni,2) * (n1*n2...*nk)/ni
Or shorter: sum of (n1*n2*n3...nk)/2 * (ni-1)

example: dimentions with 3,8,9,11:
(3*8*9*11)/2*(3-1) = 2376
(3*8*9*11)/2*(8-1) = 8316
(3*8*9*11)/2*(9-1) = 9504
(3*8*9*11)/2*(11-1) = 11880
Total = 32076

The easiest equasion:
(n1*n2*n3...ni)(n1+n2+...ni - k)/2, where ni are lenghs, and k is number of dimentions

  • 1
    You are absolute genius man! I was on way to find out this because there is a rule that causes a linear decline when you doing this step by step. Hats off and thanks! This is right –  Mar 24 '17 at 16:53
  • I like to make me brain sweat :) Nice to help, and meet you :) I wish you menny successes on your career :) – Arkadiusz Raszeja Mar 24 '17 at 16:55
  • I can send you all tasks from my correspondence seminar :D All tasks are mind blowing but this much more than others. It is in czech language so if you translate it you can solve it all and make your brain more hotter :D –  Mar 24 '17 at 16:59
  • shit for 11 dimensions it seems not workl lol :D why –  Mar 24 '17 at 18:39
  • the problem might be in lenght of int (or event unsigned int). It's about 2 mld, after multiplaying 11 numbers it might get crush. I'm Polish, my slavic brother :) – Arkadiusz Raszeja Mar 24 '17 at 19:11
  • Try to construct your own type, like string containing number and then define operators like "*" or "+". Then lenght of your number can be almost infinit. uLong can handle 20 dogits, decimal 29. That is the limit of "normal" variables. – Arkadiusz Raszeja Mar 24 '17 at 19:13
  • I figured that long is enough but still its not correct. I will make research but maybe just task is not good written :) –  Mar 24 '17 at 19:29
  • So you maybe can translate tasks better :D –  Mar 24 '17 at 19:30
  • why you think it's not correct? You get error, or you have different values than book requires? Send an example. – Arkadiusz Raszeja Mar 24 '17 at 19:32
  • O got different. I will send it tommorow. I think its bad in task. not in algorithm –  Mar 24 '17 at 22:26
0

I have create sample code so you can try it. i have check it's working fine.

Equation is : C(n,r)=n!/(r!(n−r)!)

Example: 1. 10C2=45 2. 10C3=120

public void Calc()
{
  int result= nCr(10, 3);
}

public int nCr(int n,int r )
{   
   int nValue=1;
   int rValue = 1;
   for (int i = n-r+1; i <= n; i++)
   {
       nValue = nValue*i;
   }
   for (int i = 1; i <= r; i++)
   {
        rValue = rValue*i;
   }
   return nValue/rValue;
}
Nayan Patel
  • 154
  • 7
  • How can i test it for 2 dimensional array with 5x8 grid? I think this is not right answer –  Mar 24 '17 at 16:25
  • this use only one dimensional please give me Equation for two dimensional so i will try it. – Nayan Patel Mar 24 '17 at 16:28
  • problem is it must be for n-dimensional, with maximal value 10^7 dimensions. And teachers says it can be done with complexity O(N)!! –  Mar 24 '17 at 16:41
  • problem is solved. thanks for your help. @ArkadiuszRaszeja got it :D –  Mar 24 '17 at 17:00
  • Most welcome. good work. good work ArkadiuszRaszeja... – Nayan Patel Mar 24 '17 at 17:04
  • Can someone please upvote this? I have ban because downvotes on good questions :( @ArkadiuszRaszeja –  Oct 17 '17 at 18:58