Avoiding Zero Problem solution Codeforces Global Round 11-
Problem statement-
A. Avoiding Zero
You are given an array of
You have to create an array of
- The array
b is a rearrangement of the arraya , that is, it contains the same values and each value appears the same number of times in the two arrays. In other words, the multisets{a1,a2,…,an} and{b1,b2,…,bn} are equal.For example, if
a=[1,−1,0,1] , thenb=[−1,1,1,0] andb=[0,1,−1,1] are rearrangements ofa , butb=[1,−1,−1,0] andb=[1,0,2,−3] are not rearrangements ofa . - For all
k=1,2,…,n the sum of the firstk elements ofb is nonzero. Formally, for allk=1,2,…,n , it must holdb1+b2+⋯+bk≠0.
If an array
Each test contains multiple test cases. The first line contains an integer
The first line of each testcase contains one integer
The second line of each testcase contains
For each testcase, if there is not an array
Otherwise print a line with the word YES, followed by a line with the
If there is more than one array
4 4 1 -2 3 -4 3 0 0 0 5 1 -1 1 -1 1 6 40 -31 -9 0 13 -40
YES 1 -2 3 -4 NO YES 1 1 -1 1 -1 YES -40 13 40 0 -9 -31
Explanation of the first testcase: An array with the desired properties is
- The first element of
b is1 . - The sum of the first two elements of
b is−1 . - The sum of the first three elements of
b is2 . - The sum of the first four elements of
b is−2 .
Explanation of the second testcase: Since all values in
Explanation of the third testcase: An array with the desired properties is
- The first element of
b is1 . - The sum of the first two elements of
b is2 . - The sum of the first three elements of
b is1 . - The sum of the first four elements of
b is2 . - The sum of the first five elements of
b is1 .
Explanation of the fourth testcase: An array with the desired properties is
- The first element of
b is−40 . - The sum of the first two elements of
b is−27 . - The sum of the first three elements of
b is13 . - The sum of the first four elements of
b is13 . - The sum of the first five elements of
b is4 . - The sum of the first six elements of
b is−27 .
Solution-
- Get link
- Other Apps
Labels
c++ codeforces programming- Get link
- Other Apps
Comments
Post a Comment