Javatpoint Logo
Javatpoint Logo

Zygodromes in Java

A positive number that is made by the identical digits running non-trivially is Zygodrome. It means if identical digits will always come together (in pairs) in the number, then that number is known as Zygodrome. Zyg is a Greek word, and its meaning is union or pair.

Example: 1

Input:

Int n = 99

Output: 99 is a Zygodrome.

Explanation: In the number 99, the digit 9 is occurring in pairs. Hence, the number 99 is Zygodrome.

Example: 2

Input:

int n = 909

Output: 909 is not a Zygodrome.

Explanation: In the number 909, the digit 9 is not occurring in pairs. There is a digit 0 between two nines. Hence, the number 909 is not Zygodrome.

Example: 3

Input:

int n = 1100

Output: 1100 is a Zygodrome.

Explanation: In the number 1100, the digit 1 is occurring in pairs, and 0 is also occurring in pairs. Hence, the number 1100 is Zygodrome.

Example: 4

Input:

int n = 4224

Output: 4224 is not a Zygodrome.

Explanation: In the number 4224, the digit 4 is not occurring in pairs as a pair of 2 is there in between two fours. Hence, the number 4224 is not Zygodrome.

Example: 5

Input:

int n = 442244

Output: 442244 is a Zygodrome.

Explanation: In the number 442244, the digit 4 is occurring in pairs, and the digit 2 is also occurring in a pair. Hence, the number 442244 is a Zygodrome.

Approach: Using While Loop

We can use a while loop. The while loop will traverse the digits of the given number and will check whether the number is Zygodrome or not with the help of an auxiliary array.

FileName: Zygodromes.java

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: The program is using two nested while-loops. However, the inner loop is also traversing the digits of the input number and also reducing the input number. Therefore, the time complexity of the program is O(d), where d is the total number of digits present in the input number. The program is using an auxiliary array. However, the size of the auxiliary array is constant. Hence, the space complexity of the program is O(1).

Approach: Using String

We can convert the input number into a string and can then make the comparison of the current, next, and previous characters of the string to check whether they are the same or not. It can be achieved using a single loop. See the following program.

FileName: Zygodromes.java

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: The time and space complexity of the program is the same as the previous one.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA