📌  相关文章
📜  生成数组,使得当 j 是 i 的倍数时 max 被最小化并且 arr[i] != arr[j]

📅  最后修改于: 2022-05-13 01:56:07.727000             🧑  作者: Mango

生成数组,使得当 j 是 i 的倍数时 max 被最小化并且 arr[i] != arr[j]

给定一个整数N ,任务是生成一个具有N个正整数的数组arr[] ,如果j可被i整除(考虑基于 1 的索引),则arr[i] ≠ arr[j]使得最大值在所有可能的序列中,该序列是最小的。

例子:

方法:这个问题可以基于以下思路来解决:

请按照下面给出的插图更好地理解。

按照下面提到的步骤来实现上述观察:

  • 创建一个N大小的数组(比如arr[] ),所有元素最初都填充为 0。
  • 使用埃拉托色尼筛法找出从i = 1 到 N的所有数字的所有倍数。
  • 对于从i = 1 到 N的遍历:
    • 运行从j = i 到 N的循环,并在每一步中将j增加i
      • arr[j]的值增加 1。
  • 返回最终数组。

下面是上述方法的实现。

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)