# Generate an array with K positive numbers such that arr[i] is either -1 or 1 and sum of the array is positive

• Difficulty Level : Basic
• Last Updated : 01 Jul, 2022

Given two positive integers, N and K ( 1 ≤ K ≤ N ), the task is to construct an array arr[](1-based indexing) such that each array element arr[i] is either i or -i and the array contains exactly K positive values such that sum of the array elements is positive. If more than one such sequence can be generated, print any of them. Otherwise, print “-1”.

Examples:

Input: N = 10, K = 6
Output: -1 2 -3 4 -5 6 -7 8 9 10
Explanation:
Consider the sequence as {-1, 2, -3, 4, -5, 6, -7, 8, 9, 10}, the sum of the constructed sequence is (-1) + 2 + (-3) + 4 + (-5) + 6 + (-7) + 8 + 9 + 10 = 23, which is positive.

Input: N = 7, K = 2
Output: -1

Approach: The given problem can be solved by using the Greedy Approach by making the first (N – K) elements in the sequence negative and the remaining K elements positive. Follow the steps below to solve the problem:

• Initialize an array say, arr[] that stores the resultant sequence.
• Initialize two variables, say sumNeg and sumPos as 0 to store the sum of the first (N – K) and the remaining elements respectively.
• Iterate over the range [0, N – K – 1] using the variable i and update the value of arr[i] to -(i + 1) and add the value arr[i] to the variable sumNeg.
• Iterate over the range [N – K, N – 1] using the variable i and update the value of arr[i] to (i + 1) and add the value arr[i] to the variable sumPos.
• If the value of the absolute value of sumNeg is greater than sumPos, then print -1. Otherwise, print the sum elements in the array arr[] as the result.

Below is the implementation of the above approach:

## 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)` `{` `    ``// Initialize 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;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.Arrays;`   `class` `GFG{` `    `  `// Function to generate the resultant` `// sequence of first N natural numbers` `static` `void` `findSequence(``int` `n, ``int` `k)` `{` `    `  `    ``// Initialize 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) ` `    ``{` `        ``System.out.print(-``1``);` `        ``return``;` `    ``}`   `    ``// Print the required sequence` `    ``for``(``int` `i = ``0``; i < n; i++)` `        ``System.out.print(arr[i] + ``" "``);` `}`   `// Driver code` `public` `static` `void` `main(String args[])` `{` `    ``int` `N = ``10``, K = ``6``;` `    `  `    ``findSequence(N, K);` `}` `}`   `// This code is contributed by sanjoy_62`

## Python3

 `# Python program for the above approach` `# Function to generate the resultant` `# sequence of first N natural numbers` `def` `findSequence(n, k):` `    `  `    ``# Initialize an array of size N` `    ``arr ``=` `[``0``]``*``n` `    `  `    ``# Stores the sum of positive and` `    ``# negative elements` `    ``sumPos ``=` `0` `    ``sumNeg ``=` `0` `    `  `    ``# Mark the first N - K elements` `    ``# as negative` `    ``for` `i ``in` `range``(``0``, n ``-` `k):` `      `  `        ``# 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` `i ``in` `range``(n ``-` `k, n): ` `        `  `        ``# 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):` `        ``print``(``-``1``)` `        ``return` `    `  `    ``# Print the required sequence` `    ``for` `i ``in` `range``(n):` `        ``print``( arr[i], end ``=``" "``)`   `# Driver Code` `N ``=` `10` `K ``=` `6` `findSequence(N, K)`   `# This code is contributed by shivanisinghss2110`

## 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)` `    ``{` `        ``// Initialize 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.`

## Javascript

 ``

Output:

`-1 -2 -3 -4 5 6 7 8 9 10`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Recommended Articles
Page :