- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a string S and a number X. There are M different players who roll the dice. one player keeps on rolling the dice until he gets a number other than X. Here in the string S, S[i] represents the number at ith roll of a dice. We have to find the value of M. One constraint is that the last character in S will never be X. So for example, if string is “3662123” and X = 6, the output will be 5. This can be described as follows −

- First player rolls and got 3
- Second player rolls, and got 6, 6 and 2
- Third player rolls, and got 1
- Fourth player rolls, and got 2
- Fifth player rolls, and got 3

The task is simple, we will traverse the string, and count the number of characters, that are not X, the count will be the answer.

#include<iostream> using namespace std; int countPlayers(string str, int x) { int count = 0; for (int i = 0; i < str.size(); i++) { if (str[i] - '0' != x) count++; } return count; } int main() { string s = "3662123"; int x = 6; cout << "Number of players: " << countPlayers(s, x); }

Number of players: 5

- Related Questions & Answers
- Dice Roll Simulation in C++
- Number of Dice Rolls With Target Sum in Python
- Maximum number of dots after throwing a dice N times in C++
- C++ Program to Emulate N Dice Roller
- C++ program to count how many ways two players win or make draw in dice throwing game
- C++ code to count number of dice rolls to get target x
- Find if the given number is present in the infinite sequence or not in C++
- Probability of getting more value in third dice throw in C++
- Find the other number when LCM and HCF given in C++
- Find the slope of the given number using C++
- Find the number of solutions to the given equation in C++
- C++ Program to Find the Longest Increasing Subsequence of a Given Sequence
- C++ Program to Find the Longest Prefix Matching of a Given Sequence
- Find the largest good number in the divisors of given number N in C++
- Find the Number of Quadrilaterals Possible from the Given Points using C++

Advertisements