Any testcase where my solution fails ? (INCRDECR)

This is my code
[](My Code WA)

My logic :

  • First Print No Based on the conditions
  • then i’ll be left with an array consisting of atmost 2 repetitions of each element
  • let’s say 1 2 2 3 4 5 5 6
  • say i print all the alternate elements from left
    1 2 4 5 6
  • and then i print all the alternate elements from right(or last but right depending on size of array)
    6 5 3 2

=> final sequence : 1 2 4 5 6 5 3 2

Can anyone point out where my code fails ?

1 Like

For clarity, you can maintain two arrays left and right to pick elements from sorted array arr.

      if(left[(int)left.size()-1] < arr[i]){

This way any repeating value will be in different arrays. Then to print you can print left the usual way and right in the reverse order.

      cout << left[i] << " ";
      cout << right[i] << " ";
  cout << endl;

I did’nt get you.
Did my code Fail at printing the array at last

Now that I took a closer look, are you checking the condition that when the max element occurs more than once it is not possible to find such an array? I think you’re only checking for triplets.

I checked for rep > 1 after the input loop that is for the last element
B.T.W My code passes for all the sample tests

Last element need not be the max element before sorting. Do that after sorting. For example what is your output for the following case

1 3 2 4 4 2
1 Like

Oh shit Thanks Man. Ill try submitting again and reach out to you

1 Like
even after modification still gives W.A Please Help

Did it work ?

No bro
See my above reply
inspite of changing it didnot work

@engineerman The input need not be such that all elements which are the same are given together.
Try 1 3 4 3 2 3

I am getting NO in this testcase


1 2 3 4 5 6 5 2 is this is correct??

So if i sort the array before checking that should work right ?

I guess that should work. Tell me if it still doesn’t work.

Any testcase, I don’t know where I did wrong