Connection Status:
Competition Arena > Haar1D
SRM 275 · 2005-11-30 · by NeverMore · Recursion, Simulation
Class Name: Haar1D
Return Type: int[]
Method Name: transform
Arg Types: (vector<int>, int)
Problem Statement

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.

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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
3
Returns: {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
3)
{1, 2, 3, 4, 4, 3, 2, 1}
3
Returns: {20, 0, -4, 4, -1, -1, 1, 1 }
4)
{1,2}
1
Returns: {3, -1 }

Submissions are judged against all 57 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class Haar1D with a public method vector<int> transform(vector<int> data, int L) · 57 test cases · 2 s / 256 MB per case

Submitting as anonymous