Defending champions Brazil eased through to the quarter-finals of the men's Olympic football, but 2016 silver medallists Germany are eliminated.
Everton striker Richarlison scored two late goals as Brazil beat Saudi Arabia 3-1 to finish top of Group D, with Ivory Coast second after holding Germany to a 1-1 draw.
In Group B, South Korea beat Honduras 6-0 to qualify as group winners.
New Zealand drew 0-0 with Romania to finish second on goal difference.
Brazil looked set to be held to a frustrating draw when a header from Saudi Arabia defender Abdulelah Al-Amri cancelled out Matheus Cunha's early opener at the Saitama Stadium.
However, Richarlison, who hit a hat-trick in Brazil's opening group game against Germany, edged them ahead again with 14 minutes remaining before sealing the win in stoppage-time.
Germany needed to beat Ivory Coast to make it through but fell behind midway through the second half when Benjamin Henrichs put the ball into his own net while attempting a tackle in the six-yard box.
Eduard Loewen levelled with a free-kick six minutes later to set up a frantic finish, but despite piling on the pressure Germany could not find a winner.
Hosts Japan became the only side to win all three of their group games after thumping France 4-0 in Yokohama.
Goals from Hiroki Sakai, Takefusa Kubo, Koki Miyoshi and Daizen Maeda sealed the victory and set up a last-eight clash against New Zealand.
Mexico secured second place and will play South Korea next after a 3-0 win against South Africa, who lost all three of their group matches.
Egypt will play Brazil in the quarter-finals after a 2-0 win over Australia secured second place in Group C ahead of Argentina, who could only draw 1-1 with group winners Spain.
Argentina midfielder Tomas Belmonte scored a late equaliser to cancel out Mikel Merino's opener, but it was not enough to prevent Argentina from slipping to third and bowing out.
Spain, who end with five points from their three group games, face Ivory Coast in the quarter-final.
All four teams in Group B went into the final round of games level on three points each, but it was South Korea who progressed as group winners as a hat-trick from Hwang Ui-jo helped secure a 6-0 win over Honduras, with New Zealand joining them in the quarter-finals.