3 3 votes You are given an $n \times n$ matrix where each row and each column is sorted in ascending order. You need to search for a target element "x" in the matrix.What is the best possible asymptotic bound in the worst case to solve this problem?$\Theta(\log n)$ $\Theta(n)$ $\Theta\left(n^2\right)$ $\Theta(1)$ Algorithms goclasses algorithms goclasses-cs-dpp goclasses-cs-dpp-day-69 goclasses-algorithms-practice-questions + – GO Classes 355 views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
3 3 votes This task can be done in $\theta(n)$ time using the following approach:We have an $n \times n$ matrix:Each row is sorted in ascending order (left $\rightarrow$ right). Each column is sorted in ascending order (top $\rightarrow$ bottom). Start from the top-right corner (row = 0, col = n-1). Compare with target x. .Rules:1. If $\operatorname{arr}[\mathrm{row}][\mathrm{col}]==x \rightarrow$ Found.2. If $\operatorname{arr}[\mathrm{row}][\mathrm{col}]>x \rightarrow$ Move left (col--).3. If arr[row][col] $<x \rightarrow$ Move down (row++).In the worst case:We move $n-1$ steps down and $n-1$ steps left. Total $=2(n-1)$ steps $=\Theta(n)$. GO Classes answered Aug 28, 2025 • reshown Aug 29, 2025 by GO Classes GO Classes comment Share Follow See 1 comment 1 1 comment reply Rajkumar Chaudhary commented Nov 10, 2025 reply Follow flag Got it! 0 0 replyShare Please log in or register to add a comment.
0 0 votes N*N Matrix are Sorted in Both Direction Then Use Bineary Serch for First Col. and Then Row For Row Log(n)+ For Coloum Log(n) O(Log n) A is correct Answer amit kurmi answered Aug 28, 2025 amit kurmi comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes answer is B for sigle n array it take log n then for n2 it will take n because in worst case scenario we have to traverse through n columns or rows Gaurav_sharma 1 answered Sep 4, 2025 Gaurav_sharma 1 comment Share Follow 0 reply Please log in or register to add a comment.