Minimal Merge Array
Medium
array
merge
sorting
two-pointers
Problem Description
Given two sorted arrays, merge them into one sorted array. Count and return the minimum number of comparisons needed during the merge process.
Input Format
First line: n m (sizes). Second line: n sorted integers. Third line: m sorted integers.
Output Format
Two lines: merged sorted array (space-separated), then number of comparisons made.
Constraints
• 1 ≤ n, m ≤ 1000
• Arrays are already sorted in ascending order
• -1000 ≤ array elements ≤ 1000
• Count each element comparison during merge
• Use optimal merge algorithm
Sample Input/Output
Input:
3 2 1 3 5 2 4
Output:
1 2 3 4 5 4
Explanation
Merge process: Compare 1<2 (1), 3>2 (2), 3<4 (3), 5>4 (4). Total comparisons: 4.
Solution Hints
90
Points
Points
2000ms
Time Limit
Time Limit
128MB
Memory Limit
Memory Limit
Code Editor
Register to Access Code Editor
Create a free account to solve coding problems and track your progress.
Register Now