📌  相关文章
📜  生成一个具有 K 个正数的数组,这样 arr[i] 要么是 -i 要么是 i 并且数组的总和是正数

📅  最后修改于: 2021-09-07 02:10:47             🧑  作者: Mango

给定两个正整数NK ( 1 ≤ K ≤ N ),任务是构造一个数组arr[] (基于 1 的索引),这样每个数组元素arr[i]要么是i要么是-i并且数组正好包含K 个正值,使得数组元素的总和为正。如果可以生成多个这样的序列,打印它们中的任何一个。否则,打印“-1”

例子:

方法:给定的问题可以通过使用贪心方法解决,方法是使序列中的前 (N – K) 个元素为负,其余K 个元素为正。请按照以下步骤解决问题:

  • 初始化一个数组,比如arr[]来存储结果序列。
  • 初始化两个变量,比如sumNegsumPos0 ,分别存储第一个(N – K)和剩余元素的总和。
  • 使用变量i迭代范围[0, N – K – 1]并将 arr[i]的值更新为-(i + 1)并将值arr[i] 添加到变量sumNeg
  • 使用变量i迭代范围[N – K, N – 1]并将arr[i]的值更新为(i + 1)并将值 arr[i] 添加到变量sumPos
  • 如果 sumNeg的绝对值大于sumPos ,则打印-1 。否则,打印数组arr[] 中的总和元素作为结果。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
  
// Function to generate the resultant
// sequence of first N natural numbers
void findSequence(int n, int k)
{
    // Initalize an array of size N
    int arr[n];
  
    // Stores the sum of positive and
    // negative elements
    int sumPos = 0, sumNeg = 0;
  
    // Mark the first N - K elements
    // as negative
    for (int i = 0; i < n - k; i++) {
  
        // Update the value of arr[i]
        arr[i] = -(i + 1);
  
        // Update the value of sumNeg
        sumNeg += arr[i];
    }
  
    // Mark the remaining K elements
    // as positive
    for (int i = n - k; i < n; i++) {
  
        // Update the value of arr[i]
        arr[i] = i + 1;
  
        // Update the value of sumPos
        sumPos += arr[i];
    }
  
    // If the sum of the sequence
    // is negative, then print -1
    if (abs(sumNeg) >= sumPos) {
        cout << -1;
        return;
    }
  
    // Print the required sequence
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
  
// Driver Code
int main()
{
    int N = 10, K = 6;
    findSequence(N, K);
  
    return 0;
}


C#
// C# program for the above approach
using System;
class GFG
{
    
    // Function to generate the resultant
    // sequence of first N natural numbers
    static void findSequence(int n, int k)
    {
        // Initalize an array of size N
        int[] arr = new int[n];
  
        // Stores the sum of positive and
        // negative elements
        int sumPos = 0, sumNeg = 0;
  
        // Mark the first N - K elements
        // as negative
        for (int i = 0; i < n - k; i++) {
  
            // Update the value of arr[i]
            arr[i] = -(i + 1);
  
            // Update the value of sumNeg
            sumNeg += arr[i];
        }
  
        // Mark the remaining K elements
        // as positive
        for (int i = n - k; i < n; i++) {
  
            // Update the value of arr[i]
            arr[i] = i + 1;
  
            // Update the value of sumPos
            sumPos += arr[i];
        }
  
        // If the sum of the sequence
        // is negative, then print -1
        if (Math.Abs(sumNeg) >= sumPos) {
            Console.Write(-1);
            return;
        }
  
        // Print the required sequence
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
  
    // Driver Code
    public static void Main()
    {
        int N = 10, K = 6;
        findSequence(N, K);
    }
}
  
// This code is contributed by ukasp.


输出:
-1 -2 -3 -4 5 6 7 8 9 10

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