Generate N-size Array with equal difference of adjacent elements containing given numbers A and B
Given two natural numbers A and B (B >= A) and an integer N, your task is to generate an array of natural numbers in non-decreasing order, such that both A and B must be part of the array and the difference between every pair of adjacent elements is the same keeping the maximum element of the array as minimum as possible.
Example:
Input: A = 10, B = 20, N = 4
Output: 5 10 15 20
Explanation: The array {5, 10, 15, 20} contains only natural numbers and both A=10 and B=20 are included in the array. The difference between every adjacent pair is 5 and the maximum element is 20 which can’t be further reduced.
Input: A = 12, B = 33, N = 2
Output: 12 33
Input: A = 7, B = 17, N = 5
Output: 2 7 12 17 22
Approach: The required array forms an AP series. Since A and B must be included in the AP, the common difference d between adjacent terms must be a divisor of (B – A). To minimize the largest term, the optimal approach is to generate the array with the smallest possible common difference that satisfies the given conditions. The problem can be solved by following below steps:
- Iterate over all values of d from 1 to B-A.
- If d is a factor of B-A and the number of elements from A to B with common difference d are not more than N, proceed further. Else, move to the next value of d.
- There are two possible cases
- Case 1 – Number of elements smaller than or equal B having common difference d is greater than N. In this case, the first element of the array will be B – (d*(N-1)).
- Case 2 – Number of elements smaller than or equal B having common difference d is smaller than N. In this case, the first element of the array will be B – d*( B-1 )/d (i.e. 1).
- Print the array using the first element and the common difference.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void generateAP( int A, int B, int N)
{
int d = B - A;
int cd, f;
for ( int i = 1; i <= d; i++) {
if (d % i == 0 && (d / i) + 1 <= N) {
int cnt = min((B - 1) / i, N - 1);
f = B - (cnt * i);
cd = i;
break ;
}
}
for ( int i = 0; i < N; i++) {
cout << (f + i * cd) << " " ;
}
}
int main()
{
int A = 10, B = 20, N = 4;
generateAP(A, B, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int min( int a , int b){
if (a < b){
return a;
}
return b;
}
public static void generateAP( int A, int B, int N)
{
int d = B - A;
int cd = 0 , f= 0 ;
for ( int i = 1 ; i <= d; i++) {
if (d % i == 0 && (d / i) + 1 <= N) {
int cnt = min((B - 1 ) / i, N - 1 );
f = B - (cnt * i);
cd = i;
break ;
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print((f + i * cd) + " " );
}
}
public static void main(String[] args)
{
int A = 10 , B = 20 , N = 4 ;
generateAP(A, B, N);
}
}
|
Python3
def generateAP(A, B, N):
d = B - A
for i in range ( 1 ,d + 1 ):
if (d % i = = 0 and (d / / i) + 1 < = N):
cnt = min ((B - 1 ) / / i, N - 1 )
f = B - (cnt * i)
cd = i
break
for i in range (N):
print (f + i * cd , end = " " )
A = 10
B = 20
N = 4
generateAP(A, B, N)
|
C#
using System;
class GFG{
public static int min( int a , int b){
if (a < b){
return a;
}
return b;
}
public static void generateAP( int A, int B, int N)
{
int d = B - A;
int cd = 0, f= 0;
for ( int i = 1; i <= d; i++) {
if (d % i == 0 && (d / i) + 1 <= N) {
int cnt = min((B - 1) / i, N - 1);
f = B - (cnt * i);
cd = i;
break ;
}
}
for ( int i = 0; i < N; i++) {
Console.Write((f + i * cd) + " " );
}
}
public static void Main()
{
int A = 10, B = 20, N = 4;
generateAP(A, B, N);
}
}
|
Javascript
<script>
function min(a, b)
{
if (a < b)
{
return a;
}
return b;
}
function generateAP(A, B, N)
{
var d = B - A;
var cd = 0, f = 0;
for ( var i = 1; i <= d; i++)
{
if (d % i == 0 && (d / i) + 1 <= N)
{
var cnt = min((B - 1) / i, N - 1);
f = B - (cnt * i);
cd = i;
break ;
}
}
for ( var i = 0; i < N; i++)
{
document.write((f + i * cd) + " " );
}
}
var A = 10, B = 20, N = 4;
generateAP(A, B, N);
</script>
|
Time Complexity: O(N + √B)
Auxiliary Space: O(1)
Last Updated :
02 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...