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

14 Mar, 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[0] – count[1]) 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[0] and count[1].Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// Cpp program for predicting winner#include <bits/stdc++.h>using namespace std; // function for winner predictionvoid predictWinner(string score, int n){     int count[2] = { 0 }, i;    for (i = 0; i < score.size(); i++) {         // increase count        count[score[i] - '0']++;         // check losing condition        if (count[0] == n && count[1] < n - 1) {            cout << "GEEKS lost";            return;        }         // check winning condition        if (count[1] == n && count[0] < n - 1) {            cout << "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.size(); i++) {         // increase count        count[score[i] - '0']++;         // check for 2 point lead        if (abs(count[0] - count[1]) == 2) {             // condition of lost            if (count[0] > count[1])                cout << "GEEKS lost";             // condition of win            else                cout << "GEEKS won";             return;        }    }} // driver programint main(){    string score = "1001010101111011101111";    int n = 15;    predictWinner(score, n);    return 0;}
// Java program for// predicting winnerimport java.io.*;import java.util.*;import java.lang.*; class GFG{// function for// winner predictionstatic 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 Codepublic static void main(String[] args){    String score = "1001010101111011101111";    int n = 15;    predictWinner(score, n);}}
# Python 3 program for predicting winner # function for winner predictiondef 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 Codeif __name__ == '__main__':    score = "1001010101111011101111"    n = 15    predictWinner(score, n) # This code is contributed by# Surendra_Gangwar
// C# program for predicting winnerusing System; class GFG{// function for winner predictionpublic static void predictWinner(string score,                                 int n){     int[] count = new int[2]; int i;    for (i = 0; i < score.Length; i++)    {         // increase count        count[score[i] - '0']++;         // check losing        // condition        if (count[0] == n && count[1] < n - 1)        {            Console.Write("GEEKS lost");            return;        }         // check winning condition        if (count[1] == n && count[0] < n - 1)        {            Console.Write("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[i] - '0']++;         // check for 2 point lead        if (Math.Abs(count[0] - count[1]) == 2)        {             // condition of lost            if (count[0] > count[1])            {                Console.Write("GEEKS lost");            }             // condition of win            else            {                Console.Write("GEEKS won");            }             return;        }    }} // Driver Codepublic static void Main(string[] args){    string score = "1001010101111011101111";    int n = 15;    predictWinner(score, n);}} // This code is contributed by Shrikant13
<script> // JavaScript program for predicting winner // function for winner predictionfunction predictWinner(score, n){    count = new Array(2).fill(0);     for(let i = 0;i < score.length; i++)    {                 // increase count        index = score.charCodeAt(i) - '0'.charCodeAt(0)        count[index]++         // check losing condition        if (count[0] == n && count[1] < n - 1)        {            document.write("GEEKS lost"," ")            return        }         // check winning condition        if (count[1] == n && count[0] < n - 1){            document.write("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        }    }    i += 1                while(i < score.length)    {                 // increase count        index = score.charCodeAt(i) - '0'.charCodeAt(0)        count[index] += 1         // check for 2 point lead        if (Math.abs(count[0] - count[1]) == 2){                         // condition of lost            if (count[0] > count[1])                document.write("GEEKS lost"," ")             // condition of win            else                document.write("GEEKS won"," ");             return        }        i++    }} // Driver Code let score = "1001010101111011101111"let n = 15predictWinner(score, n) // This code is contributed by ShinjanPatra </script>
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.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
shrikanth13
SURENDRA_GANGWAR
shinjanpatra
binary-string
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.