STICKS - Editorial

A different implementation of the above logic.

https://www.codechef.com/viewsolution/10616757

First enter all the length of the sticks in an array, sort it in O(nlogn) and then traverse the array from the right end, checking for multiple instances of any stick. This also covers the special case of a square.

hey,can anyone just help me to find error in my code…
https://www.codechef.com/viewsolution/10623972

A different approach,which has O(N*logN) time complexity but uses Only One Array is as follows:

After storing the array containing the lengths of sticks, sort it.
We must note that the same lengths will automatically come together after sorting.
For example,

the array 5 2 8 3 1 2 4 3 5

becomes 1 2 2 3 3 4 5 5 8

Then if we traverse the array from backwards picking up the first two occurrences of a pair of same numbers(for e.g (5,5) would be the first occurrence and (3,3) would be the second in this case.Don’t forget to break the loop here. :stuck_out_tongue: )
the answer would be 5*3=15.If we are unable to find two occurrences we print -1.

The JAVA solution of the problem using this method is:

CodeChef: Practical coding for everyone.

4 Likes

@code_blooded_ I did same in C++.
https://www.codechef.com/viewsolution/10622129

1 Like

CodeChef: Practical coding for everyone Please let me know what is wrong with this solution.

@sreedishps if N<3 , then you are not reading sticks length

https://www.codechef.com/viewsolution/10627623 Hey, Can anybody tell why I am getting WA ? I used the algorithm mentioned in editorial.

CodeChef: Practical coding for everyone Can somebody please look into it,I am getting a WA.

Can anybody tell why I am getting WA ? link-CodeChef: Practical coding for everyone

Hi,

This is my code
https://www.codechef.com/viewsolution/10630891

Can anyone please help me to tell why is this WA ?

Thanks

y am i getting WA in this solution? CodeChef: Practical coding for everyone

https://www.codechef.com/viewsolution/10633288

can someone help me with my code…? its coming as “time limit exceeded”…

Can anyone help with the code. I have included the square case too. But I don’t know why my solution is not getting accepted. CodeChef: Practical coding for everyone

@pada1211
Your code fails for
1 4 1 2 3 3

CodeChef: Practical coding for everyone I cant seem to figure out whats wrong with my solution. It passes most of the test cases I can come up with.
Please help out. Thanks in advance :slight_smile:
PS: I looked through some solutions… They use the size of count array as 10000 whereas the mentioned size is 10^3 = 1000. Why is that so?
I tried both ways by the way. Still wrong answer.

@sreedishps
This case gives Runtime error with your code :
1
7
4 4 3 3 3 3 1

@sebastinsanty,

check the case

1

7

2 3 1 1 2 1 1

answer should be 2

https://www.codechef.com/viewsolution/10644839
Can someone explain why am i getting WA

Can somebody please tell me at what test i got wrong answer
https://www.codechef.com/viewsolution/10647330

Please can anyone point out the error in my code- CodeChef: Practical coding for everyone