Ad
  • Custom User Avatar

    sorted() is not required. range or rangeClosed are iterators generating consecutive numbers. Always increasing by nature.

  • Custom User Avatar

    You don't need a boolean[] as the input. The only concern is the count of urinals.

    To ensure that no two persons are standing next to each other, people can simply be placed after leaving one vacant space.
    The algorithm you implemented is to ensure that people stand at the farthest distance possible. A simple logic to get to the next to next is enough.

    I'm saying the optimality of both the approaches is same. In both the approaches, a simple formula is used to move to the next space.

    In any case, if the number of people is greater than ceil (urinalsCount / 2), it will be a cursed situation.

  • Custom User Avatar

    StringBuilder is the easiest way to reverse a string.