#K50522. Combined Problem: Median of Two Sorted Arrays and Spiral Matrix Traversal
Combined Problem: Median of Two Sorted Arrays and Spiral Matrix Traversal
Combined Problem: Median of Two Sorted Arrays and Spiral Matrix Traversal
This problem consists of two subtasks:
Subtask 1: Median of Two Sorted Arrays
You are given two sorted arrays nums1 and nums2 in non-decreasing order. Your task is to find the median of the two arrays. The overall run time complexity should be \(O(\log(m+n))\). For an array of total length \(N\), if \(N\) is odd, the median is the \((N+1)/2\)-th smallest element; if \(N\) is even, the median is the average of the \(N/2\)-th and \((N/2)+1\)-th smallest elements.
Subtask 2: Spiral Order of a Matrix
You are given a 2D matrix of integers with \(r\) rows and \(c\) columns. Your task is to print the elements of the matrix in spiral order (i.e. starting at the top-left corner and moving right, then down, then left, and then up, and so on until all elements are visited).
The input will specify which subtask to execute. Read the first integer from input; if it is 1 then solve the median problem, if it is 2 then solve the spiral order problem.
inputFormat
The first line of input contains an integer indicating the problem type:
- 1: Median of Two Sorted Arrays
- 2: Spiral Order of a Matrix
If the problem type is 1, the input format is as follows:
1 m num1_1 num1_2 ... num1_m n num2_1 num2_2 ... num2_n
If the problem type is 2, the input format is as follows:
2 r c row1_element1 row1_element2 ... row1_elementc row2_element1 row2_element2 ... row2_elementc ... rowr_element1 rowr_element2 ... rowr_elementc
outputFormat
If the problem type is 1, output a single line containing the median (as a floating-point number).
If the problem type is 2, output a single line containing the matrix elements in spiral order. The elements should be separated by a single space.
## sample1
2
1 3
1
2
2.0
</p>