Initial and Final Accounts in a Transfer Chain
Problem statement
You are given directed account-transfer pairs [from, to] in arbitrary order. Together they form one non-branching chain containing every pair exactly once.
Return [initialAccount, finalAccount], where the initial account has no incoming transfer and the final account has no outgoing transfer.
Function
transferEndpoints(transfers: int[][]) → int[]Examples
Example 1
transfers = [[222,111],[111,333],[444,222]]return = [444,333]The complete chain is 444 to 222 to 111 to 333.
Example 2
transfers = [[1,2]]return = [1,2]A one-transfer chain exposes both endpoints directly.
Example 3
transfers = [[3,4],[1,3],[4,8]]return = [1,8]Input order does not affect the recovered endpoints.
Constraints
1 ≤ transfers.length ≤ 100000.- Every transfer contains two distinct integer account IDs.
- The pairs form exactly one acyclic chain with no branches.