Find the winner of a game where scores are given as a binary string | Set 2
Last Updated :
07 Jan, 2022
Given a binary string str 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. The task is to find whether GEEK’s team had won or lost the match.
Examples:
Input: str = “01011111111110110101”
Output: GEEK’S won
Explanation: GEEK wins with score of 15-5
Input: str = “010101010101010101010101010100”
Output: GEEK’s lost
Explanation: The opponent wins with score of 16-14
Naive Approach: The naive approach is mentioned in Set-1 of this problem.
Efficient Approach: No matter what is the timeline, the player scoring the last point will be winner. The following are the reasons for that:
The game ends with someone winning the set and meeting the required conditions to win the game.
- If the game ends by someone reaching the 15 points first then he will be one to win the last set.
- If the game is won by someone maintaining two points lead after reaching 15-14 state, then also the winner will be the one to win the last set maintaining a two point lead.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string findTheWinner(string str, int N)
{
if (str[N - 1] == '1' ) {
return "GEEK's won" ;
}
return "GEEK's lost" ;
}
int main()
{
string str1 = "01011111111110110101" ;
int N1 = str1.size();
string str2 = "010101010101010101010101010100" ;
int N2 = str2.size();
cout << findTheWinner(str1, N1) << endl;
cout << findTheWinner(str2, N2) << endl;
return 0;
}
|
Java
class GFG {
static String findTheWinner(String str, int N)
{
if (str.charAt(N - 1 ) == '1' )
{
return "GEEK's won" ;
}
return "GEEK's lost" ;
}
public static void main(String args[])
{
String str1 = "01011111111110110101" ;
int N1 = str1.length();
String str2 = "010101010101010101010101010100" ;
int N2 = str2.length();
System.out.println(findTheWinner(str1, N1));
System.out.println(findTheWinner(str2, N2));
}
}
|
Python3
def findTheWinner( str , N):
if ( str [N - 1 ] = = '1' ):
return "GEEK's won"
return "GEEK's lost"
str1 = "01011111111110110101"
N1 = len (str1)
str2 = "010101010101010101010101010100"
N2 = len (str2)
print (findTheWinner(str1, N1))
print (findTheWinner(str2, N2))
|
C#
using System;
class GFG{
static string findTheWinner( string str, int N)
{
if (str[N - 1] == '1' )
{
return "GEEK's won" ;
}
return "GEEK's lost" ;
}
public static void Main()
{
string str1 = "01011111111110110101" ;
int N1 = str1.Length;
string str2 = "010101010101010101010101010100" ;
int N2 = str2.Length;
Console.WriteLine(findTheWinner(str1, N1));
Console.WriteLine(findTheWinner(str2, N2));
}
}
|
Javascript
<script>
function findTheWinner(str, N)
{
if (str[N - 1] == '1' ) {
return "GEEK's won" ;
}
return "GEEK's lost" ;
}
let str1 = "01011111111110110101" ;
let N1 = str1.length;
let str2 = "010101010101010101010101010100" ;
let N2 = str2.length;
document.write(findTheWinner(str1, N1) + '<br>' );
document.write(findTheWinner(str2, N2) + '<br>' );
</script>
|
Output
GEEK's won
GEEK's lost
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...