Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#157 solved #324

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def search_matrix(matrix, target):
if not matrix or not matrix[0]:
return False

rows = len(matrix)
cols = len(matrix[0])

# Start at the top-right corner
row = 0
col = cols - 1

while row < rows and col >= 0:
if matrix[row][col] == target:
return True
elif matrix[row][col] < target:
row += 1
else:
col -= 1

return False


matrix = [
[1, 2, 3, 4, 5],
[2, 6, 7, 8, 9],
[3, 10, 19, 16, 22],
[4, 13, 14, 17, 24],
[5, 21, 23, 26, 30]
]

target = 21
result = search_matrix(matrix, target)
print(f"Element {target} found in the matrix: {result}")