This question already has answers here:
How to find sum of elements from given index interval (i, j) in constant time?
(5 answers)
Closed 4 years ago.
I have large array with random number stored in it. I want to create a function which returns the sum of numbers from given starting and end position in O(1) time.
Is there another method of storing number to achieve O(1) ?
It’s not possible to make a O(1) code, to get the sum of numbers.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Consider 3 matrices of binary variables b0,b1,b2,b3. All these matrices have same number of column but can be different number of rows. Each element of the matrix can have three values 1,0 or 2 where 2 represent don't care. I have to find binary strings that presents in all three matrices. For example consider the following 3 matrices:
matrix1:
1 0 2 2
2 2 0 0
1 2 1 1
matrix2:
2 2 0 2
1 0 1 2
matrix3:
2 2 1 2
1 2 2 1
2 2 2 1
So, for this example string b0=1,b1=0,b2=1,b3=1 is present in all matrices. Because, in matrix1, b0=1,b1=2,b2=1,b3=1 is same as 1011. In matrix2, b0=1,b1=0,b2=1,b3=2 is same as 1011 and in matrix3, b0=2,b1=2,b3=2,b3=1 is same as 1011.
How to find all binary strings that exists in all 3 matrices?
The idea is to "expand" each row to its set of possibilities, so for example 1022 gets expanded to:
1000
1001
1010
1011
Then, it's convenient to convert each string to an integer (a single byte integer since the "strings" are 4 bit long) and place in a sorted array, or even a set.
Next step is to sort groups by length, from the smallest to the largest, then iterate the smallest group values and see that it exists in all other groups, this is very fast because of the preparation work in the "parsing" step.
Every value that passes for all groups is a match.
I suppose that simplest and reasonably efficient algorithm will be to brute-force check all possible combinations. Start with 0000 then 0001, then 0010 etc. With each of them, iterate each matrix and compare values. On first match, go to next matrix, on non-match, immediately reject.
You will have to iterate each matrix maximum 16 times, which is still O(N) from size of matrix.
If you want to optimize actual comparison, you can precompute lookup strings for each matrix. Create reverse bitmasks for 0-allowed and 1-allowed and AND them with bitmasks of has-0 and has-1 of query string. If any of two results is non-zero (you can just add or OR them and check result), string won't be matching.
In any case, it should be very fast with any kind of comparison implementation, as you will be doing only 16*(1000+1000+1000) rather than (1000*1000*1000) operations you probably were considering.
This question already has answers here:
Find maximum length of good path in a grid
(4 answers)
Closed 7 years ago.
Basically, you have something like this:
0 9 5 3'
4 1 5' 4'
5 7' 6' 9
2 8' 5 10
In this case, the longest snake would be 3 -> 4 -> 5 -> 6 -> 7 -> 8. I put ' behind the numbers in this to help show it visually.
You can go both horizontally and vertically. The matrix can be n x m, so there isn't really a limit to the number of rows and columns.
What is the most optimal way to figure this out?
I've thought about starting at position n/2 and m/2, then recursively doing breadth-first search and keeping track of the max interval I can find. I'm not sure how to best tackle it.
You could create a graph where nodes are matrix positions and vertices are pointing from a number N to a N+1 neighbour.
Once the graph is built, your problem amounts to finding one of the longest paths in this graph.
This question already has answers here:
How to check if a number is a power of 2
(32 answers)
Closed 8 years ago.
What is the fastest (in asymptotic worst-case time complexity) algorithm for determining if a sum of arbitrary positive integers is a power of two?
One cute bit twiddling trick is to test if x&(x-1) is equal to 0.
Note that you need to decide what to do if x is equal to 0, this test will mark 0 as a power of 2 so you may want an exception for this case.
Subtract 1 from the sum and perform a bitwise AND with the original number. Powers of 2 will have a result of 0.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Code to calculate “median of five” in C#
Suppose I have five numbers in a list, say [3,2,1,4,5].
How do I find the median with 6 comparisons?
Take an optimal sorting network for N=5 which requires 9 comparisons and then prune the unneeded comparisons for outputs 0, 1, 3 and 4. There's an example of this which uses 6 comparisons in the answer to this question.