Someone Please explain me the editorial as I am unable to understand the logic.

Thanks in Advance

Someone Please explain me the editorial as I am unable to understand the logic.

Thanks in Advance

Chef will start asking from students from 1st row to nth row and then from (n-1)th row to 2nd row. This constitutes 1 time period lets say. Thus, after every time period, chef will ask 1st row 1st student.

In one time period except 1st and last row students, other students are asked 2 questions while former have been asked 1 question.Thus no. of questions asked in 1 period is T=n*m+(n-2)*m. Left out question is k%T.

Now remaining questions are asked starting from 1st row 1st student.

Now let f(x,y) denotes no. of questions asked to xth row yth column student

no of periods,nt=k/T

remaining = k%T

f(1,y)=f(n,y)=nt

f(x,y)=2*nt x>1&&x<n

for (i = 0; k > 0 && i < n; i++)

for (j = 0; j < m && k > 0; j++)

{

f(i,j)++;

kâ€“;

}

Now simply iterate over all possible n,m and find max,min and questions asked to Sergei

Complexity O(n*m)

Hope it helps. In case of any doubt, you can comment.

1 Like