Javatpoint Logo
Javatpoint Logo

Straight Line Numbers in Java

A number n can be said to be a straight-line number if the digits of the number form an arithmetic progression. It is evident that to decide whether the digits are in arithmetic progression or not. There is a requirement of at least three digits. Therefore, straight-line numbers should always contain at least three digits while excluding the leading zeros.

Example 1:

Input: N = 178

Output: No, 178 is not a Straight-Line number.

Explanation: The digits of the number 178 are 1, 7, 8. 7 - 1 = 6, and 8 - 7 = 1. Thus, the common difference is not the same, and hence, the digits are not in the arithmetic progression. Thus, 178 is not a Straight-Line number.

Example 2:

Input: N = 111

Output: Yes, 111 is a Straight-Line number.

Explanation: The digits of the number 111 are 1, 1, and 1. 1 - 1 = 0, and 1 - 1 = 0. Thus, the common difference is the same, and hence, the digits are in the arithmetic progression. Thus, 111 is a Straight-Line number.

Example 3:

Input: N = 25

Output: No, 25 is not a Straight-Line number.

Explanation: The digits of the number 25 are 2 and 5. The two digits are not sufficient to decide whether they are in arithmetic progression or not. Thus, 25 is not a Straight-Line number.

Naïve Approach

The approach is simple. First of all, we have to store all the digits of the number in an array. Then, using a loop, we can find the difference of two consecutive elements while iterating from either left to right or from right to left. If the difference of all of the two consecutive elements is the same, then we can say that the number is a straight-line number; otherwise, it is not.

FileName: StraightLineNumbersNaive.java

Output:

No, 178 is not a Straight Line number.
Yes, 111 is a Straight Line number.
No, 25 is not a Straight Line number.

Complexity Analysis: The loops used in the program iterates until all the digits of the number is explored. Thus, making the time complexity of the program O(d). The program is also storing the digits in an array list, which makes the space complexity of the program O(d), where d is the total number of digits present in the input number.

Approach: Using Strings

In this approach, first, we need to convert the number into a string. Then, we can iterate over the characters to find whether they are in arithmetic progression or not, and accordingly, we can decide whether they are straight-line numbers or not. The illustration of it is mentioned in the following program.

FileName: StraightLineNumbersStr.java

Output:

No, 178 is not a Straight Line number.
Yes, 111 is a Straight Line number.
No, 25 is not a Straight Line number.

Complexity Analysis: The time, as well as the space complexity of the program, is the same as the previous program.

We can avoid using strings and array list to reduce the space complexity. The illustration of it is mentioned below.

FileName: StraightLineNumbersOptimized.java

Output:

No, 178 is not a Straight Line number.
Yes, 111 is a Straight Line number.
No, 25 is not a Straight Line number.

Complexity Analysis: The time, as well as the space complexity of the program, is the same as the previous program.

Output:

No, 178 is not a Straight Line number.
Yes, 111 is a Straight Line number.
No, 25 is not a Straight Line number.

Complexity Analysis: The time complexity of the program is O(d), where d is the total number of digits present in the number. The space complexity of the program is O(1).

Finding Straight Line Numbers Within a Range

In this section, we will see how to filter the straight-line numbers within a range.

FileName: StraightLineNumbersRange.java

Output:

Straight Line Numbers from range 100 and 1000 are:
111 123 135 147 159 210 222 234 246 258 321 333 345 357 369 420 432 444 456 468 531 543 555 567 579 630 642 654 666 678 741 753 765 777 789 840 852 864 876 888 951 963 975 987 999






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