I have this question from a past contest that i participated in. I just can’t figure out what corner cases am I missing out on as two of the test cases ain’t giving correct answers. Plz help.
Problem link: Programming Problems and Competitions :: HackerRank
My Code: Programming Problems and Competitions :: HackerRank
Thanks
Method 1: Using sorting
for _ in range(input()):
n = input();i=1;temp=1;ans=0
a=map(int,raw_input().split())
a.sort()
while(i<n):
if(a[i]==a[i-1]):
temp+=1
i+=1
else:
ans+=temp*(temp-1)
i+=1
temp=1
ans+=temp*(temp-1)
print ans
Method 2: Counting frequency
for _ in range(input()):
a=[0]1000005
n=input()
ans=0
b=map(int,raw_input().split())
for i in range(n):
a[b[i]]+=1
for i in range(1000005):
ans+=a[i](a[i]-1)
print ans
I used above two methods and got accepted in all test cases for both. I could not access your code but hope above two python codes will help you.
Link to test cases that I am getting wrong:
Input: https://hr-testcases-us-east-1.s3.amazonaws.com/15172/input04.txt?AWSAccessKeyId=AKIAJAMR4KJHHUS76CYQ&Expires=1449964738&Signature=WAV%2F%2FHvuN4vaS76Q6ZZ1p8O1VNk%3D&response-content-type=text%2Fplain
Output: https://hr-testcases-us-east-1.s3.amazonaws.com/15172/output04.txt?AWSAccessKeyId=AKIAJAMR4KJHHUS76CYQ&Expires=1449964963&Signature=Crn0N%2BPDlJzvMctQyyWs8B0pcao%3D&response-content-type=text%2Fplain
Plzz help someone!
Thanks.
Can you review my code once?
int t,n,i,j,m,c,s;
vector<int>vec;
int main() {
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=0;i<n;++i){
scanf("%d",&m);
vec.push_back(m);
}
sort(vec.begin(),vec.end());
s=0;
for(i=0;i<n;++i){
m=vec[i];
c=1;
for(j=i+1;(vec[j]<=m)&&(j<n);j++){
c++;
}
s+=c*(c-1);
i+=c-1;
}
printf("%d\n",s);
vec.clear();
}
return 0;
}</pre>