Find the winner of a game where scores are given as a binary string
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
Approach 1:
- 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 then find the winner on the value of count[0] and count[1].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void predictWinner(string score, int n)
{
int count[2] = { 0 }, i;
for (i = 0; i < score.size(); i++) {
count[score[i] - '0' ]++;
if (count[0] == n && count[1] < n - 1) {
cout << "GEEKS lost" ;
return ;
}
if (count[1] == n && count[0] < n - 1) {
cout << "GEEKS won" ;
return ;
}
if (count[0] == n - 1 && count[1] == n - 1) {
count[0] = 0;
count[1] = 0;
break ;
}
}
for (i++; i < score.size(); i++) {
count[score[i] - '0' ]++;
if ( abs (count[0] - count[1]) == 2) {
if (count[0] > count[1])
cout << "GEEKS lost" ;
else
cout << "GEEKS won" ;
return ;
}
}
}
int main()
{
string score = "1001010101111011101111" ;
int n = 15;
predictWinner(score, n);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void predictWinner(String score, int n)
{
int count[] = new int [ 2 ], i;
for (i = 0 ; i < score.length(); i++) {
count[score.charAt(i) - '0' ]++;
if (count[ 0 ] == n && count[ 1 ] < n - 1 ) {
System.out.print( "GEEKS lost" );
return ;
}
if (count[ 1 ] == n && count[ 0 ] < n - 1 ) {
System.out.print( "GEEKS won" );
return ;
}
if (count[ 0 ] == n - 1 && count[ 1 ] == n - 1 ) {
count[ 0 ] = 0 ;
count[ 1 ] = 0 ;
break ;
}
}
for (i++; i < score.length(); i++) {
count[score.charAt(i) - '0' ]++;
if (Math.abs(count[ 0 ] - count[ 1 ]) == 2 ) {
if (count[ 0 ] > count[ 1 ])
System.out.print( "GEEKS lost" );
else
System.out.print( "GEEKS won" );
return ;
}
}
}
public static void main(String[] args)
{
String score = "1001010101111011101111" ;
int n = 15 ;
predictWinner(score, n);
}
}
|
Python3
def predictWinner(score, n):
count = [ 0 for i in range ( 2 )]
for i in range ( 0 , len (score), 1 ):
index = ord (score[i]) - ord ( '0' )
count[index] + = 1
if (count[ 0 ] = = n and count[ 1 ] < n - 1 ):
print ( "GEEKS lost" , end = " " )
return
if (count[ 1 ] = = n and count[ 0 ] < n - 1 ):
print ( "GEEKS won" , end = " " )
return
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 ):
index = ord (score[i]) - ord ( '0' )
count[index] + = 1
if ( abs (count[ 0 ] - count[ 1 ]) = = 2 ):
if (count[ 0 ] > count[ 1 ]):
print ( "GEEKS lost" , end = " " )
else :
print ( "GEEKS won" , end = " " )
return
if __name__ = = '__main__' :
score = "1001010101111011101111"
n = 15
predictWinner(score, n)
|
C#
using System;
class GFG {
public static void predictWinner( string score, int n)
{
int [] count = new int [2];
int i;
for (i = 0; i < score.Length; i++) {
count[score[i] - '0' ]++;
if (count[0] == n && count[1] < n - 1) {
Console.Write( "GEEKS lost" );
return ;
}
if (count[1] == n && count[0] < n - 1) {
Console.Write( "GEEKS won" );
return ;
}
if (count[0] == n - 1 && count[1] == n - 1) {
count[0] = 0;
count[1] = 0;
break ;
}
}
for (i++; i < score.Length; i++) {
count[score[i] - '0' ]++;
if (Math.Abs(count[0] - count[1]) == 2) {
if (count[0] > count[1]) {
Console.Write( "GEEKS lost" );
}
else {
Console.Write( "GEEKS won" );
}
return ;
}
}
}
public static void Main( string [] args)
{
string score = "1001010101111011101111" ;
int n = 15;
predictWinner(score, n);
}
}
|
Javascript
<script>
function predictWinner(score, n)
{
count = new Array(2).fill(0);
for (let i = 0;i < score.length; i++)
{
index = score.charCodeAt(i) - '0' .charCodeAt(0)
count[index]++
if (count[0] == n && count[1] < n - 1)
{
document.write( "GEEKS lost" , " " )
return
}
if (count[1] == n && count[0] < n - 1){
document.write( "GEEKS won" , " " )
return
}
if (count[0] == n - 1 && count[1] == n - 1){
count[0] = 0
count[1] = 0
break
}
}
i += 1
while (i < score.length)
{
index = score.charCodeAt(i) - '0' .charCodeAt(0)
count[index] += 1
if (Math.abs(count[0] - count[1]) == 2){
if (count[0] > count[1])
document.write( "GEEKS lost" , " " )
else
document.write( "GEEKS won" , " " );
return
}
i++
}
}
let score = "1001010101111011101111"
let n = 15
predictWinner(score, n)
</script>
|
Time Complexity: O(n)
Space Complexity: O(1)
Efficient Approach: If we observe carefully, we can see that the match will end with a winning point. Hence, the team scoring the last point must be the winner. So, we can conclude which team that scored the last point is the winner.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
void findWinner(string score)
{
if (score.back() == '1' ) {
cout << "GEEKS won" << endl;
}
else {
cout << "GEEKS lost" << endl;
}
}
int main()
{
string score = "1001010101111011101111" ;
findWinner(score);
return 0;
}
|
Java
public class GFG {
static void findWinner(String score) {
if (score.charAt(score.length() - 1 ) == '1' ) {
System.out.println( "GEEKS won" );
} else {
System.out.println( "GEEKS lost" );
}
}
public static void main(String[] args) {
String score = "1001010101111011101111" ;
findWinner(score);
}
}
|
Python3
def findWinner(score):
if score[ - 1 ] = = '1' :
print ( 'GEEKS won' )
else :
print ( 'GEEKS lost' )
if __name__ = = '__main__' :
score = "1001010101111011101111"
findWinner(score)
|
Javascript
function findWinner(score) {
if (score.slice(-1) === '1' ) {
console.log( "GEEKS won" );
} else {
console.log( "GEEKS lost" );
}
}
let score = "1001010101111011101111" ;
findWinner(score);
|
C#
using System;
class Program {
static void FindWinner( string score)
{
if (score[score.Length - 1] == '1' ) {
Console.WriteLine( "GEEKS won" );
}
else {
Console.WriteLine( "GEEKS lost" );
}
}
static void Main()
{
string score = "1001010101111011101111" ;
FindWinner(score);
}
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
21 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...