# Find the winner of a game where scores are given as a binary string

• Last Updated : 29 Aug, 2022

Given a binary string representing the scores of a Volleyball match. The task is to find the winner of the match according to below conditions:

• In volleyball, the two teams play with each other and the team which scores 15 points first will be the winner except the case when both teams have reached to 14 points.
• In the case when both teams have reached 14 points then the team maintaining a lead of two points will be the winner.

In the given binary string, 0 means GEEK’s team lose a point and 1 means GEEK’s team win a point. You have to find whether GEEK’s team had won or lost the match.

Examples:

```Input : score[] = 01011111111110110101
Output : GEEK's won

Input : score[] = 010101010101010101010101010100
Output : GEEK's lost```
• Case I : When one of the team scores 15 points first and the second team has scored less than 15 points. Then traverse the given binary string and store the count of zero’s and one’s. After that if you get the count of one to be 15 and the count of zero less than 15 then GEEK’s won and on the other hand if you get count of zero to be 15 and count of one less than 15 then GEEK’s lost.
• Case II : When both of the team scores 14 points then reset count of both to zero and for each zero or one increment their count and simultaneously check if abs(count – count) is equal to 2, if this happens then it means any one of the team had scored two more points than its opponent and will be the winner. You can than find the winner on the value of count and count.

Below is the implementation of the above approach:

## C++

 `// Cpp program for predicting winner``#include ``using` `namespace` `std;` `// function for winner prediction``void` `predictWinner(string score, ``int` `n)``{` `    ``int` `count = { 0 }, i;``    ``for` `(i = 0; i < score.size(); i++) {` `        ``// increase count``        ``count[score[i] - ``'0'``]++;` `        ``// check losing condition``        ``if` `(count == n && count < n - 1) {``            ``cout << ``"GEEKS lost"``;``            ``return``;``        ``}` `        ``// check winning condition``        ``if` `(count == n && count < n - 1) {``            ``cout << ``"GEEKS won"``;``            ``return``;``        ``}` `        ``// check tie on n-1 point``        ``if` `(count == n - 1 && count ==``                                   ``n - 1) {``            ``count = 0;``            ``count = 0;``            ``break``;``        ``}``    ``}` `    ``for` `(i++; i < score.size(); i++) {` `        ``// increase count``        ``count[score[i] - ``'0'``]++;` `        ``// check for 2 point lead``        ``if` `(``abs``(count - count) == 2) {` `            ``// condition of lost``            ``if` `(count > count)``                ``cout << ``"GEEKS lost"``;` `            ``// condition of win``            ``else``                ``cout << ``"GEEKS won"``;` `            ``return``;``        ``}``    ``}``}` `// driver program``int` `main()``{``    ``string score = ``"1001010101111011101111"``;``    ``int` `n = 15;``    ``predictWinner(score, n);``    ``return` `0;``}`

## Java

 `// Java program for``// predicting winner``import` `java.io.*;``import` `java.util.*;``import` `java.lang.*;` `class` `GFG``{``// function for``// winner prediction``static` `void` `predictWinner(String score,``                          ``int` `n)``{` `    ``int` `count[] = ``new` `int``[``2``], i;``    ``for` `(i = ``0``;``         ``i < score.length(); i++)``    ``{` `        ``// increase count``        ``count[score.charAt(i) - ``'0'``]++;` `        ``// check losing``        ``// condition``        ``if` `(count[``0``] == n &&``            ``count[``1``] < n - ``1``)``        ``{``            ``System.out.print(``"GEEKS lost"``);``            ``return``;``        ``}` `        ``// check winning condition``        ``if` `(count[``1``] == n &&``            ``count[``0``] < n - ``1``)``        ``{``            ``System.out.print(``"GEEKS won"``);``            ``return``;``        ``}` `        ``// check tie on n-1 point``        ``if` `(count[``0``] == n - ``1` `&&``            ``count[``1``] == n - ``1``)``        ``{``            ``count[``0``] = ``0``;``            ``count[``1``] = ``0``;``            ``break``;``        ``}``    ``}` `    ``for` `(i++; i < score.length(); i++)``    ``{` `        ``// increase count``        ``count[score.charAt(i) - ``'0'``]++;` `        ``// check for 2 point lead``        ``if` `(Math.abs(count[``0``] -``                     ``count[``1``]) == ``2``)``        ``{` `            ``// condition of lost``            ``if` `(count[``0``] > count[``1``])``                ``System.out.print(``"GEEKS lost"``);` `            ``// condition of win``            ``else``                ``System.out.print(``"GEEKS won"``);` `            ``return``;``        ``}``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``String score = ``"1001010101111011101111"``;``    ``int` `n = ``15``;``    ``predictWinner(score, n);``}``}`

## Python3

 `# Python 3 program for predicting winner` `# function for winner prediction``def` `predictWinner(score, n):``    ``count ``=` `[``0` `for` `i ``in` `range``(``2``)]` `    ``for` `i ``in` `range``(``0``, ``len``(score), ``1``):``        ` `        ``# increase count``        ``index ``=` `ord``(score[i]) ``-` `ord``(``'0'``)``        ``count[index] ``+``=` `1` `        ``# check losing condition``        ``if` `(count[``0``] ``=``=` `n ``and` `count[``1``] < n ``-` `1``):``            ``print``(``"GEEKS lost"``, end ``=` `" "``)``            ``return` `        ``# check winning condition``        ``if` `(count[``1``] ``=``=` `n ``and` `count[``0``] < n ``-` `1``):``            ``print``(``"GEEKS won"``, end ``=` `" "``)``            ``return` `        ``# check tie on n-1 point``        ``if` `(count[``0``] ``=``=` `n ``-` `1` `and``            ``count[``1``] ``=``=` `n ``-` `1``):``            ``count[``0``] ``=` `0``            ``count[``1``] ``=` `0``            ``break``    ``i ``+``=` `1`   `        ` `    ``for` `i ``in` `range``(i, ``len``(score), ``1``):``        ` `        ``# increase count``        ``index ``=` `ord``(score[i]) ``-` `ord``(``'0'``)``        ``count[index] ``+``=` `1` `        ``# check for 2 point lead``        ``if` `(``abs``(count[``0``] ``-` `count[``1``]) ``=``=` `2``):``            ` `            ``# condition of lost``            ``if` `(count[``0``] > count[``1``]):``                ``print``(``"GEEKS lost"``, end ``=` `" "``)` `            ``# condition of win``            ``else``:``                ``print``(``"GEEKS won"``, end ``=` `" "``);` `            ``return` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``score ``=` `"1001010101111011101111"``    ``n ``=` `15``    ``predictWinner(score, n)` `# This code is contributed by``# Surendra_Gangwar`

## C#

 `// C# program for predicting winner``using` `System;` `class` `GFG``{``// function for winner prediction``public` `static` `void` `predictWinner(``string` `score,``                                 ``int` `n)``{` `    ``int``[] count = ``new` `int``; ``int` `i;``    ``for` `(i = 0; i < score.Length; i++)``    ``{` `        ``// increase count``        ``count[score[i] - ``'0'``]++;` `        ``// check losing``        ``// condition``        ``if` `(count == n && count < n - 1)``        ``{``            ``Console.Write(``"GEEKS lost"``);``            ``return``;``        ``}` `        ``// check winning condition``        ``if` `(count == n && count < n - 1)``        ``{``            ``Console.Write(``"GEEKS won"``);``            ``return``;``        ``}` `        ``// check tie on n-1 point``        ``if` `(count == n - 1 &&``            ``count == n - 1)``        ``{``            ``count = 0;``            ``count = 0;``            ``break``;``        ``}``    ``}` `    ``for` `(i++; i < score.Length; i++)``    ``{` `        ``// increase count``        ``count[score[i] - ``'0'``]++;` `        ``// check for 2 point lead``        ``if` `(Math.Abs(count - count) == 2)``        ``{` `            ``// condition of lost``            ``if` `(count > count)``            ``{``                ``Console.Write(``"GEEKS lost"``);``            ``}` `            ``// condition of win``            ``else``            ``{``                ``Console.Write(``"GEEKS won"``);``            ``}` `            ``return``;``        ``}``    ``}``}` `// Driver Code``public` `static` `void` `Main(``string``[] args)``{``    ``string` `score = ``"1001010101111011101111"``;``    ``int` `n = 15;``    ``predictWinner(score, n);``}``}` `// This code is contributed by Shrikant13`

## Javascript

 ``

Output

`GEEKS won`

This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up