My problem is here https://www.codechef.com/problems/STICKS My solution this to code is here htcode
Please help me out.
Your code fails for this testcase
1
3
5 5 5
answer is -1 but your code prints 25
Mistake in your code is if 3 same values are there then you are taking 1st,2nd values as one pair and 2nd,3rd values as another pair.
you are doing it in O(n*n).
its eazy to do in O(N).
firstly create an array of 10^3 and assign 0 to all.
then while taking input , just do a a[input-1]++.(this stores the count of that number ).
then sort your array . then start transversing from the back, if you find a[i]=4 then it means a square can be formed , and break out , you get your area , else if (a[i]==2) that means you get your first longest side, then do a i-1, so that it does not again reach there , then again if you find any a[i]=2 break out and you get your max area, if you donot find it means there are not .
not able to understand. feel free to ask
it can also be done in O(n*n) as n<=10^3
no i just gave an insight of doing it in O(n).
anytime
bro just add a 2 lines in your code-
1 . when ever you find a pair just do i++; (for not counting a length two times)
2 . break the for loop when (flag==2)
Tada
do comment if ur stuck…