n^2 array search algorithm [duplicate] - algorithm

This question already has answers here:
find an element in a sorted matrix [duplicate]
(9 answers)
Search a sorted 2D matrix [duplicate]
(4 answers)
Most efficient way to search a sorted matrix?
(9 answers)
Find number in sorted matrix (Rows n Columns) in O(log n) [duplicate]
(5 answers)
Closed 5 years ago.
In sorted array, we can search in O(logn) by binary search.
But I thought that in n*n array, how can we apply this algorithm(or other) to the array to search faster?
n*n list is sorted each row and column like below.
1 3 7 13 19
2 5 12 14 20
4 9 15 16 22
8 10 18 23 25
11 17 21 24 27

Obviously the naive solution is to perform binary search on each row (or column) which will result in a runtime complexity of O (n log n).
The main problem I see in directly adapting binary search to the whole matrix is that there is no complete ordering. This makes it hard to define a "split element" where all elements to the "left" are smaller and all elements to the "right" are larger.
My approach for a direct adaptation would be closer to a spatial index like a quad-tree. The basic observation is the following: For each sub-matrix of the original matrix you can define bounds of the elements by looking at the top-left (lower bound) and bottom-right (upper-bound) element.
Now you can basically perform depth first search, recursively splitting the matrix in 4 sub matrices, computing upper and lower bounds for each sub matrix and discarding or exploring it depending if the key is within its bounds.

Related

Add numbers from list of elements. O(1) Logic c++ [duplicate]

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.

Intersection of trinary matrices [closed]

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.

Given a matrix of ints, find the longest consecutive snake of incrementing by 1 numbers [duplicate]

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.

Determine if sum is a power of 2 [duplicate]

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.

How does one find the median of 5 distinct values with 6 comparisons? [duplicate]

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.

Resources