ADACOINS - Ada and Coins - SPOJ Solution C++

  Problem Link : ADACOINS 


👉 Hint : Use bitset and then cumulative sum array

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q,x,y;
    scanf("%d %d",&n,&q);
    int arr[n];
    bitset<100001> bt(0);
    bitset<100001> tmp(0);
    bitset<100001> tmp2(0);
    for(int i=0;i<n;i++)
        scanf("%d",&arr[i]);
    bt.set(arr[0]);
    for(int i=1;i<n;i++)
    {
        tmp=bt<<arr[i];
        tmp.set(arr[i]);
        bt=bt|tmp;
    }
    int cum[100002];
    cum[0]=bt.test(0);
    for(int i=1;i<100002;i++)
    {
        cum[i]=cum[i-1]+bt[i];
    }
    while(q--)
    {
        scanf("%d %d",&x,&y);
 
        printf("%d\n",cum[y]-cum[x-1]);
        
    }
 
} 

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!