AND-Not passing all test cases

I could pass the first sub-task only.Can someone please tell me why I am unable to pass all the sub-tasks?
Following is the link to my code: http://www.codechef.com/viewsolution/3946284

Thanks a lot in advance.

1 Like

your program is O (N^2) . For the 3rd and 4th sub tasks , It will be nearly O (10^10) which is causing time limit exceeded . Try optimizing your code . For the 2nd sub task , the data type of the sum should be long long int .

Here is the code for subtask 2nd .

#include <iostream>
using namespace std;
 
int main() {
int i,n,j;
long long int sum=0;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<(n-1);i++)
{
for(j=i+1;j<n;j++)
sum=sum+(a[i]&a[j]);
}
printf("%lld",sum);
return 0;
}