Construct an AP series consisting of A and B having minimum possible Nth term
Last Updated :
10 Apr, 2023
Given two integers A, B which are any two terms of an Arithmetic Progression series, and an integer N, the task is to construct an Arithmetic Progression series of size N such that it must include both A and B and the Nth term of the AP should be minimum.
Examples:
Input: N = 5, A = 20, B = 50
Output: 10 20 30 40 50
Explanation:
One of the possible AP sequences is {10, 20, 30, 40, 50} having 50 as the 5th value, which is the minimum possible.
Input: N = 2, A = 1, B = 49
Output: 1 49
Approach: The Nth Term of an AP is given by XN = X + (N – 1)*d, where X is the first term and d is a common difference. To make the largest element minimum, minimize both x and d. It can be observed that the value of X cannot be more than min(A, B) and the value of d cannot be more than abs(A – B).
- Now, use the same formula to construct the AP for every possible value of x (From 1 to min(A, B)) and d(From 1 to abs(A – B)).
- Now, construct the array arr[] as {x, x + d, x + 2d, …, x + d*(N – 1)}.
- Check if A and B are present in it or not and the Nth element is minimum possible or not. If found to be true, then update the ans[] by the constructed array arr[].
- Otherwise, iterate further and check for other values of x and d.
- Finally, print ans[] as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check_both_present( int arr[], int N,
int a, int b)
{
bool f1 = false , f2 = false ;
for ( int i = 0; i < N; i++) {
if (arr[i] == a) {
f1 = true ;
}
if (arr[i] == b) {
f2 = true ;
}
}
if (f1 && f2) {
return true ;
}
else {
return false ;
}
}
void print_array( int ans[], int N)
{
for ( int i = 0; i < N; i++) {
cout << ans[i] << " " ;
}
}
void build_AP( int N, int a, int b)
{
int arr[N], ans[N];
for ( int i = 0; i < N; i++)
ans[i] = INT_MAX;
int flag = 0;
if (a > b) {
swap(a, b);
}
int diff = b - a;
for ( int start = 1;
start <= a; start++) {
for ( int d = 1;
d <= diff; d++) {
arr[0] = start;
for ( int i = 1; i < N; i++) {
arr[i] = arr[i - 1] + d;
}
if (check_both_present(arr, N, a, b)
&& arr[N - 1] < ans[N - 1]) {
for ( int i = 0; i < N; i++) {
ans[i] = arr[i];
}
}
}
}
print_array(ans, N);
}
int main()
{
int N = 5, A = 20, B = 50;
build_AP(N, A, B);
return 0;
}
|
Java
import java.io.*;
class GFG{
public static boolean check_both_present( int [] arr,
int N, int a,
int b)
{
boolean f1 = false , f2 = false ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == a)
{
f1 = true ;
}
if (arr[i] == b)
{
f2 = true ;
}
}
if (f1 && f2)
{
return true ;
}
else
{
return false ;
}
}
public static void print_array( int [] ans, int N)
{
for ( int i = 0 ; i < N; i++)
{
System.out.print(ans[i] + " " );
}
}
public static void build_AP( int N, int a, int b)
{
int [] arr = new int [N];
int [] ans = new int [N];
for ( int i = 0 ; i < N; i++)
ans[i] = Integer.MAX_VALUE;
int flag = 0 ;
if (a > b)
{
a += (b - (b = a));
}
int diff = b - a;
for ( int start = 1 ; start <= a; start++)
{
for ( int d = 1 ; d <= diff; d++)
{
arr[ 0 ] = start;
for ( int i = 1 ; i < N; i++)
{
arr[i] = arr[i - 1 ] + d;
}
if (check_both_present(arr, N, a, b) &&
arr[N - 1 ] < ans[N - 1 ])
{
for ( int i = 0 ; i < N; i++)
{
ans[i] = arr[i];
}
}
}
}
print_array(ans, N);
}
public static void main(String[] args)
{
int N = 5 , A = 20 , B = 50 ;
build_AP(N, A, B);
}
}
|
Python3
import sys
def check_both_present(arr, N, a, b):
f1 = False
f2 = False
for i in range ( 0 , N):
if arr[i] = = a:
f1 = True
if arr[i] = = b:
f2 = True
if f1 and f2:
return True
else :
return False
def print_array(ans, N):
for i in range ( 0 , N):
print (ans[i], end = " " )
def build_AP(N, a, b):
INT_MAX = sys.maxsize
arr = [ None for i in range (N)]
ans = [INT_MAX for i in range (N)]
flag = 0
if a > b:
a, b = b, a
diff = b - a
for start in range ( 1 , a + 1 ):
for d in range ( 1 , diff + 1 ):
arr[ 0 ] = start
for i in range ( 1 , N):
arr[i] = arr[i - 1 ] + d
if ((check_both_present(arr, N, a, b) and
arr[N - 1 ] < ans[N - 1 ])):
for i in range ( 0 , N):
ans[i] = arr[i]
print_array(ans, N)
if __name__ = = "__main__" :
N = 5
A = 20
B = 50
build_AP(N, A, B)
|
C#
using System;
class GFG{
static bool check_both_present( int [] arr, int N,
int a, int b)
{
bool f1 = false , f2 = false ;
for ( int i = 0; i < N; i++)
{
if (arr[i] == a)
{
f1 = true ;
}
if (arr[i] == b)
{
f2 = true ;
}
}
if (f1 && f2)
{
return true ;
}
else
{
return false ;
}
}
static void print_array( int [] ans, int N)
{
for ( int i = 0; i < N; i++)
{
Console.Write(ans[i] + " " );
}
}
static void build_AP( int N, int a, int b)
{
int [] arr = new int [N];
int [] ans = new int [N];
for ( int i = 0; i < N; i++)
ans[i] = int .MaxValue;
if (a > b)
{
a += (b - (b = a));
}
int diff = b - a;
for ( int start = 1; start <= a; start++)
{
for ( int d = 1; d <= diff; d++)
{
arr[0] = start;
for ( int i = 1; i < N; i++)
{
arr[i] = arr[i - 1] + d;
}
if (check_both_present(arr, N, a, b) &&
arr[N - 1] < ans[N - 1])
{
for ( int i = 0; i < N; i++)
{
ans[i] = arr[i];
}
}
}
}
print_array(ans, N);
}
static public void Main()
{
int N = 5, A = 20, B = 50;
build_AP(N, A, B);
}
}
|
Javascript
<script>
function check_both_present(arr, N, a, b)
{
let f1 = false , f2 = false ;
for (let i = 0; i < N; i++)
{
if (arr[i] == a)
{
f1 = true ;
}
if (arr[i] == b)
{
f2 = true ;
}
}
if (f1 && f2)
{
return true ;
}
else
{
return false ;
}
}
function print_array(ans, N)
{
for (let i = 0; i < N; i++)
{
document.write(ans[i] + " " );
}
}
function build_AP(N, a, b)
{
let arr = Array(N).fill(0);
let ans = Array(N).fill(0);
for (let i = 0; i < N; i++)
ans[i] = Number.MAX_VALUE;
let flag = 0;
if (a > b)
{
a += (b - (b = a));
}
let diff = b - a;
for (let start = 1; start <= a; start++)
{
for (let d = 1; d <= diff; d++)
{
arr[0] = start;
for (let i = 1; i < N; i++)
{
arr[i] = arr[i - 1] + d;
}
if (check_both_present(arr, N, a, b) &&
arr[N - 1] < ans[N - 1])
{
for (let i = 0; i < N; i++)
{
ans[i] = arr[i];
}
}
}
}
print_array(ans, N);
}
let N = 5, A = 20, B = 50;
build_AP(N, A, B);
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...