Skip to content

Practice Valid Arrangement of Pairs

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2097
LeetCode Problem

Valid Arrangement of Pairs

Problem Statement

You are given a 0-indexed 2D integer array pairs where pairs[i] = [starti, endi]. An arrangement of pairs is valid if for every index i where 1 <= i < pairs.length, we have endi-1 == starti. Return any valid arrangement of pairs. Note: The inputs will be generated such that there exists a valid arrangement of pairs.

Source: Valid Arrangement of Pairs on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
pairs = [[5,1],[4,5],[11,9],[9,4]]
Output
[[11,9],[9,4],[4,5],[5,1]]

# Example 2

Input
pairs = [[1,3],[3,2],[2,1]]
Output
[[1,3],[3,2],[2,1]]

# Example 3

Input
pairs = [[1,2],[1,3],[2,1]]
Output
[[1,2],[2,1],[1,3]]

Constraints

1 <= pairs.length <= 10^5
pairs[i].length == 2
0 <= starti, endi <= 10^9
starti != endi
No two pairs are exactly the same.
There exists a valid arrangement of pairs.

Related LeetCode Problems

Practice more problems similar to Valid Arrangement of Pairs.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.