tallitester
BAN USERIn this case you would miss out on finding out if kth element was the first or second byte of 2 byte. Run the algorithm with the following array for k=4 and k=5, where k starts from 0. In both cases it will come out of the loop after k=4 and the solution will show B.
[1,1,1,1,200,1]
Incrementing by 2 is not an option.
A better solution would be:
1. If element is between 128 and 255,
1a. Check if previous value was B, current is C, else current is B
2. If element is between 0 and 127,
2a. Check if previous value was B, current is C, else current is A
Let us do it this way. Say, we know that for a 3X3, the number of ways to traverse is 6, for 3X2, it is 3, and for 3X1, it is 1. Now, when we change it to 4X3, the number of paths would be sum of all these three, which is 10, because you move right and then it is a 3X3 matrix, then you move down and right, it is a 3X2 matrix, then down, down and right, and it is a 3X1 matrix. Similarly, for 4X4, it would be N(4X3)+N(3X3)+N(2X3)+N(1X3), which is 20, and so on.
Translating it into code,
//call the function as countpaths(0,0) for m,n matrix
function countpaths(x,y)
{
int j=y,count=0;
for (i=x;i<m;i++)
{
if ((i==m-1) && (j==n-2)) || ((i==m-2) && (j=n-1))
return count + 1;
if (j < n-1)
count =count + countpaths(i,j+1);
}
}
Use a chained hash map. For every key, there exists a linked list, that contains the following structure:
- tallitester January 20, 2012priority;
actual advertisement;
array of countries where it is banned;
When building the list, sort by priority.