# Check if a given mobile number is fancy in C++

C++Server Side ProgrammingProgramming

We have a 10 digit mobile number, our task is to check whether the number is fancy number or not. There are three different conditions for a fancy number. If at least one is true, then the number is fancy. These conditions are like below −

• A single number occurs three consecutive times, like 555
• Three consecutive numbers are either in increasing or decreasing order like 123 or 321.
• A single digit occurs four or more times in a number, like 8965499259, here 9 has occurred four times.

One example of fancy number is 9859009976, this is a fancy number as the third condition satisfies.

We will take the number as string, for condition three, count the frequency of each number, a basic concept of hashing is used here.

## Example

Live Demo

#include <iostream>
#define N 5
using namespace std;
bool consecutiveThreeSameDigits(string s) {
for (int i = 0; i < s.size() - 2; i++) {
if (s[i] == s[i + 1] && s[i + 1] == s[i + 2])
return true;
}
return false;
}
bool incDecThree(string s) {
for (int i = 0; i < s.size() - 2; i++) {
if ((s[i] < s[i + 1] && s[i + 1] < s[i + 2]) || (s[i] > s[i + 1] && s[i + 1] > s[i + 2]))
return true;
}
return false;
}
bool fourOccurrence(string s) {
int freq;
for(int i = 0; i < 10; i++)
freq[i] = 0;
for (int i = 0; i < s.size(); i++)
freq[s[i] - '0']++;
for (int i = 0; i < 9; i++)
if (freq[i] >= 4)
return true;
return false;
}
bool isFancyNumber(string s) {
if (consecutiveThreeSameDigits(s) || incDecThree(s) || fourOccurrence(s))
return true;
else
return false;
}
int main() {
string s = "7609438921";
if (isFancyNumber(s))
cout << "This is fancy number";
else
cout << "This is not a fancy number";
}

## Output

This is fancy number