Can Anyone please tell me what is wrong with this code? I have tried a lot of test cases but I am unable to find the one which is responsible for giving me WA
#include<stdio.h>
#include<stdlib.h>
long long int input()
{
long long int n = 0;
int ch;
ch = getchar_unlocked();
while(ch >= '0' && ch <= '9')
{
n = (n << 3) + (n << 1) + (ch - '0');
ch = getchar_unlocked();
}
return n;
}
int main()
{
long long int t;
t = input();
while(t--)
{
long long int n, count = 0, temp = 0, greater_2 = 0, equal_2 = 0, i;
n = input();
for(i = 0; i < n; i++)
{
temp = input();
if(temp == 2)
{
count = count + greater_2;
equal_2++;
}
else if(temp > 2)
{
count = count + greater_2 + equal_2;
greater_2++;
}
}
printf("%lld\n",count);
}
return 0;
}
@brijwasi1995 This test case is giving your problem dear.
1
3
2 2 2
Here, your code is giving output of 3 but correct output is 0. See that 2x2= 2+2 and the condition of a[I] x a[j] being STRICTLY GREATER does not hold.
That’s why you are asked to count number of element equal to 2, number of elements greater than 2 and apply Permutation and combination logic to derive the answer, which is given in formula. Ping me in case of doubt
Can anyone prove me mathematically why do we need to add (number of 2’s)(number of elements greater than 2)
to (c(c-1)/2). Hope you do prove mathematically??
Denote C0 as the number of 0s in the given array. And C1 is the number of 1s. C2 is the number of numbers which are greater than or equal to 2. As analyzed before, the answer is C2 * (C2 - 1) / 2.
I think this part of editorial is wrong. Say, array = {2, 2, 2, 2, 2}.
C1 = 0, C2 = 5. The answer is not 10 but 0.
Sorry to have mislead you. What I have done is I calculated the 1’s and 0’s in c. Then I reduced n(the total no. of elements) by c.So,now n is the total no. of elements >=2. And count is the number of 2’s. Then i applied the formula n*(n-1)/2 - count*(count-1)/2. Also, replacing by long long int didn’t work.