Who Won the Election???

Elections are going on, and there are two candidates A and B, contesting with each other. There is a queue of voters and in this queue, some of them are supporters of A and some of them are supporters of B. Many of them are neutral. The fate of the election will be decided on which side the neutral voters vote. Supporters of A and supporters of B make attempts to win the votes of neutral voters.

The way this can be done is explained below:

1. The voter queue is denoted by three characters, viz {-, A, B}. The ‘-’ denotes neutral candidate, ‘A’ denotes supporter of candidate A and ‘B’ denotes supporter of candidate B.
2. Supporters of A can only move towards the left side of the queue.
3. Supporters of B can only move towards the right side of the queue.
4. Since time is critical, supporters of both A and B will move simultaneously.
5. They both will try and influence the neutral voters by moving in their direction in the queue. If a supporter of A reaches the neutral voter before a supporter of B reaches him, then that neutral voter will become a supporter of candidate A.
6. Similarly, if a supporter of B reaches the neutral voter before supporter of A reaches him, then that neutral voter will become a supporter of candidate B.
7. Finally, if both reach at the same time, the voter will remain neutral. A neutral vote cannot decide the outcome of the election.
8. If finally, the queue has more votes for candidate A, then A wins the election. If B has more votes, then B wins that election. If both have equal votes, then it will be a coalition government.

Your task is to find the outcome of the election.

For Example:
Given string- “B--A-”
              B --->  B  A   <--- A    B
              ----------------------------->
Output - B as B can move towards right only and A can move in left direction only. Thus B has 3 supporters in total while A have only 2 supporters. 

Note:

1. There are no test cases where all votes are neutral.
2. The influenced voters do not move and hence does not have any influence over the neutral voters.
Input Format:
The first line of the input contains a single integer T, representing the number of test cases. 

The first line of each test case will contain the string having characters ‘A’, ‘B’ or ‘-’
Output Format:
For each test case, you need to print ‘A’ if A wins the election, ‘B’ if B wins the election or ‘Coalition’ if both have equal votes.

Output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <= 10^4,
Where T is the number of testcases,
and N is the length of the string.

Time Limit: 1sec
CodingNinjas
author
2y

or the Input 14
--AB--AB---A--

Explanation:
For starting positions where there is no opposition from supporter of B, supporter of A can promote in left side of the queue. The voting queue will then look ...read more

Comment deleted
CodingNinjas
author
2y
Brute Force Approach.

In this approach, we use the brute force approach to solve the problem. We traverse over each character of the given string and if the ith character is ‘-’ we use another loop to ...read more

CodingNinjas
author
2y
Maintain Count at Each Index using extra space.

In this approach, we use extra space to store the distance of supporters of A and B for each index.

Steps:

  • Maintain the count of votes of A and B in vari...read more
Boggula Venkata Ajith Reddy
2d
Hamburger Menu AmbitionBox Logo Search designation Asked inTCS,System Engineer Who Won the Election??? Elections are going on, and there are two candidates A and B, contesting with each other. There i...read more
brucewayne
5d
works at
I want job
beatingcopernicus
14d
works at
Nithishkumar maari
2mo
To determine who won the election based on the voters' preferences, you can simulate how neutral voters cast their votes by observing the influence of the supporters of candidates A and B
fearlessbrodo
3mo
works at
tHIS ANswer is correct
Nikhil Jinson
4mo
A AND B
Comment deleted
fearlessbrodo
1y
works at
mnlml
sindhu hm
1y

Good working and Ard work

Add answer anonymously...
TCS System Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter