call(back)
Algorithms & Data StructureseasySign in to star problems

Team Photo I

You're photographing two teams arranged in two rows. Every back-row player must be strictly taller than the front-row player directly in front of them.

Two teams of equal size each fill one row, and you may order each row however you like. Return which team goes in front: ["A", "B"] when team A can stand in front of team B, ["B", "A"] when team B can stand in front of team A, and null when neither works.

teamA = [170, 160], teamB = [180, 165]   ->  ["A", "B"]   (160 before 165, 170 before 180)
teamA = [3, 4],     teamB = [1, 2]       ->  ["B", "A"]
teamA = [170],      teamB = [170]        ->  null         (equal height blocks)

Hints

Hint 1

Sort both teams. If the i-th shortest back player clears the i-th shortest front player for every i, the arrangement works — and if any arrangement works, this sorted pairing does.

Hint 2

Check team A in front of team B, then the reverse. Both can't succeed at once.

Asked at