Printing Binary Arrays

Was a fairly interesting one, although didn't take me much time to think about the logic but I liked the basic idea that was used in this problem cause it wasn't very obvious to me at first, cause I was firstly thinking of doing something with the elem that is contributing to a score and also of swapping the 2 variables, but then dropped these idea due to the counter cases that came in my mind for them but eventually this final logic came to me after some minutes of racking my brain.

After going through a 1600 level problem, this one was like a piece of cake, but the main fun is in those

higher level probs.

Well, take a look into the problem statement and see how your mind goes through this.

Good Luck, 

Problem Statement:-

Consider a binary array =1,2,, of length . Being a binary array means that every  is either 0 or 1.
() is defined as the number of indices  (where 1<) such that +1.

You are given a binary array =1,2,, of length . Print a binary array  of length  such that

  • , and
  • ()=().

That is, print a binary array  which is not identical to , but has the same Score as that of .

It can be proven that such a  always exists.

Input Format

  • The first line of input contains a single integer , denoting the number of test cases.
  • Each test case consists of two lines of input:
    • The first line of each test case contains a single integer  — the length of the binary array .
    • The second line of each test case contains  space-separated integers 1,2,, representing the binary array .

Output Format

For each test case, print  space-separated integers 1,2,, satisfying all the given conditions.

If there are multiple solutions, you can print any of them.

Constraints

  • 1105
  • 1105
  • 01
  • The sum of  over all test cases won't exceed 105.

Sample 1:

Input
Output
2
1
0
3
1 1 0
1
0 1 1

Explanation:

Testcase 1: The given array is =[0]()=0, since there are no indices such that +1. Therefore, we need to output another array of length 1, whose Score is also 0. =[1] is the only such array, and we output it.

Testcase 2: The given array is =[1,1,0]()=1, since 23. And it is the only such index. Therefore, we need to output another array of length 3, whose Score is also 1. =[0,1,1] is one such array, and we output it. Note that there are other such arrays as well.

Explanation:-

Just reverse each elem, that means, if it is 0 make it 1 else make it 0 if it was 1. Because the logic is that the score will always be the same as we are just having 1 and 0 in the array. If instead of 1 and 0 there would have been alphabets or special characters then this question would have been a little bit tricky but not like this. I think that in that case, we would have had to traverse to a valid Character, like for example ['a','b','a','b','a'], the score is 4. Now here the resultant array can be, ['c','b','c','b','c'], it's like I'll change the every 2nd char starting from 1st into a vaild char( that is different from the char in front of it).

Code Implementation:-

for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().split()))
    arr=[1 if(bit==0) else 0 for bit in arr]
    print(*arr)

Comments

Popular Posts