SUMARRAY - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: notsoloud
Tester: mridulahi
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

Given N (which is even) and K, find an array A of length N such that:

  • Half the elements of A are even and the other half are odd.
  • The sum of the elements of A is K.
  • 1 \leq A_i\leq 10^5

EXPLANATION:

We want \frac{N}{2} elements each to be odd and even; and they should all be positive.
So, the smallest array we can create is A = [1, 1, 1, 1, \ldots, 1, 2, 2,2, \ldots, 2] containing \frac{N}{2} each of ones and twos.

If the sum of this array is \gt K, then we certainly can’t have a sum of K; so the answer is -1.
Now, let’s see how we can modify this array to obtain K as the sum.

The current sum of the array is \frac{N}{2} + 2\cdot\frac{N}{2} = \frac{3N}{2}.
That means we still need to add in K - \frac{3N}{2} more.
Let Y = K - \frac{3N}{2}.

To maintain the parity condition, we can only increase each element by an even number.
In particular, this means that if Y is odd, we can’t make a sum of K since we need to add an odd number to the sum which is impossible while also maintaining the parity condition; so the answer would be -1.

This leaves us with the case when Y is even. Note also the constraint 1 \leq A_i \leq 10^5 that we need to satisfy.

  • Iterate i from 1 to N.
  • At index i, figure out how much to add to A_i: this is the minimum of Y and 99998 (since we start with either 1 or 2, and can’t exceed 10^5 anywhere).
    Add this value to A_i and subtract it from Y.

In the end, if Y = 0 we’re done, and the resulting array can be printed; otherwise no answer exists and we print -1.

TIME COMPLEXITY

\mathcal{O}(N) per testcase.

CODE:

Editorialist's code (C++)
#include <bits/stdc++.h>
using namespace std;

int main() {
	int t; cin >> t;
	while (t--) {
	    int n, k; cin >> n >> k;
	    vector<int> a(n, 1);
	    for (int i = 0; i < n; ++i) {
	        if (i >= n/2) ++a[i];
	        k -= a[i];
	    }
	    if (k < 0 or k%2 == 1) cout << -1 << '\n';
	    else {
	        for (int i = 0; i < n; ++i) {
	            int take = min(k, 99998);
	            k -= take;
	            a[i] += take;
	        }
	        if (k > 0) cout << -1 << '\n';
	        else {
	            for (int i = 0; i < n; ++i) cout << a[i] << ' ';
	            cout << '\n';
	        }
	    }
	}
	return 0;
}
Editorialist's code (Python)
for _ in range(int(input())):
    n, k = map(int, input().split())
    if 3*n//2 > k: print(-1)
    elif k%2 != (3*n//2)%2: print(-1)
    else:
        a = [1, 2] * (n//2)
        rem = k - 3*n//2
        for i in range(n):
            have = (100000 - a[i]) // 2
            take = min(have, rem // 2)
            rem -= 2*take
            a[i] += 2*take
        if rem > 0: print(-1)
        else: print(*a)
2 Likes


During the competition I happened to get this problem incorrect but when I debugged my code when it ended I found that this was deemed incorrect for some reason even though it is a true answer. Is there something I have missed?

2 Likes


same here.

3 Likes

Same testcase and same issue @the_gangleader

Why to do this

Instead of just

for (int i = 0; i < n; i++) {
                if (i < n / 2)
                    a[i] = 1;
                else
                    a[i] = 2;
            }

            k -= (3 * n / 2);

            if (k % 2 == 1) {
                System.out.println(-1);
                continue;
            }

            a[n - 1] = k+2;

            printArray(a);
  • I am just adding the n/2 one’s and n/2 two’s in our output array and the sum of these elements will be 3N/2.

  • Now if remaining k (after k -= 3N/2) is odd then return -1 otherwise place remaining k+2 to the some of the 2’s place.

@iceknight1093 Why it will not work?

Same Issue… Wrong Test Case

Note the condition 1 \leq A_i \leq 10^5.
If you give everything to a single index, you can exceed 10^5 (since K can be as large as 10^9).
That’s why we try to distribute the values to all indices.

1 Like

yes ,same problem

same for mee too

Having problems with the following code

[Solution: 1023145908 - CodeChef] link

Getting issues with the test case:

N = 2, K = 5

My output: 3 2

which is think I totally correct but the Codechef IDE shows wrong answer
I tweaked the code to a little different logic, giving the same output for the same test case but this time it gets accepted

tweaked code:

[Solution: 1023146199 - CodeChef] link

I think if this is an error on the Codechef part, the contest should be unrated, my whole time went into resolving the same issue :frowning:


This contest should be unrated. Wasted so much time fixing this error, changing the logic as well but still the same WA due to error from server side.

I thought of this case!! and i did distributed values in my submission ID: 1023157250
Even though it’s Giving Wrong Verdict

Although this is correct, in the editorial shown above they do not even do that, they simply give the minimum of Y and 99998 to the first then go from there to each value so 4 1, 2 3, 3 2, 1 4 are all valid solutions yet still incorrect in codechef

@zeroo_bitches @harshjain9425
Ignore the “Debug my code” output for this problem. Since there are multiple answers possible, this feature will not work on this problem. Your code would be failing on some other test case.

1 Like

Oh! Okay
thank for reply @iceknight1093

WA on last Test Case for SUMARRAY problem - help - CodeChef Discuss

Please check this once … !!

I think it was a pretty problem that gives a lesson to read constraints carefully lol I got 2 WA cause I never read A[i]<=1e5, but eventually it was very doable.

1 Like