📌  相关文章
📜  生成满足给定条件的大小为 K 的数组

📅  最后修改于: 2021-10-25 06:24:30             🧑  作者: Mango

给定两个整数NK ,任务是生成一个长度为K的数组arr[] ,使得:

  1. arr[0] + arr[1] + … + arr[K – 1] = N
  2. arr[i] > 0对于0 ≤ i < K
  3. arr[i] < arr[i + 1] ≤ 2 * arr[i]0 ≤ i < K – 1

如果有多个答案,请找到其中任何一个,否则,打印-1

例子:

方法:r = n – k * (k + 1) / 2 。如果r < 0那么答案已经是-1 。否则,让我们构造数组arr[] ,其中所有arr[i]都是floor(r / k) ,除了最右边的r % k值,它们是ceil(r / k)
不难看出,这个数组的总和为r ,按非递减顺序排序,最大元素与最小元素之差不大于 1。
让我们添加1 arr[1],2 arr[2],等等(这是我们一开始从n个减)。
然后,如果r != k – 1k = 1arr[]是我们需要的数组。否则,我们会得到一些类型为1, 3, ….., arr[k] 的数组。对于k = 2k = 3 ,这种情况没有答案。否则,我们可以从arr[2] 中减去1并将其添加到arr[k] 中,这个答案将是正确的。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to generate and print
// the required array
void generateArray(int n, int k)
{
 
    // Initializing the array
    vector array(k, 0);
 
    // Finding r (from above approach)
    int remaining = n - int(k * (k + 1) / 2);
 
    // If r<0
    if (remaining < 0)
        cout << ("NO");
 
    int right_most = remaining % k;
 
    // Finding ceiling and floor values
    int high = ceil(remaining / (k * 1.0));
    int low = floor(remaining / (k * 1.0));
 
    // Fill the array with ceiling values
    for (int i = k - right_most; i < k; i++)
        array[i]= high;
 
    // Fill the array with floor values
    for (int i = 0; i < (k - right_most); i++)
        array[i]= low;
 
    // Add 1, 2, 3, ... with corresponding values
    for (int i = 0; i < k; i++)
        array[i] += i + 1;
 
    if (k - 1 != remaining or k == 1)
    {
        for(int u:array) cout << u << " ";
    }
     
    // There is no solution for below cases
    else if (k == 2 or k == 3)
        printf("-1\n");
    else
    {
 
        // Modify A[1] and A[k-1] to get
        // the required array
        array[1] -= 1;
        array[k - 1] += 1;
        for(int u:array) cout << u << " ";
    }
}
 
// Driver Code
int main()
{
    int n = 26, k = 6;
    generateArray(n, k);
    return 0;
}
 
// This code is contributed
// by Mohit Kumar


Java
// Java implementation of the approach
class GFG
{
 
// Function to generate and print
// the required array
static void generateArray(int n, int k)
{
 
    // Initializing the array
    int []array = new int[k];
 
    // Finding r (from above approach)
    int remaining = n - (k * (k + 1) / 2);
 
    // If r < 0
    if (remaining < 0)
        System.out.print("NO");
 
    int right_most = remaining % k;
 
    // Finding ceiling and floor values
    int high = (int) Math.ceil(remaining / (k * 1.0));
    int low = (int) Math.floor(remaining / (k * 1.0));
 
    // Fill the array with ceiling values
    for (int i = k - right_most; i < k; i++)
        array[i] = high;
 
    // Fill the array with floor values
    for (int i = 0; i < (k - right_most); i++)
        array[i] = low;
 
    // Add 1, 2, 3, ... with corresponding values
    for (int i = 0; i < k; i++)
        array[i] += i + 1;
 
    if (k - 1 != remaining || k == 1)
    {
        for(int u:array)
            System.out.print(u + " ");
    }
     
    // There is no solution for below cases
    else if (k == 2 || k == 3)
        System.out.printf("-1\n");
    else
    {
 
        // Modify A[1] and A[k-1] to get
        // the required array
        array[1] -= 1;
        array[k - 1] += 1;
        for(int u:array)
            System.out.print(u + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 26, k = 6;
    generateArray(n, k);
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
import sys
from math import floor, ceil
 
# Function to generate and print
# the required array
def generateArray(n, k):
 
    # Initializing the array
    array = [0] * k
     
    # Finding r (from above approach)
    remaining = n-int(k*(k + 1)/2)
 
    # If r<0
    if remaining<0:
        print("NO")
        sys.exit()
 
    right_most = remaining % k
 
    # Finding ceiling and floor values
    high = ceil(remaining / k)
    low = floor(remaining / k)
 
    # Fill the array with ceiling values
    for i in range(k-right_most, k):
        array[i]= high
 
    # Fill the array with floor values
    for i in range(k-right_most):
        array[i]= low
 
    # Add 1, 2, 3, ... with corresponding values
    for i in range(k):
        array[i]+= i + 1
 
    if k-1 != remaining or k == 1:
        print(*array)
        sys.exit()
 
    # There is no solution for below cases
    elif k == 2 or k == 3:
        print("-1")
        sys.exit()
    else:
 
        # Modify A[1] and A[k-1] to get
        # the required array
        array[1]-= 1
        array[k-1]+= 1
        print(*array)
        sys.exit()
 
# Driver Code
if __name__=="__main__":
    n, k = 26, 6
    generateArray(n, k)


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to generate and print
// the required array
static void generateArray(int n, int k)
{
 
    // Initializing the array
    int []array = new int[k];
 
    // Finding r (from above approach)
    int remaining = n - (k * (k + 1) / 2);
 
    // If r < 0
    if (remaining < 0)
        Console.Write("NO");
 
    int right_most = remaining % k;
 
    // Finding ceiling and floor values
    int high = (int) Math.Ceiling(remaining /
                                 (k * 1.0));
    int low = (int) Math.Floor(remaining /
                              (k * 1.0));
 
    // Fill the array with ceiling values
    for (int i = k - right_most; i < k; i++)
        array[i] = high;
 
    // Fill the array with floor values
    for (int i = 0;
             i < (k - right_most); i++)
        array[i] = low;
 
    // Add 1, 2, 3, ... with
    // corresponding values
    for (int i = 0; i < k; i++)
        array[i] += i + 1;
 
    if (k - 1 != remaining || k == 1)
    {
        foreach(int u in array)
            Console.Write(u + " ");
    }
     
    // There is no solution for below cases
    else if (k == 2 || k == 3)
        Console.Write("-1\n");
    else
    {
 
        // Modify A[1] and A[k-1] to get
        // the required array
        array[1] -= 1;
        array[k - 1] += 1;
        foreach(int u in array)
            Console.Write(u + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 26, k = 6;
    generateArray(n, k);
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
1 2 4 5 6 8

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程