Lv.1arrayhash
Two Sum
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Acceptance
- 25.0%
- Solvers
- 1
Two Sum
Given an integer array nums and an integer target, return the two distinct indices i < j such that
nums[i] + nums[j] == target, as the array [i, j].
Exactly one answer exists. You may not use the same element twice.
Constraints
-
nums.length -
nums[i] -
target
Examples
| nums | target | result |
|---|---|---|
[2, 7, 11, 15] | 9 | [0, 1] |
[3, 2, 4] | 6 | [1, 2] |
Expected time complexity: . The pairwise scan exceeds the performance time limit.
Function signature
solution(nums: int[], target: int) → int[]
Expected complexity
- Time
- O(N)
- Space
- O(N)
Examples
| nums | target | Expected |
|---|---|---|
[2,7,11,15] | 9 | [0,1] |
[3,2,4] | 6 | [1,2] |
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | derived |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1030 ms | derived |
| C# 14 (.NET 10) | 1000 ms | derived |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 2 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 4 | verdict only |