HackathonMay 27, 2025

Alice and Bob buy crackers

Hazrat Ali

Hackathon

Alice and Bob want to buy firecrackers. There are N types of firecrackers available in the market with an ID i ranging from 1 to N. The cost of each firecracker is Ci. Each firecracker can be bought at most once. Bob's father allows him to spend all the money in any way but his only condition is that Alice and Bob receive equal amounts of money.

Your task is to determine the number of different amounts of money both can receive from Bob's father. You are given the cost of all crackers and the money according to the condition provided. 

In other words, let S be a set. For each subset of firecrackers, if the sum of the costs of these firecrackers is even, then add their sum to S and print the size of S. Note that the set does not contain equal elements.

Input format

  • First line: N denoting the types of firecrackers
  • Next line: C1,C2,…,Cn denoting the cost of the firecrackers

Output format

Print the number of different amounts of money that Bob and Alice can receive from Bob's father.

Constraints

1≤n≤201≤Ci≤1000

Sample Input
3
2 5 8
Sample Output
3


Solution

#include<bits/stdc++.h>
using namespace std;

int main()
{


    int m;
    cin >> m;
    int cost[m];
    for(int i = 0; i < m; i++)
        cin >> cost[i];
    int firecrakers = 0;
    bool temp[20000 * m] = {0};
    for(int i = 0; i < (1<<m); i++)
    {
        long long int sum = 0;
        for(int j = 0; j < m; j++)
            if(i & (1<<j))
                sum += cost[j];
        if(!(temp[sum]) && sum && !(sum & 1))
            firecrakers++;
        temp[sum] = {1};
    }
    cout << firecrakers;
    return 0;
}




Comments