Given two integers **A**, **B** which are any two terms of an Arithmetic Progression series, and an integer **N**, the task is to minimize the **N**^{th} term of that arithmetic progression.

**Note:** All the elements of an AP series must be positive.

**Examples:**

Input:A = 1, B = 6, N = 3Output:11Explanations:First three terms of the given arithmetic progression are: {1, 6, 11}.

Therefore, the required output is 11.

Input:A = 20, B = 50, N = 5Output:50Explanations:First Five terms of the given arithmetic progression are: {10, 20, 30, 40, 50}.

Therefore, the required output is 50.

**Approach:** The problem can be solved by placing **A** and **B** at all possible positions in an Arithmetic Progression and check which generates the least possible **N**^{th} term. Considering the positions of **A** and **B** to be **i** and **j** respectively, then the following calculations need to be made:

Common Difference(D) of an AP = (B – A)/(j – i)

First term of an AP = A – (i – 1) * D

Nth term of an AP = First Term + (N – 1)* D

Finally, return the smallest** N**^{th} term obtained.

Follow the steps below to solve the problem:

- Initialize a variable, say
**res**to store the smallest possible value of the required**N**^{th}term of arithmetic progression. - Using two nested loops, place
**A**and**B**at all possible positions in an AP, and calculate the**N**^{th}term using above calculations. Keep updating**res**to store the least value of**N**^{th}term obtain. - Finally, print the value of
**res**as the required answer.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the smallest` `// Nth term of an AP possible` `int` `smallestNth(` `int` `A, ` `int` `B, ` `int` `N)` `{` ` ` `// Stores the smallest Nth term` ` ` `int` `res = INT_MAX;` ` ` `for` `(` `int` `i = 1; i < N; i++) {` ` ` `for` `(` `int` `j = N; j > i; j--) {` ` ` `// Check if common difference` ` ` `// of AP is an integer` ` ` `if` `((B - A) % (j - i) == 0) {` ` ` `// Store the common` ` ` `// difference` ` ` `int` `D = (B - A) / (j - i);` ` ` `// Store the First Term` ` ` `// of that AP` ` ` `int` `FirstTerm = A - (i - 1) * D;` ` ` `// Store the Nth term of` ` ` `// that AP` ` ` `int` `NthTerm = FirstTerm + (N - 1) * D;` ` ` `// Check if all elements of` ` ` `// an AP are positive` ` ` `if` `(FirstTerm > 0)` ` ` `res = min(res, NthTerm);` ` ` `}` ` ` `}` ` ` `}` ` ` `// Return the least` ` ` `// Nth term obtained` ` ` `return` `res;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 3;` ` ` `int` `A = 1;` ` ` `int` `B = 6;` ` ` `cout << smallestNth(A, B, N);` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.io.*;` `class` `GFG{` ` ` `// Function to find the smallest` `// Nth term of an AP possible` `static` `int` `smallestNth(` `int` `A, ` `int` `B, ` `int` `N)` `{` ` ` `// Stores the smallest Nth term` ` ` `int` `res = Integer.MAX_VALUE;` ` ` ` ` `for` `(` `int` `i = ` `1` `; i < N; i++) ` ` ` `{` ` ` `for` `(` `int` `j = N; j > i; j--)` ` ` `{` ` ` ` ` `// Check if common difference` ` ` `// of AP is an integer` ` ` `if` `((B - A) % (j - i) == ` `0` `) ` ` ` `{` ` ` ` ` `// Store the common` ` ` `// difference` ` ` `int` `D = (B - A) / (j - i);` ` ` ` ` `// Store the First Term` ` ` `// of that AP` ` ` `int` `FirstTerm = A - (i - ` `1` `) * D;` ` ` ` ` `// Store the Nth term of` ` ` `// that AP` ` ` `int` `NthTerm = FirstTerm + (N - ` `1` `) * D;` ` ` ` ` `// Check if all elements of` ` ` `// an AP are positive` ` ` `if` `(FirstTerm > ` `0` `)` ` ` `res = Math.min(res, NthTerm);` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// Return the least` ` ` `// Nth term obtained` ` ` `return` `res;` `}` ` ` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `N = ` `3` `;` ` ` `int` `A = ` `1` `;` ` ` `int` `B = ` `6` `;` ` ` ` ` `System.out.print(smallestNth(A, B, N));` `}` `}` `// This code is contributed by code_hunt` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `import` `sys` `# Function to find the smallest` `# Nth term of an AP possible` `def` `smallestNth(A, B, N):` ` ` ` ` `# Stores the smallest Nth term` ` ` `res ` `=` `sys.maxsize` ` ` `for` `i ` `in` `range` `(` `1` `, N):` ` ` `for` `j ` `in` `range` `(N, i, ` `-` `1` `):` ` ` `# Check if common difference` ` ` `# of AP is an integer` ` ` `if` `((B ` `-` `A) ` `%` `(j ` `-` `i) ` `=` `=` `0` `):` ` ` `# Store the common` ` ` `# difference` ` ` `D ` `=` `(B ` `-` `A) ` `/` `/` `(j ` `-` `i)` ` ` `# Store the First Term` ` ` `# of that AP` ` ` `FirstTerm ` `=` `A ` `-` `(i ` `-` `1` `) ` `*` `D` ` ` `# Store the Nth term of` ` ` `# that AP` ` ` `NthTerm ` `=` `FirstTerm ` `+` `(N ` `-` `1` `) ` `*` `D` ` ` `# Check if all elements of` ` ` `# an AP are positive` ` ` `if` `(FirstTerm > ` `0` `):` ` ` `res ` `=` `min` `(res, NthTerm)` ` ` `# Return the least` ` ` `# Nth term obtained` ` ` `return` `res` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `N ` `=` `3` ` ` `A ` `=` `1` ` ` `B ` `=` `6` ` ` ` ` `print` `(smallestNth(A, B, N))` ` ` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System;` `class` `GFG{` ` ` `// Function to find the smallest` `// Nth term of an AP possible` `static` `int` `smallestNth(` `int` `A, ` `int` `B, ` `int` `N)` `{` ` ` ` ` `// Stores the smallest Nth term` ` ` `int` `res = Int32.MaxValue;` ` ` ` ` `for` `(` `int` `i = 1; i < N; i++)` ` ` `{` ` ` `for` `(` `int` `j = N; j > i; j--) ` ` ` `{` ` ` ` ` `// Check if common difference` ` ` `// of AP is an integer` ` ` `if` `((B - A) % (j - i) == 0) ` ` ` `{` ` ` ` ` `// Store the common` ` ` `// difference` ` ` `int` `D = (B - A) / (j - i);` ` ` ` ` `// Store the First Term` ` ` `// of that AP` ` ` `int` `FirstTerm = A - (i - 1) * D;` ` ` ` ` `// Store the Nth term of` ` ` `// that AP` ` ` `int` `NthTerm = FirstTerm + (N - 1) * D;` ` ` ` ` `// Check if all elements of` ` ` `// an AP are positive` ` ` `if` `(FirstTerm > 0)` ` ` `res = Math.Min(res, NthTerm);` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// Return the least` ` ` `// Nth term obtained` ` ` `return` `res;` `}` ` ` `// Driver Code` `public` `static` `void` `Main ()` `{` ` ` `int` `N = 3;` ` ` `int` `A = 1;` ` ` `int` `B = 6;` ` ` ` ` `Console.Write(smallestNth(A, B, N));` `}` `}` `// This code is contributed by code_hunt` |

*chevron_right*

*filter_none*

**Output:**

11

**Time Complexity: **O(N^{2})**Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Program for N-th term of Arithmetic Progression series
- Arithmetic Progression containing X and Y with least possible first term
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Program for N-th term of Geometric Progression series
- Longest Arithmetic Progression | DP-35
- Find the missing number in Arithmetic Progression
- Check whether Arithmetic Progression can be formed from the given array
- Count of AP (Arithmetic Progression) Subsequences in an array
- Minimum De-arrangements present in array of AP (Arithmetic Progression)
- Program to print Arithmetic Progression series
- PHP program to print an arithmetic progression series using inbuilt functions
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Longest arithmetic progression with the given common difference
- Convert given array to Arithmetic Progression by adding an element
- Arithmetic Progression

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.