# [Java] [Algorithm] N numbers with intervals of X

less than 1 minute read

# Problem description

The method solution takes a parameter of integer x and a natural number n and returns a list with n numbers starting from x and incrementing by x. Please see the following conditions and create a method solution that satisfies the conditions.

# Condition

• x is an integer of -10000000 or more and 10000000 or less.
• n is a natural number less than 1000.

# I/O example

| x | n | result | |:—————–|——————:|:——— ———:| | 2| 5| [2,4,6,7,10]| | 4| 3| [4,8,12]| | -4| 2| [-4,-8]|

# Commentary

*The explanation is a code I created, so if you have a better algorithm, please share it!

``````
class Solution {
public long[] solution(int x, int n) {
long[] result = new long[n];
result[0] = x; //Begin with x, so initialize x to Index 0

//Since number 0 is initialized above, i starts from 1 and repeats until n
for (int i = 1; i <n; i++) {
// As it increases by x, perform Index: i-1 value + x of result.
result[i] = result[i-1] + x;
}

return result;
}
}
``````

Tags:

Updated: