Minimize Nth term of an Arithmetic progression (AP)
Last Updated :
14 Apr, 2021
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 Nth term of that arithmetic progression.
Note: All the elements of an AP series must be positive.
Examples:
Input: A = 1, B = 6, N = 3
Output: 11
Explanations: First three terms of the given arithmetic progression are: {1, 6, 11}.
Therefore, the required output is 11.
Input: A = 20, B = 50, N = 5
Output: 50
Explanations: 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 Nth 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 Nth term obtained.
Follow the steps below to solve the problem:
- Initialize a variable, say res to store the smallest possible value of the required Nth term of arithmetic progression.
- Using two nested loops, place A and B at all possible positions in an AP, and calculate the Nth term using above calculations. Keep updating res to store the least value of Nth term obtain.
- Finally, print the value of res as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallestNth( int A, int B, int N)
{
int res = INT_MAX;
for ( int i = 1; i < N; i++) {
for ( int j = N; j > i; j--) {
if ((B - A) % (j - i) == 0) {
int D = (B - A) / (j - i);
int FirstTerm = A - (i - 1) * D;
int NthTerm = FirstTerm + (N - 1) * D;
if (FirstTerm > 0)
res = min(res, NthTerm);
}
}
}
return res;
}
int main()
{
int N = 3;
int A = 1;
int B = 6;
cout << smallestNth(A, B, N);
}
|
Java
import java.io.*;
class GFG{
static int smallestNth( int A, int B, int N)
{
int res = Integer.MAX_VALUE;
for ( int i = 1 ; i < N; i++)
{
for ( int j = N; j > i; j--)
{
if ((B - A) % (j - i) == 0 )
{
int D = (B - A) / (j - i);
int FirstTerm = A - (i - 1 ) * D;
int NthTerm = FirstTerm + (N - 1 ) * D;
if (FirstTerm > 0 )
res = Math.min(res, NthTerm);
}
}
}
return res;
}
public static void main (String[] args)
{
int N = 3 ;
int A = 1 ;
int B = 6 ;
System.out.print(smallestNth(A, B, N));
}
}
|
Python3
import sys
def smallestNth(A, B, N):
res = sys.maxsize
for i in range ( 1 , N):
for j in range (N, i, - 1 ):
if ((B - A) % (j - i) = = 0 ):
D = (B - A) / / (j - i)
FirstTerm = A - (i - 1 ) * D
NthTerm = FirstTerm + (N - 1 ) * D
if (FirstTerm > 0 ):
res = min (res, NthTerm)
return res
if __name__ = = '__main__' :
N = 3
A = 1
B = 6
print (smallestNth(A, B, N))
|
C#
using System;
class GFG{
static int smallestNth( int A, int B, int N)
{
int res = Int32.MaxValue;
for ( int i = 1; i < N; i++)
{
for ( int j = N; j > i; j--)
{
if ((B - A) % (j - i) == 0)
{
int D = (B - A) / (j - i);
int FirstTerm = A - (i - 1) * D;
int NthTerm = FirstTerm + (N - 1) * D;
if (FirstTerm > 0)
res = Math.Min(res, NthTerm);
}
}
}
return res;
}
public static void Main ()
{
int N = 3;
int A = 1;
int B = 6;
Console.Write(smallestNth(A, B, N));
}
}
|
Javascript
<script>
function smallestNth(A, B, N)
{
let res = Number.MAX_VALUE;
for (let i = 1; i < N; i++)
{
for (let j = N; j > i; j--)
{
if ((B - A) % (j - i) == 0)
{
let D = (B - A) / (j - i);
let FirstTerm = A - (i - 1) * D;
let NthTerm = FirstTerm + (N - 1) * D;
if (FirstTerm > 0)
res = Math.min(res, NthTerm);
}
}
}
return res;
}
let N = 3;
let A = 1;
let B = 6;
document.write(smallestNth(A, B, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...