Problem. bags of coins; one bag contains only fakes, each 1 gram lighter. With one weighing on a scale that shows the weight, find the fake bag.

Solution: take coins from bag () and weigh them together; the deficit from the expected weight is the index of the fake bag.

int fakeBag(int n, int good, function<long long(vector<int>)> weigh) {   // fake coins weigh good - 1; one weighing
    vector<int> take(n); for (int i = 0; i < n; i++) take[i] = i + 1;         // i + 1 coins from bag i
    long long expected = (long long)good * n * (n + 1) / 2;
    return (int)(expected - weigh(take)) - 1;                                 // deficit = index + 1
}

Related: Counterfeit Coin.