Rearrange Students
Learn this problemProblem statement
In your school, two lines of students are formed A and B, with each line having exactly N students. The two lines of students will face each other. The Physical Education teacher wants the heights of all students standing across from each other to be equal. To do this, students may be swapped from one line to another. Any student in one line can be swapped with any student in the other line. Each swap counts as one operation, and each operation costs the minimum height in the swap. Reordering the students within one line has no cost. Determine the minimal cost to achieve the desired result. If it is impossible, return -1.
Function
rearrangeStudents(arrA: int[], arrB: int[]) → long
Complete the function rearrangeStudents in the editor.
The function has the following parameters:
int arrA[n]: the heights of the children in line Aint arrB[n]: the heights of the children in line B
Returns
long: the minimal cost to rearrange the students or -1
Examples
Example 1
arrA = [4, 2, 2, 2]arrB = [1, 4, 1, 2]return = 1If the arrays are sorted descending, then arrA=[4, 2, 2, 2] and arrB = [4, 2, 1, 1]. Swap a 2 in arrA with a 1 in arrB for a cost of min(1, 2) = 1. This is the only swap that must occur, so the answer is 1.
Constraints
1 <= n <= 2 x 10^51 <= arrA[i], arrB[i] <= 10^9
More Tiktok problems
- Count Access Code PairsOA · Seen Jul 2026
- Count Key ChangesOA · Seen Jul 2026
- Travel Distance on ScootersOA · Seen Jul 2026
- Count Skipped Numbers After SubtractionsOA · Seen Jul 2026
- Obstacle Placement QueriesOA · Seen Jul 2026
- Repeated Grouped Digit SumOA · Seen Jul 2026
- Count Cyclic Digit PairsOA · Seen Jun 2026
- Event ID Check Completion TimesOA · Seen Jun 2026