Having trouble understanding a portion of code

179 Views Asked by At

I have this problem:

You have n problems. You have estimated the difficulty of the i-th one as integer ci. Now you want to prepare a problemset for a contest, using some of the problems you've made.

A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be at least l and at most r. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at least x.

Find the number of ways to choose a problemset for the contest.

Input The first line contains four integers n, l, r, x (1 ≤ n ≤ 15, 1 ≤ l ≤ r ≤ 109, 1 ≤ x ≤ 106) — the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.

The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 106) — the difficulty of each problem.

Output Print the number of ways to choose a suitable problemset for the contest.

I tried to solve it but unfortunately I couldn't do it. I asked a friend to give me an idea and he solved it for me but i don't understand something:

Code:

#include <stdio.h>
int a[25], l, r, x, i, j, n, ans;
int main(){
    scanf("%d %d %d %d", &n, &l, &r, &x);
    for(i=0; i<n; i++) scanf("%d", &a[i]);
    for(i=0; i<(1<<n); i++){
        int s = 0;
        int max = 0, min = 1e9;
        for(j=0; j<n; j++){
            if((i>>j)&1){
                if(a[j] > max) max = a[j];
                if(min > a[j]) min = a[j];
                s += a[j];
            }
        }
        if(l <= s && s <= r && max-min >= x) ans++;
    }
    printf("%d", ans);
    return 0;
}
  1. Why is he going through that array till i<(1<<n) if he only got n elements?

  2. Why he does this: if((i>>j)&1) ?

I know that 1<<n is the equivalent of multiplying by a power of two and 1>>n is equivalent to integer division by 2^n but it makes no sense here.

1

There are 1 best solutions below

3
On BEST ANSWER

You have n possible problems, and each one can either be included or excluded from the problem set. This means that there are 2 options for every problem, so with n problems there are 2^n options for creating possible problem sets.

With the line for(i=0; i<(1<<n); i++) you are iterating all these possible problems sets, each one identified by an integer between 0 and 2^n - 1. Next, we need to identify which problems belong to a certain problem set, and we have the integer which represents it.

To do that, we take the binary representation of that integer. It will have n bits, and lets say that each bit corresponds to a problem: if it is 1 then the problem is included, otherwise it is not. This is what the line if((i>>j)&1) is doing: it checks if the bit in position j inside the integer i is equal to 1, which means that the corresponding problem is included.

The rest is easier: from all the included problems, you calculate the minimum, maximum, and sum. Then, check if the sum s is in the valid range, and if the difference between the maximum and minimum is at least x.