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 wonInput : 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:
// Cpp program for predicting winner #include <bits/stdc++.h> using namespace std;
// function for winner prediction void 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 program int main()
{ string score = "1001010101111011101111" ;
int n = 15;
predictWinner(score, n);
return 0;
} |
// Java program for // predicting winner import java.io.*;
import java.lang.*;
import java.util.*;
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);
}
} |
# 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# program for predicting winner using System;
class GFG {
// function for winner prediction
public 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 Code
public 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 prediction function 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 = 15 predictWinner(score, n) // This code is contributed by ShinjanPatra </script> |
GEEKS won
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++ program for finding winner. #include <iostream> using namespace std;
// function for finding winner. void findWinner(string score)
{ if (score.back() == '1' ) {
cout << "GEEKS won" << endl;
}
else {
cout << "GEEKS lost" << endl;
}
} // driver code int main()
{ string score = "1001010101111011101111" ;
findWinner(score);
return 0;
} // This code is contributed by codebraxnzt |
// Java program for finding winner. public class GFG {
// function for finding winner
static void findWinner(String score) {
if (score.charAt(score.length() - 1 ) == '1' ) {
System.out.println( "GEEKS won" );
} else {
System.out.println( "GEEKS lost" );
}
}
// driver code
public static void main(String[] args) {
String score = "1001010101111011101111" ;
findWinner(score);
}
} |
# Python 3 program for finding winner. # function for finding winner. def findWinner(score):
if score[ - 1 ] = = '1' :
print ( 'GEEKS won' )
else :
print ( 'GEEKS lost' )
# driver code if __name__ = = '__main__' :
score = "1001010101111011101111"
findWinner(score)
# This code is contributed by # Abhishek_Singh |
// function for finding winner. function findWinner(score) {
if (score.slice(-1) === '1' ) {
console.log( "GEEKS won" );
} else {
console.log( "GEEKS lost" );
}
} // driver code let score = "1001010101111011101111" ;
findWinner(score); |
// C# program for finding winner. using System;
class Program {
// function for finding winner.
static void FindWinner( string score)
{
if (score[score.Length - 1] == '1' ) {
Console.WriteLine( "GEEKS won" );
}
else {
Console.WriteLine( "GEEKS lost" );
}
}
// driver code
static void Main()
{
string score = "1001010101111011101111" ;
FindWinner(score);
}
} |
GEEKS won
Time Complexity: O(1)
Auxiliary Space: O(1)