**Introduction**-

This blog will discuss the problem to find the array which when sorted forms an AP and has least maximum. Before going into details of the problem, let's recall what an AP is:

"Arithmetic progression (AP) is a sequence of numbers in which difference of any two consecutive numbers is constant called the common difference."

In this problem, we will be given three positive integers' N', 'X,' and 'Y,' where X is less than Y. We have to find an array of positive integers containing both X and Y, which when sorted forms an AP and the length of the array should be 'N.' One more condition is that the maximum element of the array should be the least possible.

Let's understand the problem with an example:

Suppose N = 5, X = 20 and Y = 50

We have to find an array whose length is 5 containing 20 and 30 and which, when sorted forms an AP and has least maximum.

Consider the array: arr = {20, 30, 40, 50, 10}

Its sorted form will be: {10, 20, 30, 40, 50}

Here we can see that array "arr" has 5 elements containing both 20 and 50 and, when sorted, forms an AP with a common difference of 10.

Now that we understand the problem, let's discuss the approach to solve it in the next section.

**Solution Approach **

This section will discuss the approach to find the array which when sorted forms an AP and has least maximum. In simple words, we have to find the 'N' elements of the required array, out of which two elements, ' X' and 'Y,' are already given.

So, we have to find the remaining 'N-2' elements of the array such that when the array is sorted, it forms an AP, and the maximum element of the array should be the least possible.

The simple logic behind this approach is if we have to make the maximum element of the array the least possible, then we have to insert as many elements as possible between X and Y. The maximum number of elements that we can insert between X and Y is 'N-2'. So, we have to start with 'num = N-2' and keep checking whether it is possible to insert the 'num' number of elements between X and Y. If it is not possible to insert, decrease 'num' by 1.

After finding the number of elements to be inserted between X and Y, calculate the common difference of the AP, which will be formed after sorting the required array. Then find the required elements of the array. To check whether it is possible to insert the 'num' number of elements between X and Y, check if we can get an integer 'common difference 'by inserting the 'num' number of elements between X and Y.

Also see, __Euclid GCD Algorithm__