INDIANAPOLIS — For more than 22 minutes Saturday, the Terrapins men’s basketball team had been scratching and clawing for a lead. So when a free throw from forward Robert Carter Jr. put the Terps ahead with less than seven minutes remaining, the fans behind the Terps’ bench erupted.

A 12-point first-half deficit had been overturned, and the Terps were minutes away from advancing to the Big Ten finals and knocking off the No. 2 team in the nation, Michigan State, at Bankers Life Fieldhouse.

But just as the win appeared within reach, the Terps offense faltered and left those same fans anxious and exasperated. Anemic for most of the night — the Terps shot 33.3 percent from the field — the offense endured a 5-minute and 45-second scoring drought and missed 10 of their final 11 shots from the field.

While the defense responded, the offensive slump doomed the No. 2-seed Terps in a 64-61 loss against No. 3-seed Michigan State as guard Melo Trimble missed a potential go-ahead layup with 4 seconds left. The Spartans advance to the finals and will play No. 4-seed Purdue on Sunday. Guard Denzel Valentine led the Spartans with 18 points, 10 assists and seven rebounds.

Carter tied the game high with 18 points and led the Terps with eight rebounds, but it wasn’t enough as Trimble’s shooting struggles reemerged. He went 2-for-15 from the field and had 11 points.

“Last three minutes, they were able to make one or two more plays than we were,” coach Mark Turgeon said. “But I was proud of my team.”

With 10 seconds left, Valentine missed the front end of a 1-and-1. The Terps, who were down 62-61, had a chance to take the lead. With no timeouts, Trimble pushed the ball up the floor and drove to the basket. He didn’t draw a foul and his shot didn’t touch the rim. After a scramble for the ball in which Carter nearly had a putback basket, Valentine secured the rebound.

Valentine, the Big Ten Player of the Year, hit two free throws, and Trimble’s desperation heave with less than a second remaining fell well short. After the game, Trimble said he was surprised he didn’t get a call on the last shot. There appeared to be contact.

Trimble’s backcourt partner, senior Rasheed Sulaimon, finished 3-for-10 and had nine points.

“You don’t want to blame it on the referees of course but some of the calls that Melo and Rasheed would usually get they didn’t get down the [stretch],” forward Damonte Dodd said.

The Terps’ familiar issues – rebounding and turnovers – weren’t huge factors Saturday. The Spartans outrebounded the Terps, 38-31, but the Terps had more offensive boards. Plus, the Terps had a plus-seven turnover margin.

Instead, the Terps were plagued again by Trimble’s shooting troubles. He had shot better than 42 percent from the field over the past four games. But Saturday he looked more like the Trimble who endured a 10-for-48 stretch (20.8 percent) over five games before breaking out of that slump.

With Trimble misfiring, the Spartans took a 41-33 lead into the break.

The Terps hung tough, and the deficit would never exceed eight in the second half as they held the Spartans to 42 percent from the field and forced 13 turnovers.

“That’s the best we’ve guarded in a long time,” Turgeon said. “We competed on the boards.”

Trimble’s first basket of the game, a 3-pointer, knotted the game at 53 with 10:26 remaining. Valentine responded with a long ball, though, giving the Spartans the lead once more.

Minutes later, Carter knocked down two free throws to give the Terps their first lead since the opening five minutes. But seconds later the Spartans regained the lead on a dunk after forward Deyonta Davis, who got away from Carter. The Terps never led again.

“One or two plays and it might be a different outcome,” Sulaimon said. “But it just wasn’t meant to be.”

While the Terps were in a somber mood after the last-second loss, Turgeon and the players took solace in their ability to hang tough with the Spartans.

After a rough end to the regular season with four losses in the final six games, the Terps believe they are back on track with the NCAA Tournament less than a week ago.

“When our backs are against the wall, we usually compete at a high clip,” Dodd said. “We showed the world tonight that’s what we’ll do.”