Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
588 views
in Online Assessments by Expert (34,270 points) | 588 views

1 Answer

0 like 0 dislike

QUESTION:
Consider this 5x9 array of 1s and 0s.

 

[1 1 0 0 0 1 0 0 0]

 

[0 1 1 0 0 1 0 0 0]

 

[0 0 1 1 1 1 0 0 0]

 

[0 0 0 0 0 0 0 0 0]

 

[0 0 0 0 0 1 0 0 0]

 

Starting from the top left, you start numbering the elements of this array as 1, 2, 3, 4, 5, 6, 7, 8, 9 and as you move to the second row, you start from 10, 11, 12, 13, 14, 15... and so on. So, you are “unrolling” the 2d array into 1D when it comes to numbering the element. So, the 4th element in the second row is numbered “13”.

 

Task

 

From the given array you need to create a string of indexes corresponding to the continuous 1s in the matrix.

 

Sample input
5 91 1 0 0 0 1 1 1 10 1 1 0 0 1 0 0 00 0 1 1 1 1 0 0 00 0 0 0 0 0 0 0 00 0 0 0 0 1 0 0 0
Sample output
1 2 11 12 21 22 23 24 15 6 7 8 9
Explanation
[1 1 0 0 0 1 0 0 0]

 

[0 1 1 0 0 1 0 0 0]

 

[0 0 1 1 1 1 0 0 0]

 

[0 0 0 0 0 0 0 0 0]

 

[0 0 0 0 0 1 0 0 0]

 

For the given array we encounter a 1 at the [0,0] position then at [0,1] -> [1,1] -> [1,2] and so on.

 

[0,0] is unrolled to position 1
[0,1] is unrolled to position 2
[1,1] is unrolled to position 11
and so on.

 

So the output we get is: [1, 2, 11, 12, 21, 22, 23, 24, 15, 6, 7, 8, 9]

 

Coding Language: Java/Python/Javascript

by Expert (34,270 points)