The new prime minister of Malaysia, who spent years in jail and was the opposition leader, was elected on Thursday.

The fate of the 75 year-old was hanging in the balance for a week after the elections.

The parties vying for power were led by the former premier and his son. Neither coalition was able to get the necessary number of seats to form a government.

After coalition talks failed, Malaysia's King Al-Sultan Abdullah Sultan Ahmad Shah spoke to the state monarchs to find a solution.

There will be a unity government in Malaysia with the leader being the sultan. The political alliances were stuck in a political stalemate.

"After going through the views of the other Malay rulers, His Royal Highness has granted his consent to appoint DS Anwar Ibrahim as Malaysia's 10th prime minister," the palace said.

The statement said that the winners do not win all and the loser does not lose everything.

Anwar has spent two decades vying for the top job

He was Malaysia's deputy prime minister in the 1990's. He was arrested and charged with sodomy and corruption after he was fired from his job.

He was sentenced to 6 years in prison for corruption in 1999. He was sentenced to nine years in prison for a sodomy conviction in 2000. In 2004, he was acquitted of the sodomy charge.

It was not the end of it. There was an accusation of sodomizing a man in 2008. The man was sentenced to five years in prison for sodomy.

The charges have been denied by the man and he calls them politically motivated.

After the election of the pakatan harpan, a royal pardon was granted to Anwar.

The new prime minister of Malaysia is going to be sworn in on Thursday.