Amazon SDE1(2021)

Given a binary array :
1 0 1 0 0 1 0 0 0 0 1 0 1
where 0 represents an empty seat and 1 represents that the seat is filled. We need to sit K people one by one.
The person will sit where the space between two persons(i.e two 1's) is minimum.
For e:g 1st person sits b/w arr[5] and arr[10] as gap is highest. As there are two centres, a[7] & a[8] he can sit on either of these postions. This procedure continues k times.
Output the seating arrangement each time a person sits.

I was not able to come with an optimized approach for this one. Please help me solve this.
Most optimal approach was needed.

Comments (6)