Posted on by Kalkicode
Code Number

# Check if a given mobile number is fancy

The given problem is to determine whether a given mobile number is "fancy" or not. A fancy number is defined by the following conditions:

1. It should not contain three adjacent digits that are the same.
2. It should not contain three adjacent digits that are either strictly increasing or strictly decreasing.
3. It should not contain any digit occurring more than three times.

To solve this problem, the code provided uses a Java program that checks these conditions for the given mobile number.

## Explanation with Example

Let's take two examples to understand the problem and its solution:

1. Test A: num = "8785917233"

• Case A: No three adjacent similar digits.
• Case B: The digits are not strictly increasing or decreasing.
• Case C: No digit occurs more than three times. Therefore, the output is "8785917233 is not a Fancy Number."
2. Test B: num = "9795017233"

• Case A: No three adjacent similar digits.
• Case B: The digits 9, 7, and 5 are strictly decreasing.
• Case C: No digit occurs more than three times. Therefore, the output is "9795017233 is a Fancy Number."

## Standard Pseudocode

``````function condition(num, n):
for i from 0 to n - 2:
if num[i] == num[i+1] and num[i+1] == num[i+2]:
return true
for i from 0 to n - 2:
if (num[i] > num[i+1] and num[i+1] > num[i+2]) or (num[i] < num[i+1] and num[i+1] < num[i+2]):
return true
initialize an array count[10] with zeros
for i from 0 to 9:
count[i] = 0
for i from 0 to 9:
count[num[i]-'0']++
if count[num[i]-'0'] >= 4:
return true
return false

function isFancyNumber(num):
n = length of num
if condition(num, n):
print num + " is Fancy Number"
else:
print num + " is not Fancy Number"
``````

## Algorithm Explanation

1. The function `condition(num, n)` checks the three cases described earlier:

• Case A: It loops through the mobile number `num` and checks for three adjacent similar digits.
• Case B: It also loops through the number and checks for three adjacent digits that are strictly increasing or decreasing.
• Case C: It initializes an array `count` to store the frequency of each digit and then checks if any digit occurs more than three times.
2. The function `isFancyNumber(num)` takes the mobile number `num` as input and calculates its length `n`. It then calls the `condition` function to check if the number satisfies the fancy conditions or not and prints the appropriate output accordingly.

## Code Solution

Here given code implementation process.

``````/*
Java Program for
Check if a given mobile number is fancy
*/
public class FancyNumber
{
public boolean condition(String num, int n)
{
// Case A
// Check if three Adjacent Similar
for (int i = 0; i < n - 2 ; ++i)
{
if(num.charAt(i)==num.charAt(i+1) &&
num.charAt(i+1) ==num.charAt(i+2))
{
return true;
}
}
// Case B
// Check if three Adjacent digits are increasing or decreasing.
for (int i = 0; i < n - 2 ; ++i )
{
if((num.charAt(i) > num.charAt(i+1) &&
num.charAt(i+1) > num.charAt(i+2))||
(num.charAt(i) < num.charAt(i+1) &&
num.charAt(i+1) < num.charAt(i+2)) )
{
return true;
}
}

// Case C
// Check that if any digits are occur more than 3 times or not.
int []count = new int[10];
// Set initial frequency of possible digit
for (int i = 0; i < 10 ; ++i )
{
count[i]=0;
}
// count frequency of each digit
for (int i = 0; i < 10 ; ++i )
{
count[num.charAt(i)- '0']++;

if(count[num.charAt(i)- '0'] >= 4)
{
return true;
}
}

return false;
}

public void isFancyNumber(String num)
{
int n = num.length();
// Assume num is valid number
if(condition(num,n))
{
System.out.println(" "+num+" is Fancy Number");
}
else
{
System.out.println(" "+num+" is not Fancy Number");
}
}

public static void main(String[] args)
{
// Test A
// Test B
}
}``````

#### Output

`````` 8785917233 is not Fancy Number
9795017233 is Fancy Number``````
``````// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
C++ Program for
Check if a given mobile number is fancy
*/
class FancyNumber
{
public: bool condition(string num, int n)
{
// Case A
// Check if three Adjacent Similar
for (int i = 0; i < n - 2; ++i)
{
if (num[i] == num[i + 1] &&
num[i + 1] == num[i + 2])
{
return true;
}
}
// Case B
// Check if three Adjacent digits are increasing or decreasing.
for (int i = 0; i < n - 2; ++i)
{
if ((num[i] > num[i + 1] &&
num[i + 1] > num[i + 2]) ||
(num[i] < num[i + 1] && num[i + 1] < num[i + 2]))
{
return true;
}
}
// Case C
// Check that if any digits are occur more than 3 times or not.
int *count = new int[10];
// Set initial frequency of possible digit
for (int i = 0; i < 10; ++i)
{
count[i] = 0;
}
// count frequency of each digit
for (int i = 0; i < 10; ++i)
{
count[num[i] - '0']++;
if (count[num[i] - '0'] >= 4)
{
return true;
}
}
return false;
}
void isFancyNumber(string num)
{
int n = num.length();
// Assume num is valid number
if (this->condition(num, n))
{
cout << " " << num << " is Fancy Number" << endl;
}
else
{
cout << " " << num << " is not Fancy Number" << endl;
}
}
};
int main()
{
// Test A
// Test B
return 0;
}``````

#### Output

`````` 8785917233 is not Fancy Number
9795017233 is Fancy Number``````
``````// Include namespace system
using System;
/*
Csharp Program for
Check if a given mobile number is fancy
*/
public class FancyNumber
{
public Boolean condition(String num, int n)
{
// Case A
// Check if three Adjacent Similar
for (int i = 0; i < n - 2; ++i)
{
if (num[i] == num[i + 1] &&
num[i + 1] == num[i + 2])
{
return true;
}
}
// Case B
// Check if three Adjacent digits are increasing or decreasing.
for (int i = 0; i < n - 2; ++i)
{
if ((num[i] > num[i + 1] &&
num[i + 1] > num[i + 2]) ||
(num[i] < num[i + 1] && num[i + 1] < num[i + 2]))
{
return true;
}
}
// Case C
// Check that if any digits are occur more than 3 times or not.
int[] count = new int[10];
// Set initial frequency of possible digit
for (int i = 0; i < 10; ++i)
{
count[i] = 0;
}
// count frequency of each digit
for (int i = 0; i < 10; ++i)
{
count[num[i] - '0']++;
if (count[num[i] - '0'] >= 4)
{
return true;
}
}
return false;
}
public void isFancyNumber(String num)
{
int n = num.Length;
// Assume num is valid number
if (this.condition(num, n))
{
Console.WriteLine(" " + num + " is Fancy Number");
}
else
{
Console.WriteLine(" " + num + " is not Fancy Number");
}
}
public static void Main(String[] args)
{
// Test A
// Test B
}
}``````

#### Output

`````` 8785917233 is not Fancy Number
9795017233 is Fancy Number``````
``````package main

import "fmt"
/*
Go Program for
Check if a given mobile number is fancy
*/

func condition(num string, n int) bool {
// Case A
// Check if three Adjacent Similar
for i := 0 ; i < n - 2 ; i++ {
if num[i] == num[i + 1] && num[i + 1] == num[i + 2] {
return true
}
}
// Case B
// Check if three Adjacent digits are increasing or decreasing.
for i := 0 ; i < n - 2 ; i++ {
if (num[i] > num[i + 1] && num[i + 1] > num[i + 2]) ||
(num[i] < num[i + 1] && num[i + 1] < num[i + 2]) {
return true
}
}
// Case C
// Check that if any digits are occur more than 3 times or not.
// Set initial frequency of possible digit
var count = make([] int, 10)
// count frequency of each digit
for i := 0 ; i < 10 ; i++ {
count[int(num[i]) - 48]++
if count[int(num[i]) - 48] >= 4 {
return true
}
}
return false
}
func isFancyNumber(num string) {
var n int = len(num)
// Assume num is valid number
if condition(num, n) {
fmt.Println(" ", num, " is Fancy Number")
} else {
fmt.Println(" ", num, " is not Fancy Number")
}
}
func main() {

// Test A
isFancyNumber("8785917233")
// Test B
isFancyNumber("9795017233")
}``````

#### Output

`````` 8785917233 is not Fancy Number
9795017233 is Fancy Number``````
``````<?php
/*
Php Program for
Check if a given mobile number is fancy
*/
class FancyNumber
{
public	function condition(\$num, \$n)
{
// Case A
// Check if three Adjacent Similar
for (\$i = 0; \$i < \$n - 2; ++\$i)
{
if (\$num[\$i] == \$num[\$i + 1] &&
\$num[\$i + 1] == \$num[\$i + 2])
{
return true;
}
}
// Case B
// Check if three Adjacent digits are increasing or decreasing.
for (\$i = 0; \$i < \$n - 2; ++\$i)
{
if ((\$num[\$i] > \$num[\$i + 1] &&
\$num[\$i + 1] > \$num[\$i + 2]) ||
(\$num[\$i] < \$num[\$i + 1] &&
\$num[\$i + 1] < \$num[\$i + 2]))
{
return true;
}
}
// Case C
// Check that if any digits are occur more than 3 times or not.
\$count = array_fill(0, 10, 0);
// Set initial frequency of possible digit
for (\$i = 0; \$i < 10; ++\$i)
{
\$count[\$i] = 0;
}
// count frequency of each digit
for (\$i = 0; \$i < 10; ++\$i)
{
\$count[ord(\$num[\$i]) - ord('0')]++;
if (\$count[ord(\$num[\$i]) - ord('0')] >= 4)
{
return true;
}
}
return false;
}
public	function isFancyNumber(\$num)
{
\$n = strlen(\$num);
// Assume num is valid number
if (\$this->condition(\$num, \$n))
{
echo(" ".\$num." is Fancy Number\n");
}
else
{
echo(" ".\$num." is not Fancy Number\n");
}
}
}

function main()
{
// Test A
// Test B
}
main();``````

#### Output

`````` 8785917233 is not Fancy Number
9795017233 is Fancy Number``````
``````/*
Node JS Program for
Check if a given mobile number is fancy
*/
class FancyNumber
{
condition(num, n)
{
// Case A
// Check if three Adjacent Similar
for (var i = 0; i < n - 2; ++i)
{
if (num.charAt(i) ==
num.charAt(i + 1)
&& num.charAt(i + 1) == num.charAt(i + 2))
{
return true;
}
}
// Case B
// Check if three Adjacent digits are increasing or decreasing.
for (var i = 0; i < n - 2; ++i)
{
if ((num.charAt(i) > num.charAt(i + 1) &&
num.charAt(i + 1) > num.charAt(i + 2)) ||
(num.charAt(i) < num.charAt(i + 1) &&
num.charAt(i + 1) < num.charAt(i + 2)))
{
return true;
}
}
// Case C
// Check that if any digits are occur more than 3 times or not.
var count = Array(10).fill(0);
// Set initial frequency of possible digit
for (var i = 0; i < 10; ++i)
{
count[i] = 0;
}
// count frequency of each digit
for (var i = 0; i < 10; ++i)
{
count[num.charCodeAt(i) - 48]++;
if (count[num.charCodeAt(i)- 48] >= 4)
{
return true;
}
}
return false;
}
isFancyNumber(num)
{
var n = num.length;
// Assume num is valid number
if (this.condition(num, n))
{
console.log(" " + num + " is Fancy Number");
}
else
{
console.log(" " + num + " is not Fancy Number");
}
}
}

function main()
{
// Test A
// Test B
}
main();``````

#### Output

`````` 8785917233 is not Fancy Number
9795017233 is Fancy Number``````
``````#    Python 3 Program for
#    Check if a given mobile number is fancy
class FancyNumber :
def condition(self, num, n) :
i = 0
#  Case A
#  Check if three Adjacent Similar
while (i < n - 2) :
if (num[i] == num[i + 1] and num[i + 1] == num[i + 2]) :
return True

i += 1

i = 0
#  Case B
#  Check if three Adjacent digits are increasing or decreasing.
while (i < n - 2) :
if ((num[i] > num[i + 1] and
num[i + 1] > num[i + 2]) or
(num[i] < num[i + 1] and num[i + 1] < num[i + 2])) :
return True

i += 1

#  Case C
#  Check that if any digits are occur more than 3 times or not.
#  Set initial frequency of possible digit
count = [0] * (10)
i = 0
#  count frequency of each digit
while (i < 10) :
count[ord(num[i]) - ord('0')] += 1
if (count[ord(num[i]) - ord('0')] >= 4) :
return True

i += 1

return False

def isFancyNumber(self, num) :
n = len(num)
#  Assume num is valid number
if (self.condition(num, n)) :
print(" ", num ," is Fancy Number")
else :
print(" ", num ," is not Fancy Number")

def main() :
#  Test A
#  Test B

if __name__ == "__main__": main()``````

#### Output

``````  8785917233  is not Fancy Number
9795017233  is Fancy Number``````
``````#    Ruby Program for
#    Check if a given mobile number is fancy
class FancyNumber
def condition(num, n)
i = 0
#  Case A
#  Check if three Adjacent Similar
while (i < n - 2)
if (num[i] == num[i + 1] && num[i + 1] == num[i + 2])
return true
end

i += 1
end

i = 0
#  Case B
#  Check if three Adjacent digits are increasing or decreasing.
while (i < n - 2)
if ((num[i] > num[i + 1] &&
num[i + 1] > num[i + 2]) ||
(num[i] < num[i + 1] && num[i + 1] < num[i + 2]))
return true
end

i += 1
end

#  Case C
#  Check that if any digits are occur more than 3 times or not.
#  Set initial frequency of possible digit
count = Array.new(10) {0}
i = 0
#  count frequency of each digit
while (i < 10)
count[num[i].ord - '0'.ord] += 1
if (count[num[i].ord - '0'.ord] >= 4)
return true
end

i += 1
end

return false
end

def isFancyNumber(num)
n = num.length
#  Assume num is valid number
if (self.condition(num, n))
print(" ", num ," is Fancy Number", "\n")
else

print(" ", num ," is not Fancy Number", "\n")
end

end

end

def main()
#  Test A
#  Test B
end

main()``````

#### Output

`````` 8785917233 is not Fancy Number
9795017233 is Fancy Number
``````
``````/*
Scala Program for
Check if a given mobile number is fancy
*/
class FancyNumber()
{
def condition(num: String, n: Int): Boolean = {
var i: Int = 0;
// Case A
// Check if three Adjacent Similar
while (i < n - 2)
{
if (num.charAt(i) == num.charAt(i + 1) &&
num.charAt(i + 1) == num.charAt(i + 2))
{
return true;
}
i += 1;
}
i = 0;
// Case B
// Check if three Adjacent digits are increasing or decreasing.
while (i < n - 2)
{
if ((num.charAt(i) > num.charAt(i + 1) &&
num.charAt(i + 1) > num.charAt(i + 2)) ||
(num.charAt(i) < num.charAt(i + 1) &&
num.charAt(i + 1) < num.charAt(i + 2)))
{
return true;
}
i += 1;
}
// Case C
// Check that if any digits are occur more than 3 times or not.
// Set initial frequency of possible digit
var count: Array[Int] = Array.fill[Int](10)(0);
i = 0;
// count frequency of each digit
while (i < 10)
{
count(num.charAt(i).toInt - '0'.toInt) += 1;
if (count(num.charAt(i).toInt - '0'.toInt) >= 4)
{
return true;
}
i += 1;
}
return false;
}
def isFancyNumber(num: String): Unit = {
var n: Int = num.length();
// Assume num is valid number
if (condition(num, n))
{
println(" " + num + " is Fancy Number");
}
else
{
println(" " + num + " is not Fancy Number");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: FancyNumber = new FancyNumber();
// Test A
// Test B
}
}``````

#### Output

`````` 8785917233 is not Fancy Number
9795017233 is Fancy Number``````
``````import Foundation;
/*
Swift 4 Program for
Check if a given mobile number is fancy
*/
class FancyNumber
{
func condition(_ text: String, _ n: Int) -> Bool
{
var num = Array(text);
var i: Int = 0;
// Case A
// Check if three Adjacent Similar
while (i < n - 2)
{
if (num[i] == num[i + 1] && num[i + 1] == num[i + 2])
{
return true;
}
i += 1;
}
i = 0;
// Case B
// Check if three Adjacent digits are increasing or decreasing.
while (i < n - 2)
{
if ((num[i] > num[i + 1] &&
num[i + 1] > num[i + 2]) ||
(num[i] < num[i + 1] && num[i + 1] < num[i + 2]))
{
return true;
}
i += 1;
}
// Case C
// Check that if any digits are occur more than 3 times or not.
// Set initial frequency of possible digit
var count: [Int] = Array(repeating: 0, count: 10);
i = 0;
// count frequency of each digit
while (i < 10)
{
count[Int(UnicodeScalar(String(num[i]))!.value) - 48] += 1;
if (count[Int(UnicodeScalar(String(num[i]))!.value) - 48] >= 4)
{
return true;
}
i += 1;
}
return false;
}
func isFancyNumber(_ num: String)
{
let n: Int = num.count;
// Assume num is valid number
if (self.condition(num, n))
{
print(" ", num ," is Fancy Number");
}
else
{
print(" ", num ," is not Fancy Number");
}
}
}
func main()
{
// Test A
// Test B
}
main();``````

#### Output

``````  8785917233  is not Fancy Number
9795017233  is Fancy Number``````
``````/*
Kotlin Program for
Check if a given mobile number is fancy
*/
class FancyNumber
{
fun condition(num: String, n: Int): Boolean
{
var i: Int = 0;
// Case A
// Check if three Adjacent Similar
while (i < n - 2)
{
if (num.get(i) == num.get(i + 1) &&
num.get(i + 1) == num.get(i + 2))
{
return true;
}
i += 1;
}
i = 0;
// Case B
// Check if three Adjacent digits are increasing or decreasing.
while (i < n - 2)
{
if ((num.get(i) > num.get(i + 1) &&
num.get(i + 1) > num.get(i + 2)) ||
(num.get(i) < num.get(i + 1) &&
num.get(i + 1) < num.get(i + 2)))
{
return true;
}
i += 1;
}
// Case C
// Check that if any digits are occur more than 3 times or not.
// Set initial frequency of possible digit
val count: Array < Int > = Array(10)
{
0
};
i = 0;
// count frequency of each digit
while (i < 10)
{
count[num.get(i).toInt() - '0'.toInt()] += 1;
if (count[num.get(i).toInt() - '0'.toInt()] >= 4)
{
return true;
}
i += 1;
}
return false;
}
fun isFancyNumber(num: String): Unit
{
val n: Int = num.length;
// Assume num is valid number
if (this.condition(num, n))
{
println(" " + num + " is Fancy Number");
}
else
{
println(" " + num + " is not Fancy Number");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test A
// Test B
}``````

#### Output

`````` 8785917233 is not Fancy Number
9795017233 is Fancy Number``````

## Time Complexity Analysis

Let's analyze the time complexity of the code:

• Case A and Case B both loop through the entire mobile number, so each of them takes O(n) time.
• Case C uses an array `count` of size 10 and loops through the mobile number, taking O(n) time.
• The main function `isFancyNumber` calls the `condition` function, which has a time complexity of O(n) as explained above.

Overall, the time complexity of the entire code is O(n).

## Finally

The provided Java program effectively checks whether a given mobile number is fancy or not based on the specified conditions. The algorithm presented in the code runs in linear time complexity, making it efficient for practical use.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post