AMES, Iowa -- Melvin Ejim scored 20 points and No. 16 Iowa State beat No. 22 Kansas State 81-75 on Saturday to snap a three-game losing streak.

Georges Niang had 18 points and freshman Matt Thomas matched a career high with 14 for the Cyclones (15-3, 3-3 Big 12), who blew a 12-point halftime lead before putting the game away late in the second half.

Ejim blocked a potential game-tying 3 from Shane Southwell and hit two free throws to put the Cyclones ahead 79-73 with 23 seconds left.

Marcus Foster scored 20 points for Kansas State (14-6, 4-3), which lost consecutive games for the first time since November.

Kansas State spent much of the afternoon allowing Iowa State to make a run and then matching it.

In the end, the Wildcats were just a couple of plays short.

Kansas State needed less than 7 minutes to erase a 46-34 first-half deficit. The Wildcats then let up nine straight points in what appeared to be a game-deciding run for the Cyclones.

But Iowa State's inconsistent defense hurt it again. The Cyclones allowed nine points in 2 minutes, and Will Spradlin's 3 tied it at 66-all with 5 minutes left.

Niang answered with a crucial 3, and Cyclones star DeAndre Kane shook off a sluggish game with a three-point play to put Iowa State ahead 72-66.

Kane finished with 10 points after shooting 3 of 12 for the second straight game.

This was the first time in the 216 meetings between Iowa State and Kansas State that both were ranked in the AP Top 25.

But the Cyclones and Wildcats entered play looking to regain some lost momentum.

Iowa State had opened the season at 14-0, the best start in school history. But losses to Oklahoma, Kansas and Texas had considerably cooled the program's national buzz and sent it tumbling eight spots in this week's poll.

Then last week, a district court judge ruled that reserve guard Bubu Palo be reinstated in a move that angered the administration and dominated local headlines.

Kansas State was coming off a buzzer-beating loss at Texas, as Jonathan Holmes hit a 3 as time expired for a 67-64 Longhorns' win on Tuesday.

The Wildcats started out hot from the perimeter, hitting four of their first five 3-point attempts. Iowa State was even better, as the struggling Thomas connected on his first four 3s to help give Iowa State its 12-point halftime edge.

But Kansas State opened the second half on a 16-4 run, and Southwell's 3 tied the game at 50 with 13:11 left.

Southwell had 14 points for Kansas State and Spradlin added 12.

Copyright 2014 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.

More From KSOO-AM / ESPN Sioux Falls