Write code to sort list L1 and reorder list L2 so that each element in L2 stays paired with its corresponding element in L1 after sorting.
1Times asked
Jun 2026Last seen
Jun 2026First seen
💡 Model Answer
You can zip the two lists together, sort by the first element, and then unzip. Here’s a concise Python solution:
python
L1 = (1, 0, 6, 3, 8, 4)
L2 = (9, 2, 4, 1, 6, 7)
# Pair elements
paired = list(zip(L1, L2))
# Sort by the first element of each pair
paired.sort(key=lambda x: x[0])
# Unzip back into two lists
sorted_L1, sorted_L2 = zip(*paired)
print(list(sorted_L1)) # [0, 1, 3, 4, 6, 8]
print(list(sorted_L2)) # [2, 9, 1, 7, 4, 6]Complexity: Sorting dominates, so O(n log n) time and O(n) extra space for the paired list. This approach keeps the pairing intact and works for any comparable type in L1. If you need to preserve the original tuples, you can return them as a list of tuples instead of separate lists.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500