← 返回 walmartlabs 的题目列表Two Sum Variant: Lexicographically Smallest Pair in an Unsorted Array
类型:online_judge
Problem: Two Sum Variant (Return the Lexicographically Smallest Index Pair)
Given an unsorted integer array nums and an integer target, find two elements nums[i] and nums[j] at different indices such that:
nums[i] + nums[j] == target
Return the index pair (i, j).
Index selection rule
If multiple solutions exist, return the lexicographically smallest index pair:
Prefer the solution with smaller i.
If i is the same, prefer the one with smaller j.
Constraints & Requirements
The input array is unsorted.
i and j must be different indices.
Duplicates are allowed.
If no solution exists, output -1 -1.
I/O format (stdin/stdout)
Read from stdin:
Line 1: integer n (array length)
Line 2: n integers (the array nums)
Line 3: integer target
Write to stdout:
One line with two integers: i j (or -1 -1 if no solution)
Scale
1 <= n <= 2 * 10^5
-10^9 <= nums[k], target <= 10^9
Test cases
input:
4
2 7 11 15
9
output:
0 1
input:
5
3 3 3 3 3
6
output:
0 1
input:
6
1 5 1 5 1 5
6
output:
0 1
input:
5
1 2 3 4 5
10
output:
-1 -1
input:
7
0 -1 2 -3 1 4 -2
1
output:
0 4
Example
Input
4
2 7 11 15
9
Output
0 1