I am stuck on trying to understand how to solve the following question. Could someone please explain for a beginner to average-case analysis?
Considering the following algorithm A which takes as input a bit string b = b_1, b_2, ... , b_n (each b_i is either 0 or 1) and does the following:
A(b,n)
k = 0
FOR i = 1 TO n DO
IF b_i = 1 THEN
k = k + 1
j = k mod 2
IF j = 1 THEN
FOR i = 1 TO k DO
PRINT("Hello World")
Assuming each string is bit string b is equally likely, what is the average number of "Hello Worlds" that A will print?
The hint I have so far is I need to express the sum directly on the sample space, where $X$ below represents the number of times "Hello World" is printed, and depends on individual inputs. $$ E(X) = \sum_{L \in S_n} X(L) \cdot Pr(L) $$