# Matrix Search - Amazon Top Interview Questions

### Problem Statement :

```Given a two-dimensional integer matrix, where every row and column is sorted in ascending order, find the kth (0-indexed) smallest number.

Constraints

n, m ≤ 250 where n and m are the number of rows and columns in matrix

Example 1

Input
matrix = [
[1, 3, 30],
[2, 3, 31],
[5, 5, 32]
]
k = 4

Output
5

Example 2

Input

matrix = [
[1, 2, 3]
]
k = 0

Output
1

Example 3

Input
matrix = [
[1],
[2],
[3]
]
k = 2

Output
3```

### Solution :

```                        ```Solution in C++ :

int solve(vector<vector<int>>& A, int k) {
int M = A.size();
int N = A[0].size();

int t = N * M - k;

int l = 0;
int h = 1e7;

while (l <= h) {
int m = (l + h) >> 1;

int cnt = 0;

for (auto x : A) cnt += x.end() - upper_bound(x.begin(), x.end(), m);

if (cnt < t)
h = m - 1;
else
l = m + 1;
}

return l;
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
public int solve(int[][] mat, int k) {
int left = mat[0][0];
int right = mat[mat.length - 1][mat[0].length - 1];
while (left < right) {
int mid = left + (right - left) / 2;
if (possible(mat, k, mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
private boolean possible(int[][] mat, int k, int ele) {
int i = 0;
int j = mat[0].length - 1;
int count = 0;
while (i < mat.length && j >= 0) {
if (mat[i][j] > ele)
j--;
else {
count += (j + 1);
i++;
}
}
return count > k;
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, matrix, n):
def possible(x):
# Is the number of elements y with
# y <= x greater than n?
c = len(matrix[0]) - 1
count = 0
for row in matrix:
while c >= 0 and row[c] > x:
c -= 1
count += c + 1
return count > n

lo = matrix[0][0]
hi = matrix[-1][-1]
while lo < hi:
mi = lo + hi >> 1
if not possible(mi):
lo = mi + 1
else:
hi = mi
return lo```
```

## Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

## Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

## Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

## Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

## Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

## Number Game on a Tree

Andy and Lily love playing games with numbers and trees. Today they have a tree consisting of n nodes and n -1 edges. Each edge i has an integer weight, wi. Before the game starts, Andy chooses an unordered pair of distinct nodes, ( u , v ), and uses all the edge weights present on the unique path from node u to node v to construct a list of numbers. For example, in the diagram below, Andy