edited by
698 views
1 votes
1 votes

Let $A$ be an $n\times n $ matrix of integers such that each row and each column is arranged in ascending order. We want to check whether a number $k$ appears in $A.$ If $k$ is present, we should report its position - that is, the row $i$ and column $j$ such that $A(i,j)=k.$ Otherwise, we  should declare that $k$ is not present in $A.$

  1. Describe an algorithm that solves this problem in time $O(n\log n).$ Justify the complexity of your algorithm.
  2. Describe an algorithm that solves this problem by examining at most $2n$ values in $A.$ Justify the complexity of your algorithm.
  3. For both algorithms, describe a worst-case input where $k$ is present in $A.$
edited by

2 Answers

0 votes
0 votes
(a) Since, each row is sorted in the ascending order therefore we can use Binary search on each row, which works in $O(logn)$ time.

For $n$ rows, time complexity $=$ $O(nlogn)$

 

PS will write b and c by evening.

Related questions

3 votes
3 votes
3 answers
4
gatecse asked Sep 13, 2019
1,874 views
Suppose you alternate between throwing a normal six-sided fair die and tossing a fair coin. You start by throwing the die. What is the probability that you will see a $5$...