TopCoder problem "Haar1D" used in SRM 275 (Division II Level Two)



Problem Statement

    The Haar wavelet transform is possibly the earliest wavelet transform, introduced by Haar in 1909. The 1-dimensional version of this transform operates on a sequence of integer data as follows: First separate the sequence into pairs of adjacent values, starting with the first and second values, then the third and fourth values, etc. Next, calculate the sums of each of these pairs, and place the sums in order into a new sequence. Then, calculate the differences of each of the pairs (subtract the second value of each pair from the first value), and append the differences in order to the end of the new sequence. The resulting sequence is a level-1 transform. Note that this requires the input sequence to have an even number of elements.



The above describes a level-1 transform. To perform a level-2 transform, we repeat the above procedure on the first half of the sequence produced by the level-1 transform. The second half of the sequence remains unchanged from the previous level. This pattern continues for higher level transforms (i.e., a level-3 transform operates with the first quarter of the sequence, and so on). Note that this is always possible when the number of elements is a power of 2.



See the examples for clarification.



Create a class Haar1D with a method transform which takes a int[] data and an int L as arguments. The output should be a int[] corresponding to the level-L Haar transform of the data.
 

Definition

    
Class:Haar1D
Method:transform
Parameters:int[], int
Returns:int[]
Method signature:int[] transform(int[] data, int L)
(be sure your method is public)
    
 

Constraints

-data will contain exactly 2, 4, 8, 16 or 32 elements.
-Each element of data will be between 0 and 100 inclusive.
-L will be between 1 and log2(# of elements in data) inclusive.
 

Examples

0)
    
{1, 2, 3, 5}
1
Returns: {3, 8, -1, -2 }
Start by forming 3=1+2, the sum of the first pair; 8=3+5, the sum of the second pair; -1=1-2, the difference of the first pair; and finally, -2=3-5, the difference of the second pair. To form the output, we create a sequence of the sums in order, and the differences in order, to obtain {3, 8, -1, -2} as the level-1 transform.
1)
    
{1, 2, 3, 5}
2
Returns: {11, -5, -1, -2 }
From the above example, the level-1 transform is given by {3, 8, -1, -2} So, the level-2 transform of {1, 2, 3, 5} is simply {11, -5, -1, -2} (11=3+8, -5=3-8).
2)
    
{1, 2, 3, 4, 4, 3, 2, 1}
3
Returns: {20, 0, -4, 4, -1, -1, 1, 1 }
3)
    
{94, 47, 46, 28, 39, 89, 75, 4, 28, 62, 69, 89, 34, 55, 81, 24}
2
Returns: {215, 207, 248, 194, 67, 49, -68, -16, 47, 18, -50, 71, -34, -20, -21, 57 }

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=5894

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=8072&pm=5894

Writer:

NeverMore

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Recursion, Simulation