Here is the interview question prompt, presented for reference.
Suppose you have a matrix where each row and column is sorted in ascending order. Your task is to apply two specific types of shifts, described below, and locate a given element x
in the transformed matrix.
Left Circular Shift (l): - Definition: Moves the first column to the last position, shifting all other columns one step left. - Example:
Before:
py
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
After:
py
[
[2, 3, 1],
[5, 6, 4],
[8, 9, 7]
]
- Visual Explanation:
Imagine the columns on a circular conveyor belt. The first column wraps to the end, while others shift left.
Top Circular Shift (u): - Definition: Moves the first row to the last position, shifting all other rows one step up. - Example:
Before:
py
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
After:
py
[
[4, 5, 6],
[7, 8, 9],
[1, 2, 3]
]
- Visual Explanation:
Picture the rows on a circular escalator. The first row descends to the bottom, while others shift up.
Let's consider a sequence of shifts like [l,u,l,u,u]
and apply them to the given matrix:
matrix = [
[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9,10],
[11,12,13,14,15],
[16,17,18,19,20]
]
After applying these rotations, the matrix transforms to:
matrix = [
[9 ,10, 6, 7, 8],
[14,15,11,12,13],
[19,20,16,17,18],
[ 4, 5, 1, 2, 3]
]
Here's an illustration to help visualize the problem: ![Problem Illustration](https://i.ibb.co/w74Rhbs/problem.png)
Given this final matrix and a specific value x
, how can you efficiently find the location of this element in the array and return it?
10^5 x 10^5
O(nlogn)
O(1)
x
is in the matrix.Considering these constraints and the above example, can you develop a method to locate the specified element in the transformed matrix?
You can see the full challenge with visuals at this link.
Challenges • Asked almost 6 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Find in Shifted Sorted Matrix.