STICKS - Editorial

C code
Can any one say me what is wrong with the code…
its working fine in code blocks…but am getting wrong answer warning while submitting.
could anyone please explain??
here is the link
https://www.codechef.com/submit/complete/13211715

Iam getting WA.But it is working fine in code blocks
https://www.codechef.com/viewsolution/13211715
could anyone explain pls

for (int i = 0; i < t; i++) {
br.readLine();
toks = br.readLine().trim().split(" ");
int[] A = new int[toks.length];
for(int j=0; j<toks.length; j++){
A[j] = Integer.parseInt(toks[j]);
}
Arrays.sort(A);
int found = 0;
int ans = 1;
for(int x=A.length-1; x>0; x–){
if(A[x] == A[x-1]){
ans = ans * A[x];
found = found + 2;
x–;
}
if(found == 4){
break;
}
}
System.out.println(found == 4 ? ans : -1);

Hello, I’m trying to understand why this solution is returning a wrong answer.

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

Any feedback would be greatly appreciated :slight_smile:

One thing to note is the question is to find the maximum area of one rectangle. I was taking the maximum possible area of all the rectangles (i.e summing all the areas ).
for example the test case :
8
1 1 1 1 1 1 1 1
output should be 1 not 2 :slight_smile:

This is my implementation. How is it?
#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;

while(t--)
{
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }

    sort(a,a+n);
    int ans=-1;
    int p=1,cnt=0;
    for(int i=n-1;i>=0;i--)
    {
        if(cnt==2)
        {
            break;
        }

        if(a[i]==a[i-1])
        {
            p=p*a[i];
            i--;
            cnt++;
        }
    }

    if(cnt==2)
    {
        cout<<p<<endl;
    }
    else
        cout<<ans<<endl;
}
return 0;

}

@admin , a stupid question, but is making squares allowed with any left over pair of sticks?

2 Likes

Can anyone tell which test case is failed for my code or why I’m getting Wrong Answer
https://www.codechef.com/viewsolution/22156698

Try this test:
1
4
1 2 3 3
Your solution output 0, but must be -1

BTW, change for(i=0;i<10002;i++) to for(i=0;i<10001;i++), your arrays size only 10001

You forget about square:
1

4

1 1 1 1

Your solution output -1, but must be 1.

1 Like

You forget about square case too

can u elaborate more ,which case i have omitted

1

4

1 1 1 1

ok,I got it …thank U

1

8

1 1 2 2 2 2 3 3

Answer is 6 = 2 * 3, but your output will be 2 * 2 = 4

@mgch Thanks :slight_smile:

1 Like

Can any one please help me I am getting wrong answer on the following python code:

for _ in range(int(input())):
    n = int(input())
    a = sorted(list(map(int,input().split())))
    l,b = [],[]
    for i in range(n-1,-1,-1):
        if a[i] not in l:
            l.append(a[i])
        elif a[i] in l and a[i] not in b:
            b.append(a[i])
            if len(b)==2:
                break
    if len(b)==2:
        print(b[0]*b[1])
    else:
        print(-1)

try this case
1
4
1 2 2 2

your output: 4
actual output : -1

Whats wrong in my code?

cook your dish here

from collections import deque
t=int(input())
def rect(l):
l1=[]
max_area=[]
q=deque(l1)
mul=1
counter=0
k=0
for i in range(0,len(l),1):
if l[i] not in q:
q.append(l[i])
else:
counter+=1
q.remove(l[i])
if counter==2:
k=1
if counter<=2:
mul=int(mul)*int(l[i])
else:
max_area.append(mul)
mul=1
counter=1
mul=int(mul)*int(l[i])

max_area.append(mul)
if k==0:
    return -1
else:
    return max(max_area)

while t>0:
n=int(input())
l=list(map(int,input().split()))
area=rect(l)
print(area)
t-=1

ink to question https://www.codechef.com/problems/STICKS
here is my code .
I have put comment lines which indicated the purpose
and and since am a beginner here my Time limit is a lot but i will optimise it more but here am getting WA which i am unable to understand.
link to solution.
[CodeChef: Practical coding for everyone ]
i tried the cases in these replies and output was correct