构造一个包含所有不同元素的 K 个子数组的数组
给定整数N和K ,任务是使用 [1, N] 范围内的数字构造一个大小为N的数组arr[] ,使得它具有K个子数组,其所有元素都是不同的。
注意:如果有多个可能的答案,请返回其中任何一个。
例子:
Input: N = 5, K = 8
Output: {1, 2, 3, 3, 3}
Explanation: This array has the distinct sub-arrays as {1}, {2}, {3}, {3}, {3}, {1, 2}, {2, 3}, {1, 2, 3}
Input: N = 6, K = 21
Output: {1, 2, 3, 4, 5, 6}
Explanation: This array has the 21 distinct sub-arrays.
方法:解决问题的想法基于以下数学观察:
- N elements will definitely form N subarrays of size 1 having unique elements.
- Now the remaining task is to form array such that (N-K subarrays) of size more than 1 have all distinct elements.
- Also it is known number of subarrays of size more than 1 from X elements are (X*(X+1))/2 – X = X*(X-1)/2.
- If X elements are distinct all these subarrays have all distinct elements.
So to form the array there is a need for such X distinct elements such that X*(X-1)/2 = K-N.
So in each step, add a distinct element until the above condition is satisfied. After that repeat the last element till the array size becomes N (because if the last element is repeated it will not effect count of subarrays with all distinct elements).
请按照以下步骤解决问题:
- 将 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)