生成包含给定数字 A 和 B 的相邻元素差相等的 N 大小数组
给定两个自然数A和B (B >= A) 和一个整数N ,您的任务是以非递减顺序生成自然数数组,使得 A 和 B 都必须是数组的一部分,并且两者之间的差每对相邻元素都是相同的,保持数组的最大元素尽可能小。
例子:
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
方法:所需的阵列形成一个 AP 系列。由于 A 和 B 必须包含在 AP 中,因此相邻项之间的公差d必须是 (B – A) 的除数。为了最小化最大项,最佳方法是生成满足给定条件的最小可能共同差的数组。该问题可以通过以下步骤解决:
- 迭代从1到BA的所有d值。
- 如果d是BA的一个因数,并且从A到B具有公差d的元素个数不超过N ,则继续进行。否则,移动到d的下一个值。
- 有两种可能的情况
- 情况 1 – 小于或等于B且具有公差d的元素的数量大于N。在这种情况下,数组的第一个元素将是B – (d*(N-1)) 。
- 情况 2 – 小于或等于B且具有公差d的元素的数量小于N 。在这种情况下,数组的第一个元素将是B – d*( B-1 )/d (即 1)。
- 使用第一个元素和共同点打印数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the array of N
// natural numbers in increasing order
// with equal difference of adjacent
// elements and containing A and B
void generateAP(int A, int B, int N)
{
// maximum possible difference
int d = B - A;
int cd, f;
// Iterating over all values of d
for (int i = 1; i <= d; i++) {
// Check if i is a factor of d
// and number of elements from
// a to b with common difference
// d are not more than N
if (d % i == 0 && (d / i) + 1 <= N) {
// Number off natural numbers
// less than B and having
// difference as i
int cnt = min((B - 1) / i, N - 1);
// Calculate the 1st element of
// the required array
f = B - (cnt * i);
cd = i;
break;
}
}
// Print the resulting array
for (int i = 0; i < N; i++) {
cout << (f + i * cd) << " ";
}
}
// Driver code
int main()
{
int A = 10, B = 20, N = 4;
// Function call
generateAP(A, B, N);
return 0;
}
Java
// Java program for the above approach
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)
{
// maximum possible difference
int d = B - A;
int cd = 0, f= 0;
// Iterating over all values of d
for (int i = 1; i <= d; i++) {
// Check if i is a factor of d
// and number of elements from
// a to b with common difference
// d are not more than N
if (d % i == 0 && (d / i) + 1 <= N) {
// Number off natural numbers
// less than B and having
// difference as i
int cnt = min((B - 1) / i, N - 1);
// Calculate the 1st element of
// the required array
f = B - (cnt * i);
cd = i;
break;
}
}
// Print the resulting array
for (int i = 0; i < N; i++) {
System.out.print((f + i * cd) + " ");
}
}
// Driver code
public static void main(String[] args)
{
int A = 10, B = 20, N = 4;
// Function call
generateAP(A, B, N);
}
}
// This code is contributed by maddler.
Python3
# Python program for the above approach
# Function to print array of N
# natural numbers in increasing order
# with equal difference of adjacent
# elements and containing A and B
def generateAP(A, B, N):
# maximum possible difference
d = B - A
# Iterating over all values of d
for i in range(1,d+1):
# Check if i is a factor of d
# and number of elements from
# a to b with common difference
# d are not more than N
if (d % i == 0 and (d // i) + 1 <= N):
# Number off natural numbers
# less than B and having
# difference as i
cnt = min((B - 1) // i, N - 1)
# Calculate the 1st element of
# the required array
f = B - (cnt * i)
cd = i
break
# Print resulting array
for i in range(N):
print(f + i * cd , end=" ")
# Driver code
A = 10
B = 20
N = 4
# Function call
generateAP(A, B, N)
# This code is contributed by shivanisinghss2110
C#
// C# program for the above approach
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)
{
// maximum possible difference
int d = B - A;
int cd = 0, f= 0;
// Iterating over all values of d
for (int i = 1; i <= d; i++) {
// Check if i is a factor of d
// and number of elements from
// a to b with common difference
// d are not more than N
if (d % i == 0 && (d / i) + 1 <= N) {
// Number off natural numbers
// less than B and having
// difference as i
int cnt = min((B - 1) / i, N - 1);
// Calculate the 1st element of
// the required array
f = B - (cnt * i);
cd = i;
break;
}
}
// Print the resulting array
for (int i = 0; i < N; i++) {
Console.Write((f + i * cd) + " ");
}
}
// Driver Code
public static void Main()
{
int A = 10, B = 20, N = 4;
// Function call
generateAP(A, B, N);
}
}
// This code is contributed by code_hunt.
Javascript
输出
5 10 15 20
时间复杂度: O(N + √B)
辅助空间: O(1)