Interview for SDE in Walmart

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-08-16Added By Anonymous
Source: Link

Given a square matrix A of size N*N and an integer X, where X is an element of A, perform the following steps:

I) Find the index of the row R and column C of X in A.

II) Calculate the sum S of R and C, i.e. S = R+C.

III) Find the element Z which is at S row(s) below X, in the same column.

IV) Print P, the sum of digits of Z.


While identifying Z, if the end of the matrix is reached, then traversing has to be continued from the beginning of

the matrix.


Read the input from STDIN and print the output to STDOUT. Do not write arbitrary strings while reading the input or

while printing, as these contribute to the standard output and testcases will fail.


Constraints:

I) Elements of A are unique integers.

II) 1 < N <= 35


Input Format:

The first line of input contains N.

Next N lines of input contain N integers each, separated by a single white space. These are the matrix elements.

The last line of input contains X.


Output Format:

The output contains P.

Sample Input1:

4

454 55 521 14

11 329 104 989

23447 174 87 845

25 81 53 111

11


Sample Output1:

20


Explanation1:

Given matrix is

454 55 521 14

11 329 104 989

23447 174 87 845

25 81 53 111

X = 11.

Index of 11→ R=1 and C=0. Therefore, S = R+C = 1+0 = 1.

The element which is 1 row below 11 is 23447.

Sum of digits of 23447 = 2+3+4+4+7 = 20

Hence output is 20.


Sample Input2:

3

11 22 33

44 55 66

77 88 99

66


Sample Output2:

12


Explanation2:

Given matrix is

11 22 33

44 55 66

77 88 99

X = 66. Index of 66-> R=1 and C=2. Therefore, S = R+C = 3.

The integer which is 1 row below 66 is 99.

The integer which is 2 rows below 66 is 33.

The integer which is 3 rows below 66 is 66 itself.

Sum of digits of 66 = 6+6 = 12.


Hence the output is 12.

Comments