Zygodromes in Java
Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg" means "union" or "pair."
Example: 1
Input: int n = 88
Output: 88 is Zygodrome.
Explanation
For example, 88 the digit 8 occurs in pairs. Hence, the number 88 is Zygodrome.
Example: 2
Input: int n = 808
Output: 808 is not a Zygodrome.
Description:
In 808, the digit 8 is not occurring in pairs. There is a digit 0 between two eights. Hence, the number 808 is not Zygodrome.
Java Program to find Zygodromes using While loop
A while loop can be used. The while loop iterates through the provided number's digits, using an auxiliary array to determine whether the number is zygodrome or not.
Zygodromes.java
Import java . util . * ;
public class Zygodromes
{
public boolean isZygodromes ( int n )
{
int d [ ] = new int [ 10 ] ;
while ( n ! = 0 )
{
int countSameDig = 0 ;
// find the current digit
int currDig = n % 10 ;
int temp = currDig ;
while( currDig == temp )
{
countSameDig = countSameDig + 1 ;
n = n / 10 ;
temp = n % 10 ;
}
if ( countSameDig == 1 )
{
return false ;
}
d [ currDig ] = countSameDig ;
countSameDig = 0 ;
}
return true ;
}
// execution of the program starts in the main section
public static void main ( String s [ ] )
{
Zygodromes o1 = new Zygodromes ( ) ;
int n1 = 99 ;
boolean isZygodrome = o1 . isZygodromes ( n1 ) ;
if ( isZygodrome )
{
System . out . println ( " taken number from user " + n1 + " is a Zygodrome . " ) ;
}
else
{
System . out . println (" Given number " + n1 + " is not a Zygodrome ." ) ;
}
System . out . println ( ) ;
// 2nd input
n1 = 909 ;
// calling the isZygodromes function and storing the outcome
isZygodrome = o1 . isZygodromes ( n1 ) ;
if ( isZygodromes )
{
System . out . println ( " Given number " + n1 + " is a Zygodrome . " ) ;
}
else
{
System . out . println ( " Given number " + n1 + " is not a Zygodrome . " ) ;
}
System . out . println ( ) ;
// 3rd input from user
n1 = 1100 ;
// calling method isZygodromes and storing the result in variable
isZygodrome = o1 . isZygodromes ( n1 ) ;
if ( isZygodromes )
{
System . out . println ( " The number " + n1 + " is a Zygodrome . " ) ;
}
else
{
System . out . println ( "Given number " + n1 + " is not a Zygodrome . " ) ;
}
System . out . println ( ) ;
// 4rth input from user
int n4 = 4224 ;
// calling the method isZygodromes and storing the result in the variable
isZygodrome = o1 . isZygodromes ( n4 ) ;
if ( isZygodromes )
{
System . out . println ( " Given number " + n4 + " is a Zygodrome . " ) ;
}
else
{
System . out . println ( " Given number " + n4 + " is not a Zygodrome ." ) ;
}
System . out . println ( ) ;
// 5TH INPUT FROM THE USER
int n5 = 442244 ;
// invoking the method isZygodromes and storing the result
isZygodrome = o1 . isZygodromes ( n5 ) ;
if ( isZygodromes )
{
System . out . println ( " Given number " + n5 + " is a Zygodrome . " ) ;
}
else
{
System . out . println ( " Given number " + n5 + " is not a Zygodrome. " ) ;
}
}
}
Output
The number 99 is a Zygodrome.
The number 909 is not a Zygodrome.
The number 1100 is a Zygodrome.
The number 4224 is not a Zygodrome.
The number 442244 is a Zygodrome.
Complexity Analysis
According to complexity analysis, the programme uses two nested while-loops. However, the inner loop also reduces the input number by iterating over its digits. The program's temporal complexity is therefore O(d), where d is the total number of digits in the input number. A second array is used by the programme. The size of the auxiliary array is fixed, though. As a result, O is the program's space complexity (1).
Java Program to find Zygodromes using Strings
The input number can be changed into a string, and the current, next, and previous characters of the string can then be compared to determine whether or not they match. A single loop can be used to accomplish it.
import java.util.* ;
public class Main
{
public boolean isZygodromes ( int n )
{
String t = Integer . toString ( n ) ;
t = ' ' + t + ' ';
for ( int k = 1 ; k < t . length ( ) – 1 ; k++ )
{
if ( t . charAt ( k ) != t . charAt ( k - 1) && t . charAt ( k ) != t . charAt ( k + 1) )
{
return false ;
}
}
return true ;
}
public static void main ( String s[ ] )
{
Main o1 = new Main ( ) ;
Scanner sc = new Scanner ( System . in ) ;
System.out . println(" enter a number ");
int n1 = sc.nextInt() ;
boolean isZygodrome = o1 . isZygodromes ( n1 ) ;
if ( isZygodrome )
{
System . out . println ( " taken input " + n1 + " is a Zygodrome ." ) ;
}
else
{
System . out . println (" taken input " + n1 + " is not a Zygodrome ." ) ;
}
System . out . println ( ) ;
System.out.println(" enter a number ");
int n2 = sc.nextInt() ;
isZygodrome = o1.isZygodromes(n2);
if(isZygodrome)
{
System.out.println(" taken input " + n2 + " is a Zygodrome.");
}
else
{
System.out.println(" taken input " + n2 + " is not a Zygodrome.");
}
System . out . println ( ) ;
System . out . println ( " enter a number " ) ;
int n3 = sc . nextInt ( ) ;
isZygodrome = o1.isZygodromes(n3);
if ( isZygodrome )
{
System . out . println ( " taken input " + n3 + " is a Zygodrome. " ) ;
}
else
{
System.out.println(" taken input " + n3 + " is not a Zygodrome.");
}
System.out.println();
System.out.println(" enter a number ");
int n4 = sc.nextInt() ;
isZygodrome = o1.isZygodromes(n4) ;
if(isZygodrome)
{
System . out . println ( " taken input " + n4 + " is a Zygodrome. " ) ;
}
else
{
System . out . println ( " taken input " + n4 + " is not a Zygodrome ." ) ;
}
System . out . println ( ) ;
// 5th input
System.out.println(" enter a number ");
int n5 = sc.nextInt() ;
isZygodrome = o1 . isZygodromes ( n5 ) ;
if ( isZygodrome )
{
System . out . println ( " taken input " + n5 + " is a Zygodrome ." ) ;
}
else
{
System . out . println ( " taken input " + n5 + " is not a Zygodrome ." ) ;
}
}
}
Output
taken input 99 is a Zygodrome .
taken input 808 is not a Zygodrome .
taken input 1210 is not a Zygodrome.
taken input 2211 is a Zygodrome.
taken input 221166 is a Zygodrome.