-1

I try to solve some problems in a online website. And I've a solved a problem with simple C++ it worked well but it sometime throw a "Time limit exceeded" error. How to get rid of this?

Here is the question that I solved

There are two integers A and B. You are required to compute the bitwise AND amongst all natural numbers lying between A and B, both inclusive.

Here is my code.

 #include<iostream>
using namespace std;
int main()
{
    int t,a,b;
    long ans;
    cin>>t;
    while(t--)
    {
        cin>>a>>b;
        ans=a;
        for(int i=a+1; i<=b; i++)
        {
            ans=ans&i;
        }
        cout<<ans<<endl;
    }
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 10
    i suppose the error is "Time limit exceeded" because your code takes too long. You need to find a different way to get the answer. As usual such coding challenges don't let the brute force approach pass – 463035818_is_not_an_ai Oct 07 '20 at 16:15
  • 2
    Either buy your own computer and install Linux (e.g. [Debian](https://debian.org/)...) on it, or read a good book about algorithms, e.g. [*Introduction to Algorithms*](https://en.wikipedia.org/wiki/Introduction_to_Algorithms) – Basile Starynkevitch Oct 07 '20 at 16:15
  • do the maths first. There is a way to get the answer that does not require you to carry out all the individual steps. Just a non-sense example: To find the maximum integer between a and b you also would not write a loop to check each single integer. There is a simpler and faster way to get the result. The situation here requires just a little more thought – 463035818_is_not_an_ai Oct 07 '20 at 16:17
  • Depending on the ranges provided, it's either 0 or some power of 2. The LSB will always AND to 0, right? – sweenish Oct 07 '20 at 16:18
  • 1
    Sometimes you can use a brute force approach to analyze patterns and then rewrite to take advantage of those patterns. Most of the time if you don't already know how to solve the problem skip it and move on to the next problem. If you have time left over, go back and analyze. – user4581301 Oct 07 '20 at 16:26
  • The expression `x & (x-1)` is well-known to bit twiddlers because it has a very interesting property compared to `x` itself. – EOF Oct 07 '20 at 16:37
  • It's not always going to be 0 or a power of 2. E.g. 12 and 15 (0b1100 to 0b1111) cumulatively and to 12 (0b1100) which isn't either. – Nathan Pierson Oct 07 '20 at 16:51

1 Answers1

3

If you have two numbers, X and Y, they are represented by a finite sets of bits:

X = Bx(1), ..., Bx(n)

and

Y = By(1), ..., By(n)

The bitwise AND between the two can be computed as

X ^ Y = (Bx(1) ^ By(1)), ..., (Bx(n) ^ By(n))

A B A ^ B

0 0 0

0 1 0

1 0 0

1 1 1

We observe that:

  • all the bits can be computed separately, we have as many equations as many bits
  • in a sequence of logical statements, where AND is the operator, the result is 0 if and only if ANY of the items is 0

So, if any numbers are pair, then the last bit is 0. Otherwise, the last bit will be 1. If any number will have a 0 as the penultimate bit, then the result for that bit will be 0. Otherwise it will be 1.

As a general rule, based on the pigeonhole principle, proposed by Dirichlet, if you have enough consecutive (elements) for a given bit, then the result for that bit will be 0. For example, for the very last bit you have two variations, therefore, if you have at least two numbers in your consecutive set, then the last bit will be 0. If we take the very next bit, then you have four variations: 00, 01, 10 and 11. So, if you have at least 3 numbers in your consecutive set, then this bit is 0. For the next bit, you have 8 variations: 000, 001, 010, 011, 100, 101, 110, 111. So, if you have at least 5 numbers in your consecutive set, then this bit is 0.

Now, since we have a simple rule that determines most bits if there are many items, you will end up with a few bits that exceed in their number of variations the rule I have described above. For those bits you can check the first and the last number. If they have the same value for that bit, then that value will be the result, be it 0 or 1.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175