Help in Beautiful Array(BTAR)

I tried to implement the same approach as mentioned in the official editorial of the problem, but I wasn’t able to debug it for WA. Here is my solution.

Please provide me a TC which fails my code. I just didn’t sum the elements initially to check if it is possible to make the array beautiful, I checked it in the later part of my code.

You are complicating your life after taking input.

Just do the following.

if count is our answer

count += two/2;

two = two%2; // Handles numbers with remainder 2.

int min = min(one, three)

one -= min;

three -= min; // after this, either variable one, or variable three will be zero.

count += min;

int rem = max(one,three);

if(two == 1){


count += 2;


rem -= 2;

}else beau = false;

} //Handle merging of 2 with two 1s or two 3s, in two steps.

count += (rem/4)*3; //merges four 1s or four 3s in 3 steps.

if(rem%4 != 0)beau = false;

if(beau)print count;

else print -1;

1 Like

Your solution is too complicated bro!!!

Using too many if and else statements.

I know my code is cumbersome, but I can’t figure out what is wrong. I think I did what you explained, just the hard way. xD

@taran_1407 plzz check my code:

It’s just that it’s too cumbersome to find bug in a cumbersome code.

Hope you understand this cumbersome problem.

I would recommend to simplify and feel free to ask if any doubt @vatsalsura

1 Like

u all can use random test cases with some AC solution

In the else part where three > one, you are first setting one=0 and then subtracting it from three ie three -= one,so the value of three never changes. That is what I fell is wrong. You should subtracting one from three and then set one to 0


@omkar__halikar Thank you. Indeed that was the only problem in my code, got ac after inter-changing both the lines.