I came across an interview questions and despite the fact I've been trying to solve it on my own I think I need some help.
I've got an array of integer numbers (positive and negative) representing points in space, the distance between two points is defined as abs(A[i]-A[j]) and I need to check that that distance is divisible by a given integer M.
So this is the situation :
Array : [-3 -2 1 0 8 7 1]
M = 3
abs(A[1]-A[2]) = 3 (for example and it's divisible by the integer)
The complexity should be O(N+M) and the space O(M)
Now these are the questions
1)I know there is a way to take in consideration al the couples without using the obvious solution with two "for loops" because the complexity would be N^2 which is undesirable, but I can't figure out how to do it
2)Complexity O(N+M) means that I need to use two for loops but not one inside another?(I mean two separate for loops), what I am trying to understand here it's if the complexity given can guide me toward the best algorithm I should use.
3)When the specification says that the integer name is M and the complexity is O(N+M), does this mean that there is a relation with the integer M and the complexity or it's only a case that the name is the same?
4)How to do it ?
I hope I have been clear enough, if not please let me know I wil try to explain myself better.
Ok Let's see if I understood correctly this is what I am trying so far :
int testCollection[7];
testCollection[0] = -3;
testCollection[1] = -2;
testCollection[2] = 1;
testCollection[3] = 0;
testCollection[4] = 8;
testCollection[5] = 7;
testCollection[6] = 1;
int arrayCollection[7];
for (unsigned int i = 0; i < 7; i++)
{
arrayCollection[i] = 1000;
}
for (unsigned int i = 0; i < 7; i++)
{
arrayCollection[i] = testCollection[i]%3;
}
the arrayCollection now contains: [0, -2, 1, 0, 2, 1 ,1 ]
I did not understand what you mean for second time can you please be a little bit more specific? Imagine I am a child :)
cheers
p.s. I don't want to disturb you too much so if you prefer you can point me to some documentation I can read about the subject, googling I did not find much unfortunately.