#C8369. Two Sum - Find Indices that Sum to Target
Two Sum - Find Indices that Sum to Target
Two Sum - Find Indices that Sum to Target
Given an array of integers and a target integer, your task is to find two distinct indices i and j such that the sum of the elements at these indices equals the target:
\( nums[i] + nums[j] = target \)
If there are multiple valid pairs, you may return any one of them. If no such pair exists, output -1 -1.
The indices should be returned based on their appearance order in the input array (0-indexed).
inputFormat
The input is given via standard input (stdin) with the following format:
- The first line contains two integers n and target, where n is the number of elements in the array.
- The second line contains n space-separated integers representing the array elements.
outputFormat
Output two space-separated integers: the indices i and j such that nums[i] + nums[j] = target.
If no valid pair exists, output -1 -1
.
4 9
2 7 11 15
0 1