Open In App

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

Last Updated : 07 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the winner
// from given timeline
string findTheWinner(string str, int N)
{
    // If last point scored is 1 then
    // GEEK is winner
    if (str[N - 1] == '1') {
        return "GEEK's won";
    }
 
    // Else GEEK lost
    return "GEEK's lost";
}
 
// Driver Code
int main()
{
    // Input score timeline
    string str1 = "01011111111110110101";
    int N1 = str1.size();
    string str2 = "010101010101010101010101010100";
    int N2 = str2.size();
 
    // Print the winner
    cout << findTheWinner(str1, N1) << endl;
    cout << findTheWinner(str2, N2) << endl;
    return 0;
}


Java




// C# implementation of the above approach
class GFG {
 
  // Function to find the winner
  // from given timeline
  static String findTheWinner(String str, int N)
  {
 
    // If last point scored is 1 then
    // GEEK is winner
    if (str.charAt(N - 1) == '1')
    {
      return "GEEK's won";
    }
 
    // Else GEEK lost
    return "GEEK's lost";
  }
 
  // Driver Code
  public static void main(String args[])
  {
 
    // Input score timeline
    String str1 = "01011111111110110101";
    int N1 = str1.length();
    String str2 = "010101010101010101010101010100";
    int N2 = str2.length();
 
    // Print the winner
    System.out.println(findTheWinner(str1, N1));
    System.out.println(findTheWinner(str2, N2));
  }
}
 
// This code is contributed by Saurabh Jaiswal


Python3




# Python code for the above approach
 
# Function to find the winner
# from given timeline
def findTheWinner(str, N):
 
    # If last point scored is 1 then
    # GEEK is winner
    if (str[N - 1] == '1'):
        return "GEEK's won"
 
    # Else GEEK lost
    return "GEEK's lost"
 
# Driver Code
 
# Input score timeline
str1 = "01011111111110110101"
N1 = len(str1)
str2 = "010101010101010101010101010100"
N2 = len(str2)
 
# Print the winner
print(findTheWinner(str1, N1))
print(findTheWinner(str2, N2))
 
# This code is contributed by Saurabh Jaiswal


C#




// C# implementation of the above approach
using System;
 
class GFG{
 
// Function to find the winner
// from given timeline
static string findTheWinner(string str, int N)
{
     
    // If last point scored is 1 then
    // GEEK is winner
    if (str[N - 1] == '1')
    {
        return "GEEK's won";
    }
 
    // Else GEEK lost
    return "GEEK's lost";
}
 
// Driver Code
public static void Main()
{
     
    // Input score timeline
    string str1 = "01011111111110110101";
    int N1 = str1.Length;
    string str2 = "010101010101010101010101010100";
    int N2 = str2.Length;
 
    // Print the winner
    Console.WriteLine(findTheWinner(str1, N1));
    Console.WriteLine(findTheWinner(str2, N2));
}
}
 
// This code is contributed by ukasp


Javascript




<script>
      // JavaScript code for the above approach
 
      // Function to find the winner
      // from given timeline
      function findTheWinner(str, N)
      {
       
          // If last point scored is 1 then
          // GEEK is winner
          if (str[N - 1] == '1') {
              return "GEEK's won";
          }
 
          // Else GEEK lost
          return "GEEK's lost";
      }
 
      // Driver Code
 
      // Input score timeline
      let str1 = "01011111111110110101";
      let N1 = str1.length;
      let str2 = "010101010101010101010101010100";
      let N2 = str2.length;
 
      // Print the winner
      document.write(findTheWinner(str1, N1) + '<br>');
      document.write(findTheWinner(str2, N2) + '<br>');
 
// This code is contributed by Potta Lokesh
  </script>


Output

GEEK's won
GEEK's lost

Time Complexity: O(1)
Auxiliary Space: O(1) 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads