Whats the price Isaac has to pay for HackerPhone ? Problem statement here .
Difficulty Level
Easy
Required Knowledge
Basic Knowledge of Probability
Time Complexity
O(N) for each test case
Approach
Short Answer
Calculate summation of numbers on all the balls and call it X. X/2 will be the answer.
Long Answer
Let us consider number written on a particular step and call it p. Probability the he will pick p is 1/2, and It is an independent event , So It will contribute X*1/2 to final expected sum. We will do it for all number written on each and every step. So, final output will be sum of all elements divided by 2.
In other way, Let us consider number written on a particular step and call it p. There will exactly 2^N-1 set which will contain p and there will be exactly same number of sets which will not contain p. So, number p will contribute p*(2^(N-1)) to total sum.
Number of subsets = 2^N
Sum of all element of all subsets = (sum of elements of every step) * 2^(N-1) // as we did for p
So expected sum = (sum of element written on each step) / 2
Setter’s Code :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int T,N,i;
cin>>T;
while(T--)
{
cin>>N;
long long int s = 1;
for(i=1 ; i<=N ; i++)
{
if(i%2 == 0)
s++;
else
s = 2*s;
}
cout<<s<<endl;
}
return 0;
}
Tester’s Code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N;
long long int x,sum=0;
cin>>N;
for(int i = 0 ; i < N ; i++)
{
cin>>x;
sum = sum + x;
}
if(sum%2==0)
{
cout<<sum/2<<".0"<<endl;
return 0;
}
else
{
cout<<sum/2<<".5"<<endl;
return 0;
}
}