I have a matrix (n*n) which all its columns are ordered in non descending order while the rows are not ordered - I know that each value in a row is smaller than all the values in the rows below, but the values inside the rows are not sorted, although they are all either smaller or identical to the values in the row below.
I need to find a value in this matrix in O(n). I thought of running a binary search on one of the columns until I get into one cell which is the closest one to my searched value (in case, of course, I did not find the value during the search).
Then, I will check for the searched value in the row of the cell and in the row above (if cell > searched_val) or the row below (if cell < searched_val).
Will it work? Will it also work in case that the column is full with identical values?
I know that each value in a row is smaller than all the values in the rows below
This means that the rows are ordered in ascending order
So, starting from your definition, both rows and columns are ordered.
In this way you can search your Matrix as an array of size n*n. Binary search in this case will go:
O(log(n*n)) = O(2*log(n)) = O(log(n)) < O(n)
You just have to define an index that moves in the Matrix in ascending (or descending) order.
Related
I'm new to data structures. I have been working for the past 72 hours to find an algorithm to insert a particular value into a singly linked list based on row and column index. I created the singly linked list based on the SPARSE MATRIX below.
I have attached the image of the linked list above. For an example, if i wanted to insert a value at row 0 and column 4 with the value of 8. What is the most suitable algorithm to make this happen ? Thanks in advance guys
An interesting point to consider.
First if you flatten your matrix, then you can notice that
the cell at (0,1) (at row 0 and column 1) becomes the cell of index 1.
the cell at (1,0) becomes the cell at index 5.
More generally, the cell (i,j) becomes the cell at index i * row_size + j
Using this observation you can go through the list until the computed index of the cell you want to insert is smaller than the computed index of the current element.
If you know how to insert a node at a specific position in a linked list (which i recommend you try first if you dont) it should be easy to make the bridge between the two.
Was asked this question in a coding round:
Given a matrix of 0's and 1's where, in any row - the values will be ascending order. i.e 1's are always after the 0's. Consider the example :
0,0,0,1,1
0,0,1,1,1
0,0,0,0,1
1,1,1,1,1
0,0,0,0,0
Find the first column that has a 1. ( from left - right )
In this case the first column ( in row 4 ) has a 1.
Answer is 1
I suggested a column wise traversal across all rows and exit when the current column encounters 1 in any of the rows.
Since the worse case performance is n * n ( comparing every element in the matrix) the interviewer wasn't pleased and was looking for a efficient solution - what is an efficient solution here ?
Take advantage of the fact that the rows are sorted which is evident from "in any row - the values will be ascending order. i.e 1's are always after the 0's"
Let there be m rows and n columns. Do a binary search on first row to figure out the first 1 and store that index in some variable, say index (One may think of a better variable name. I am just focused here on solving the problem optimally.) Continue binary search on every row, update the index if the first column containing 1 has lesser index than the index. After doing binary search on every row, you'll end up with the result in index variable.
Time complexity: m rows * log2(n columns) i.e. O(m * log2(n)).
This is the approach I could think of, which is better than the brute force approach having O(mn) time complexity. I don't think there would be a more optimal approach in terms of time and space complexity, as one has to search for the first 1 in every row.
[I don't think I should add the details on how to do a binary search to figure out the first column containing a 1. In case someone isn't very familiar with binary search, I leave this trivial part as an exercise.]
I have an excel file that holds a column with data in numbers, when I try to sort it, it's sorted like this:
original list:
1,99,1011,33122,4589,330
sorted now:
1,1011,330,33122,4589,99
need to be sorted like this:
1,99,330,1011,4589,33122
How can I do that?
Select a single cell from the column you want to sort.
On the Data tab, in the Sort and filter group, click. to rank in ascending order (from A to Z or from the smallest to the largest number).
Click on. to sort in descending order (from Z to A or from the largest to the smallest number).
Assume that we have an Mystery-Sort(A), which takes an array A of
length n as input, sorts the numbers in A in non-decreasing order, and returns the sorted array.
We do not know whether the algorithm implemented by Mystery-Sort is stable.
I need a procedure that takes an array of n integers and returns the sorted array in non-decreasing order, but i need the procedure to be stable.
How can i achieve this the pseudo-code of a stable sorting procedure Stable-Sort(A), which pre-processes and/or postprocesses
the elements in A in O(n) time, makes only one call to Mystery-Sort, and returns
the sorted array in non-decreasing order.
I see this as coming in two phases: a pre-processing phase in which you find all duplicated elements with their identifiers; a post-processing phase where you simply overwrite the found elements into their original order.
You didn't specify how you can differentiate elements that sort as the same value; I'll call that the id. In this first pass, construct a table with one row per value. Iterate through the array; store the id of each element (or the entire element) in the matching table row in the table. If there's already an element there, extend that row and store the current element.
At this point, if you wish, you can eliminate any row of the table with fewer than 2 elements.
For the post-procesing, iterate through the sorted array. If the value you find is in the table, then don't trust the order returned from Mystery-Sort. Instead, simply overwrite the next elements with the ones from that row of the table. This restores their original order.
When you reach the end of the sorted list, you're done.
Lets use the following table below. How can I efficiently check if the value 11 is in the table? Note that the numbers in the yellow may not always be consecutive. Looping through all the values is n^2 but that's not very efficient.
One possible solution is the following - put all the numbers either from the yellow row or the yellow column in some set, say hash set. Let's use the row for an example. After that iterate over the column and for each number x check if the number A - x is in the hash set(in your case A is 11). This approach would lead to linear complexity and linear additional memory. You do not need the hash set if you know that the numbers are sorted to get the same computational complexity.