C++ Program to check player position is dangerous or not in football match

C++Server Side ProgrammingProgramming

Suppose we have a binary string S of size n. Amal loves football very much. One day, he was watching a match, he was writing the players' current positions on a piece of paper. The given string is the written position. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is marked as "dangerous". We have to check whether it is dangerous or not.

Problem Category

To solve this problem, we need to manipulate strings. Strings in a programming language are a stream of characters that are stored in a particular array-like data type. Several languages specify strings as a specific data type (eg. Java, C++, Python); and several other languages specify strings as a character array (eg. C). Strings are instrumental in programming as they often are the preferred data type in various applications and are used as the datatype for input and output. There are various string operations, such as string searching, substring generation, string stripping operations, string translation operations, string replacement operations, string reverse operations, and much more. Check out the links below to understand how strings can be used in C/C++.

https://www.tutorialspoint.com/cplusplus/cpp_strings.htm

https://www.tutorialspoint.com/cprogramming/c_strings.htm

So, if the input of our problem is like S = "1000000001001", then the output will be True, as there are 8 > 7 consecutive 0s.

Steps

To solve this, we will follow these steps −

if "0000000" is in S, then:
   return true
otherwise when "1111111" is in S, then:
   return true
return false

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
bool solve(string S){
   if (S.find("0000000") != string::npos){
      return true;
   }
   else if (S.find("1111111") != string::npos){
      return true;
   }
   return false;
}
int main(){
   string S = "1000000001001";
   cout << solve(S) << endl;
}

Input

"1000000001001"

Output

1
raja
Updated on 08-Apr-2022 11:36:50

Advertisements