Find the number of players who roll the dice when the dice output sequence is given Last Updated : 21 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string S and a number X. There are M players who roll the dice. A player keeps on rolling the dice until he gets a number other than X. In the string S, S[i] represents the number at ith roll of a dice. The task is to find M. Note that the last character in S will never be X.Examples: Input: s = "3662123", X = 6 Output: 5 First player rolls and gets 3. Second player rolls and gets 6, 6 and 2. Third player rolls and gets 1. Fourth player rolls and gets 2. Fifth player rolls and gets 3. Input: s = "1234223", X = 2 Output: 4 Approach: Iterate in the string and count the characters which are not X. The number of characters which are not X will be the number of players.Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the number of players int findM(string s, int x) { // Initialize cnt as 0 int cnt = 0; // Iterate in the string for (int i = 0; i < s.size(); i++) { // Check for numbers other than x if (s[i] - '0' != x) cnt++; } return cnt; } // Driver code int main() { string s = "3662123"; int x = 6; cout << findM(s, x); return 0; } Java // Java implementation of the approach import java.util.*; class GFG { // Function to return the number of players static int findM(String s, int x) { // Initialize cnt as 0 int cnt = 0; // Iterate in the string for (int i = 0; i < s.length(); i++) { // Check for numbers other than x if (s.charAt(i) - '0' != x) cnt++; } return cnt; } // Driver code public static void main(String args[]) { String s = "3662123"; int x = 6; System.out.println(findM(s, x)); } } //This code is contributed by // Surendra_Gangwar Python3 # Python 3 implementation of the approach # Function to return the number of players def findM(s, x): # Initialize cnt as 0 cnt = 0 # Iterate in the string for i in range(len(s)): # Check for numbers other than x if (ord(s[i]) - ord('0') != x): cnt += 1 return cnt # Driver code if __name__ == '__main__': s = "3662123" x = 6 print(findM(s, x)) # This code is contributed by # Surendra_Gangwar C# // C# implementation of the approach using System; class GFG { // Function to return the number of players static int findM(String s, int x) { // Initialize cnt as 0 int cnt = 0; // Iterate in the string for (int i = 0; i < s.Length; i++) { // Check for numbers other than x if (s[i] - '0' != x) cnt++; } return cnt; } // Driver code public static void Main() { String s = "3662123"; int x = 6; Console.Write(findM(s, x)); } } // This code is contributed by // mohit kumar PHP <?php // PHP implementation of the approach // Function to return the number of players function findM($s, $x) { // Initialize cnt as 0 $cnt = 0; // Iterate in the string for ($i = 0; $i < strlen($s); $i++) { // Check for numbers other than x if (ord($s[$i]) - ord('0') != $x) $cnt++; } return $cnt; } // Driver code $s = "3662123"; $x = 6; echo findM($s, $x); // This code is contributed by Ryuga ?> JavaScript <script> // javascript implementation of the approach // Function to return the number of players function findM( s , x) { // Initialize cnt as 0 var cnt = 0; // Iterate in the string for (i = 0; i < s.length; i++) { // Check for numbers other than x if (s.charCodeAt(i) - '0'.charCodeAt(0) != x) cnt++; } return cnt; } // Driver code var s = "3662123"; var x = 6; document.write(findM(s, x)); // This code contributed by Rajput-Ji </script> Output: 5 Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the length of the string. Auxiliary Space: O(1), as we are not using any extra. Comment More infoAdvertise with us Next Article Score of two players after the alternative round of game S Striver Follow Improve Article Tags : Strings Game Theory DSA Practice Tags : Game TheoryStrings Similar Reads Find probability that a player wins when probabilities of hitting the target are given Given four integers p, q, r, and s. Two players are playing a game where both the players hit a target and the first player who hits the target wins the game. The probability of the first player hitting the target is p / q and that of the second player hitting the target is r / s. The task is to fin 5 min read Find the number of subsequences of N friends Given two arrays A[] and B[] of length N and M respectively. A[] represents the age of N friends and B[] contains M number of pairs in the form of (X â Y), which denotes X knows Y and vice-versa. Then your task is to output the count of all possible sequences of length N friends with the given condi 15 min read Score of two players after the alternative round of game Given an array nums of size n. There are two people in the game P1 and P2, where P1 starts the game. The game is as follows:Each person always chooses the first number in the nums adds it to their score and then removes the first element.If the removed number is even then reverse the rest of the arr 8 min read Find the player who will win by choosing a number in range [1, K] with sum total N Given two integers k and n. Two players are playing a game with these two numbers, Player1 and Player2. Player1 and Player2 take turns with Player1 starting first. At each turn, a player can choose a number in the range 1 to k, both inclusive and subtract the chosen value from n. The game ends when 6 min read Number of wins for each player in a series of Rock-Paper-Scissor game Two players are playing a series of games of Rockâpaperâscissors. There are a total of K games played. Player 1 has a sequence of moves denoted by string A and similarly player 2 has string B. If any player reaches the end of their string, they move back to the start of the string. The task is to co 11 min read Probability that the sum of all numbers obtained on throwing a dice N times lies between two given integers Given three integers N, A, and B, the task is to calculate the probability that the sum of numbers obtained on throwing the dice exactly N times lies between A and B. Examples: Input: N = 1, A = 2, B = 3Output: 0.333333Explanation: Ways to obtained the sum 2 by N ( = 1) throws of a dice is 1 {2}. Th 15 min read Like