Matrix Game(D)

0

0 votes
Problem

Rohit is told to find a number in a given N*N matrix. If the number is present in the matrix, then he should print the sum of indices of the first occurrence of that number(when traversed row-wise), else he should print '-1'.(Consider zero-based indexing).

INPUT FORMAT:

  • The first line contains a single integer T denoting the number of test cases.
  • Each test case contains two integers: N(size of matrix) and X(number to be searched).

OUTPUT FORMAT:

For each test case, print the sum of indices of the first occurrences of X if present, else print '-1'.

Constraints:

0 <= T <= 100

0 <= N <= 20

0 <= X, mat[i][j] <=30.

 

Sample Input
2
2
1 2
3 4
5
2
1 1
3 5
5
Sample Output
-1
2
Time Limit: 5
Memory Limit: 256
Source Limit:
Editor Image

?