生成数组,使得当 j 是 i 的倍数时 max 被最小化并且 arr[i] != arr[j]
给定一个整数N ,任务是生成一个具有N个正整数的数组arr[] ,如果j可被i整除(考虑基于 1 的索引),则arr[i] ≠ arr[j]使得最大值在所有可能的序列中,该序列是最小的。
例子:
Input: N = 3
Output: 1 2 2
Explanation: In this sequence 2 is divisible by 1. arr[2] = 2, arr[1] = 1 and arr[2] ≠ arr[1].
In this sequence 3 is divisible by 1. arr[3] = 2, arr[1] = 1 and arr[3] ≠ arr[1].
The maximum value is 2 which is the minimum possible among all sequences.
Input: N = 1
Output: 1
方法:这个问题可以基于以下思路来解决:
For each index i, the value of arr[i] must be at least same as the number of divisors it has (including the number itself).
Instead of directly finding divisors for each number, one can also find multiples (say j) of each index i. and increment count of divisors of j.
请按照下面给出的插图更好地理解。
Say N = 3.
Create arr[] = {0, 0, 0} to store number of divisors of each value.
For i = 1:
=> Multiples of 1 are 1, 2 and 3.
=> Increment values of arr[1], arr[2] and arr[3].
=> So arr[] = {1, 1, 1}
For i = 1:
=> Multiples of 2 is 2.
=> Increment value of arr[2].
=> So arr[] = {1, 2, 1}
For i = 3:
=> Multiples of 3 is 3.
=> Increment value of arr[3].
=> So arr[] = {1, 2, 2}
So the final array is arr = {1, 2, 2} where the maximum value (i.e. 2) is minimum among all possible sequences.
按照下面提到的步骤来实现上述观察:
- 创建一个N大小的数组(比如arr[] ),所有元素最初都填充为 0。
- 使用埃拉托色尼筛法找出从i = 1 到 N的所有数字的所有倍数。
- 对于从i = 1 到 N的遍历:
- 运行从j = i 到 N的循环,并在每一步中将j增加i :
- 将arr[j]的值增加 1。
- 运行从j = i 到 N的循环,并在每一步中将j增加i :
- 返回最终数组。
下面是上述方法的实现。
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to generate the array
vector goodSequence(int N)
{
vector res(N);
res[0] = 1;
// Loop to implement
// sieve of Eratosthenes
for (int i = 1; i * i <= N; i++) {
for (int j = 2 * i; j <= N; j += i)
res[j - 1] = res[i - 1] + 1;
}
return res;
}
// Driver code
int main()
{
int N = 3;
// Function call
vector ans = goodSequence(N);
for (int x : ans)
cout << x << " ";
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
class GFG {
// Function to generate the array
static int[] goodSequence(int N)
{
int[] res = new int[N];
res[0] = 1;
// Loop to implement
// sieve of Eratosthenes
for (int i = 1; i * i <= N; i++) {
for (int j = 2 * i; j <= N; j += i)
res[j - 1] = res[i - 1] + 1;
}
return res;
}
// Driver code
public static void main (String[] args) {
int N = 3;
// Function call
int ans[] = goodSequence(N);
for (int x = 0; x < ans.length; x++)
System.out.print(ans[x] + " ");
}
}
// This code is contributed by hrithikgarg03188.
Python3
# python3 code to implement the approach
import math
# Function to generate the array
def goodSequence(N):
res = [0 for _ in range(N)]
res[0] = 1
# Loop to implement
# sieve of Eratosthenes
for i in range(1, int(math.sqrt(N)) + 1):
for j in range(2*i, N+1, i):
res[j - 1] = res[i - 1] + 1
return res
# Driver code
if __name__ == "__main__":
N = 3
# Function call
ans = goodSequence(N)
for x in ans:
print(x, end=" ")
# This code is contributed by rakeshsahni
C#
// C# code to implement the approach
using System;
class GFG {
// Function to generate the array
static int[] goodSequence(int N)
{
int[] res = new int[N];
res[0] = 1;
// Loop to implement
// sieve of Eratosthenes
for (int i = 1; i * i <= N; i++) {
for (int j = 2 * i; j <= N; j += i)
res[j - 1] = res[i - 1] + 1;
}
return res;
}
// Driver code
public static void Main()
{
int N = 3;
// Function call
int[] ans = goodSequence(N);
for (int x = 0; x < ans.Length; x++)
Console.Write(ans[x] + " ");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
1 2 2
时间复杂度: O(N*log(log(N)))
辅助空间: O(N)