📌  相关文章
📜  构造一个包含所有不同元素的 K 个子数组的数组

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

构造一个包含所有不同元素的 K 个子数组的数组

给定整数NK ,任务是使用 [1, N] 范围内的数字构造一个大小为N的数组arr[] ,使得它具有K个子数组,其所有元素都是不同的。

注意:如果有多个可能的答案,请返回其中任何一个。

例子:

方法:解决问题的想法基于以下数学观察:

请按照以下步骤解决问题:

  • 将 K 递减K – N ,因为每个整数都构成一个大小为 1 的不同子数组。
  • 现在初始化一个变量num = 0以跟踪添加到数组中的整数以及形成的子数组的数量。
  • 从 K 开始,减少将 (num + 1) 添加到新数组后形成的不同子数组的数量。 (直到 num <= K )。
  • 检查数组大小是否达到N。如果没有 添加num - K重复次数,直到数组填满。

以下是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to construct the array
// with k distinct subarrays
vector construct_array(int n, int k)
{
 
    // Each invidividual integers
    // contributes to one subarray
    k = k - n;
 
    // Initialize a variable to keep
    // track of integers that are
    // adding to to the array
    int num = 0;
 
    // Initialize the res to
    // store the array
    vector res;
 
    // Push as many possible distinct
    // integers into the vector
    while (k >= num) {
        res.push_back(num + 1);
 
        // Decerement the count of
        // distinct subarrays incrementing
        k -= num;
        num++;
    }
    // If still it is not possible to
    // get the array of size n then
    // adding n-k at the end make num-k
    // more distinct subarrays
    while (res.size() < n) {
        res.push_back(num - k);
    }
 
    // Return the array
    return res;
}
 
// Driver code
int main()
{
    int N = 5;
    int K = 8;
 
    // Function call
    vector ans = construct_array(N, K);
    for (int x : ans)
        cout << x << " ";
    return 0;
}


Java
// JAVA program for the above approach
import java.util.*;
class GFG {
 
  // Function to construct the array
  // with k distinct subarrays
  public static ArrayList construct_array(int n,
                                                   int k)
  {
 
    // Each invidividual integers
    // contributes to one subarray
    k = k - n;
 
    // Initialize a variable to keep
    // track of integers that are
    // adding to to the array
    int num = 0;
 
    // Initialize the res to
    // store the array
    ArrayList res = new ArrayList();
 
    // Push as many possible distinct
    // integers into the vector
    while (k >= num) {
      res.add(num + 1);
 
      // Decerement the count of
      // distinct subarrays incrementing
      k -= num;
      num++;
    }
    // If still it is not possible to
    // get the array of size n then
    // adding n-k at the end make num-k
    // more distinct subarrays
    while (res.size() < n) {
      res.add(num - k);
    }
 
    // Return the array
    return res;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 5;
    int K = 8;
 
    // Function call
    ArrayList ans = construct_array(N, K);
    for (int x = 0; x < ans.size(); x++)
      System.out.print(ans.get(x) + " ");
  }
}
 
// This code is contributed by Taranpreet


C#
// C# program for the above approach
using System;
using System.Collections;
 
public class GFG{
 
  // Function to construct the array
  // with k distinct subarrays
  public static ArrayList construct_array(int n,
                                          int k)
  {
 
    // Each invidividual integers
    // contributes to one subarray
    k = k - n;
 
    // Initialize a variable to keep
    // track of integers that are
    // adding to to the array
    int num = 0;
 
    // Initialize the res to
    // store the array
    ArrayList res = new ArrayList();
 
    // Push as many possible distinct
    // integers into the vector
    while (k >= num) {
      res.Add(num + 1);
 
      // Decerement the count of
      // distinct subarrays incrementing
      k -= num;
      num++;
    }
    // If still it is not possible to
    // get the array of size n then
    // adding n-k at the end make num-k
    // more distinct subarrays
    while (res.Count < n) {
      res.Add(num - k);
    }
 
    // Return the array
    return res;
  }
 
  // Driver code
  static public void Main (){
 
    int N = 5;
    int K = 8;
 
    // Function call
    ArrayList ans = construct_array(N, K);
    foreach(int x in ans)
      Console.Write(x + " ");
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript


输出
1 2 3 3 3 

时间复杂度: O(N)
辅助空间: O(N)