Question: USING JAVA WITH TIME COMPLEXITY OF O(N+M) Sherlock Holmes loves mind palaces! A mind palace, according to Mr. Holmes is something that lets him retrieve

USING JAVA WITH TIME COMPLEXITY OF O(N+M)

Sherlock Holmes loves mind palaces! A mind palace, according to Mr. Holmes is something that lets him retrieve a given memory in the least time possible. For this, he structures his mind palace in a very special way. Let a NxM Matrix denote the mind palace of Mr. Holmes. For fast retrieval, he keeps each row and each column sorted. Now given a memory X, you have to tell the position of the memory in Sherlock's mind palace.

Input:

Take input from in1.txt. First line contains number of test cases T. Each test case begins with a line containing space separated N and M. The next N lines each contain M numbers (space separated), each referring to a memory Y (no duplicates). The next line contains Q, the number of queries. The next Q lines contain a single element X, the memory you have to search in Sherlock's mind palace.

Output:

If Y is present in Mr. Holmes memory, output its position (0-based indexing).

Else output "-1 -1" (quotes for clarity only).

Constraints:

2 ? N , M ? 1000

2 ? Q ? 1000

-10^9 ? X,Y ? 10^9

SAMPLE INPUT:

1

5 5

-10 -5 -3 4 9

-6 -2 0 5 10

-4 -1 1 6 12

2 3 7 8 13

100 120 130 140 150

3

0

-2

170

SAMPLE OUTPUT:

1 2

1 1

-1 -1

Expected Complexity: less than or equal to O(N+M)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!