×

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.

Related Topics

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Java String split() method

Java String split() method split current String against given regular expression and returns a char array. Syntax: public String split(String regex)                 public String split(String regex, int...

2 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Accessors and Mutator in Java

Introduction Accessors and mutators are used in Java to get and set the value of private fields, respectively. Accessors and mutators are both referred to as getters and setters, respectively. The...

6 minutes read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Loose Coupling in Java

Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable...

3 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.