Friday, April 11, 2014

Hackerrank Ad Infinitum - Math Programming Contest March'14 A Chocolate Fiesta solution

Hackerrank Ad Infinitum - Math Programming Contest March'14 A Chocolate Fiesta: https://www.hackerrank.com/contests/infinitum-mar14/challenges/a-chocolate-fiesta

Hackerrank Ad Infinitum - Math Programming Contest March'14 A Chocolate Fiesta editorial: https://www.hackerrank.com/blog/infinitum-mar14-a-chocolate-fiesta

Hackerrank Ad Infinitum - Math Programming Contest March'14 A Chocolate Fiesta solution: http://ideone.com/kGDyWm


#include <iostream>
#include <cstdio>
using namespace std;

int main() {
int n, a, r, b;
const int md=1e9+7;
r=1, b=0;
scanf("%d", &n);
for(int i=0; i<n; i++) {scanf("%d", &a); if(a & 1) b++;}
if(b==0) n++;
for(int i=1; i<n; i++) r<<=1, r%=md;
printf("%d", r-1);
return 0;
}

note: take pen and paper. do it yourself on the paper for very basic inputs like:

input 1:
3
2 4 6
output 1:
15

input 2:
3
1 2 3
output 2:
7

input 3:
3
1 3 5
output 3:
7

input 4:
3
1 2 4
output 4:
7

input 5:
5
2 4 6 8 10
output 5:
31
explanation 5:
empty subset, 1 element subset, 2 element subset, 3 element subset, 4 element subset, 5 element subset
1, 5, 10, 10, 5, 1
and that totals to 32, subtract empty subset: 32-1=31(answer is 31)

input 6:
5
1 2 4 6 8
output 6:
15

input 7:
5
1 2 3 4 5
output 7:
15

input 8:
5
1 2 3 4 6
output 8:
15
explanation 8:
empty subset, 1 element subset, 2 element subset, 3 element subset, 4 element subset, 5 element subset
1, 3, 4, 4, 3, 1
and that sums up to 16, subtract empty subset: 16-1=15(answer is 15)

No comments:

Post a Comment