Maximum Length of Pair Chain in C++

Suppose in the world of Dota2, there are two parties − The Radiant and also the Dire. The Dota2 senate consists of senators coming from two parties. Now the senate wants to form a choice a few change within the Dota2 game. The voting for this alteration may be a round-based procedure. In each round, each senator can exercise one among the two rights −

  • Ban one senator's right − A senator can make another senator lose all his rights during this and every one the subsequent rounds.

  • Announce the victory − If this senator found the senators who still have rights to vote are all from an equivalent party, he can announce the victory and make the choice about the change within the game.

Given a string representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and also the Dire party respectively. Then if there are n senators, the dimensions of the given string are going to be n.

The round-based procedure starts from the primary senator to the last senator within the given order. This procedure will last until the top of voting. All the senators who have lost their rights are going to be skipped during the procedure.

Suppose every senator is sensible enough and can play the simplest strategy for his own party, you would like to predict which party will finally announce the victory and make the change within the Dota2 game. The output should be Radiant or Dire.

So if the input is like “RDD”, it will be Dire. This is because the first senator comes from Radiant and he can just ban the next senator's right in the round 1. And the second senator can't exercise any rights anymore since his right has been banned. After that the third senator comes from Dire and he can ban the first senator's right in the round 1. Now in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.

To solve this, we will follow these steps −

  • Make a queue q1, q2, n := size of string. For all R insert into q1, and for all D, insert into q2.

  • while both queues are not empty

    • if front element of q1 < front element of q2, then

      • insert n into q1, delete from q2 and q1

    • otherwise insert n into q2, delete from q2 and q1

    • increase n by 1

  • if queue is empty, then return Dire, otherwise, return Radiant


Let us see the following implementation to get a better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
   string predictPartyVictory(string s) {
      queue <int> q1, q2;
      int n = s.size();
      for(int i = 0; i < s.size(); i++){
         if(s[i] == 'R'){
         } else {
      while(q1.size() && q2.size()){
         if(q1.front() < q2.front()){
         } else {
      return q1.empty()? "Dire" : "Radiant";
   Solution ob;
   cout <<(ob.predictPartyVictory("RDD"));