Problem: AND

Help!

This program is able to complete only two subtask 1 & 2 while Shows TLE(Time Limit Exceeded) for subtask 3 & 4, where i am doing mistake??

#include<stdio.h>
int main()

{

long long n,i,j,res=0;

scanf("%lld",&n);

long long *A=new long long[n];

for(i=0;i<n;i++)

	scanf("%lld",&A[i]);

for(i=0;i<n;i++)

{

	for(j=i+1;j<n;j++)

	{

		res+=(A[i]&A[j]);

	}

}

printf("\n%lld",res);

delete[] A;

return 0;

}

Please post the link to the problem.

TLE IS Time limit exceeded not time limit executed :stuck_out_tongue: Means your algorithm is slow. You have used an O(10^5 * 10^5) algorithm in the worst case. Will give you a tle for sure.

try to use nested for loops as follows:
for(i=n-4;i<n;i++)
for(j=i+1;j<n;j++)
with statements you are using already.It will help reduce the time complexity and hence the TLE.

Buddy if the solution was so straight forward and if brute force was suffice for the program then the problem would not have been written. It won’t be so simple

Moreover O(n^2) also won’t work here because n is too big g try another approach there is a sweet and simple observation that would help you to do the program.

Moreover you can see the editorial of the program too.
Hope this helps.
Happy Coding.