id stringlengths 17 53 | domain stringclasses 5
values | category stringclasses 24
values | difficulty stringclasses 3
values | language stringclasses 10
values | problem stringlengths 29 149 | thinking_1 stringlengths 236 493 | solution_1 stringlengths 166 541 | thinking_2 stringlengths 302 467 | solution_2 stringlengths 184 565 | thinking_3 stringlengths 302 463 | solution_3 stringlengths 169 1.14k | metadata dict |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
two_sum_python_20_f2bf1ddf | algorithms | arrays | easy | python | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Implement using functional programming style. | Approach: Brute Force for Two Sum
Language: Python | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | def two_sum(nums, target):
"""
Brute Force: Check every pair of elements.
Time: O(N^2), Space: O(1)
"""
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] | Approach: Hash Map for Two Sum
Language: Python | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | def two_sum(nums, target):
"""
Optimized: Single-pass hash map for O(1) lookups.
Time: O(N), Space: O(N)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] | Approach: Two Pointer for Two Sum
Language: Python | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | def two_sum(nums, target):
"""
Two Pointer: Sort and use two pointers from both ends.
Time: O(N log N), Space: O(N) for indexed copy
"""
indexed = [(num, i) for i, num in enumerate(nums)]
indexed.sort()
left, right = 0, len(indexed) - 1
while left < right:
current_sum = indexed[l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_javascript_20_70b28473 | algorithms | arrays | easy | javascript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Implement using functional programming style. | Approach: Brute Force for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
St... | function twoSum(nums, target) {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}... | Approach: Hash Map for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces... | function twoSum(nums, target) {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
... | Approach: Two Pointer for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted witho... | function twoSum(nums, target) {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < right) {
const sum = ind... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_java_20_451d1cde | algorithms | arrays | easy | java | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Implement using functional programming style. | Approach: Brute Force for Two Sum
Language: Java | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | import java.util.*;
class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j... | Approach: Hash Map for Two Sum
Language: Java | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | import java.util.*;
class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums... | Approach: Two Pointer for Two Sum
Language: Java | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | import java.util.*;
class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0]... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_cpp_20_1f6e0259 | algorithms | arrays | easy | cpp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Implement using functional programming style. | Approach: Brute Force for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: T... | #include <vector>
using namespace std;
class Solution {
public:
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time c... | #include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i+... | Approach: Two Pointer for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without losi... | #include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> indexed;
for (int i = 0; i < (int)nums.s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_go_20_2a37181b | algorithms | arrays | easy | go | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Implement using functional programming style. | Approach: Brute Force for Two Sum
Language: Go | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: Th... | package main
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
func twoSum(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
... | Approach: Hash Map for Two Sum
Language: Go | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time co... | package main
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := seen[complement]; ok {
return []int{idx, i}
... | Approach: Two Pointer for Two Sum
Language: Go | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without losin... | package main
import "sort"
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
func twoSum(nums []int, target int) []int {
type pair struct { val, idx int }
indexed := make([]pair, len(nums))
for i, num := range nums {
indexed[i] = pair{num, i}
}
sort.... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_rust_20_1f88c34c | algorithms | arrays | easy | rust | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Implement using functional programming style. | Approach: Brute Force for Two Sum
Language: Rust | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.len();
for i in 0..n {
for j in (i + 1)..n {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
... | Approach: Hash Map for Two Sum
Language: Rust | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
let mut seen = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&... | Approach: Two Pointer for Two Sum
Language: Rust | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
let mut indexed: Vec<(i32, usize)> = nums.into_iter().enumerate()
.map(|(i, v)| (v, i)).collect();
indexed.sort_by_key(|&(v, _)| v);
let (mut l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_typescript_20_9ce393d5 | algorithms | arrays | easy | typescript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Implement using functional programming style. | Approach: Brute Force for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
St... | function twoSum(nums: number[], target: number): number[] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i,... | Approach: Hash Map for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces... | function twoSum(nums: number[], target: number): number[] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
... | Approach: Two Pointer for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted witho... | function twoSum(nums: number[], target: number): number[] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < righ... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_csharp_20_b249bd19 | algorithms | arrays | easy | csharp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Implement using functional programming style. | Approach: Brute Force for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | using System;
using System.Collections.Generic;
public class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | using System;
using System.Collections.Generic;
public class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)... | Approach: Two Pointer for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | using System;
using System.Linq;
public class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
var indexed = nums.Select((val, idx) => new[] { val, idx }).ToArray();
Array.Sort(indexed, (a, b)... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_ruby_20_520e1326 | algorithms | arrays | easy | ruby | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Implement using functional programming style. | Approach: Brute Force for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | def two_sum(nums, target)
# Brute Force: Check every pair of elements.
# Time: O(N^2), Space: O(1)
n = nums.length
(0...n).each do |i|
((i + 1)...n).each do |j|
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end | Approach: Hash Map for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | def two_sum(nums, target)
# Optimized: Single-pass hash map for O(1) lookups.
# Time: O(N), Space: O(N)
seen = {}
nums.each_with_index do |num, i|
complement = target - num
return [seen[complement], i] if seen.key?(complement)
seen[num] = i
end
[]
end | Approach: Two Pointer for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | def two_sum(nums, target)
# Two Pointer: Sort and use two pointers from both ends.
# Time: O(N log N), Space: O(N)
indexed = nums.map.with_index { |num, i| [num, i] }
indexed.sort_by!(&:first)
left, right = 0, indexed.length - 1
while left < right
sum_val = indexed[left][0] + indexed[right][0]
retur... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_swift_20_143ed764 | algorithms | arrays | easy | swift | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Implement using functional programming style. | Approach: Brute Force for Two Sum
Language: Swift | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4:... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.count
for i in 0..<n {
for j in (i + 1)..<n {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
... | Approach: Hash Map for Two Sum
Language: Swift | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
var seen = [Int: Int]()
for (i, num) in nums.enumerated() {
let complement = target - num
if let idx = seen[complement] {
return [idx, i]
... | Approach: Two Pointer for Two Sum
Language: Swift | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without lo... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
var indexed = nums.enumerated().map { ($1, $0) }
indexed.sort { $0.0 < $1.0 }
var left = 0, right = indexed.count - 1
while left < right {
let s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_python_21_0c26d0bb | algorithms | arrays | medium | python | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Add memoization to avoid recomputation. | Approach: Brute Force for Two Sum
Language: Python | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step... | def two_sum(nums, target):
"""
Brute Force: Check every pair of elements.
Time: O(N^2), Space: O(1)
"""
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] | Approach: Hash Map for Two Sum
Language: Python | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces t... | def two_sum(nums, target):
"""
Optimized: Single-pass hash map for O(1) lookups.
Time: O(N), Space: O(N)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] | Approach: Two Pointer for Two Sum
Language: Python | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without... | def two_sum(nums, target):
"""
Two Pointer: Sort and use two pointers from both ends.
Time: O(N log N), Space: O(N) for indexed copy
"""
indexed = [(num, i) for i, num in enumerate(nums)]
indexed.sort()
left, right = 0, len(indexed) - 1
while left < right:
current_sum = indexed[l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_javascript_21_3a63c951 | algorithms | arrays | medium | javascript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Add memoization to avoid recomputation. | Approach: Brute Force for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
... | function twoSum(nums, target) {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}... | Approach: Hash Map for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduc... | function twoSum(nums, target) {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
... | Approach: Two Pointer for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted wit... | function twoSum(nums, target) {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < right) {
const sum = ind... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_java_21_c5c87855 | algorithms | arrays | medium | java | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Add memoization to avoid recomputation. | Approach: Brute Force for Two Sum
Language: Java | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | import java.util.*;
class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j... | Approach: Hash Map for Two Sum
Language: Java | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | import java.util.*;
class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums... | Approach: Two Pointer for Two Sum
Language: Java | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | import java.util.*;
class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0]... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_cpp_21_f10e793c | algorithms | arrays | medium | cpp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Add memoization to avoid recomputation. | Approach: Brute Force for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4:... | #include <vector>
using namespace std;
class Solution {
public:
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time... | #include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i+... | Approach: Two Pointer for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without lo... | #include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> indexed;
for (int i = 0; i < (int)nums.s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_go_21_b61f853e | algorithms | arrays | medium | go | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Add memoization to avoid recomputation. | Approach: Brute Force for Two Sum
Language: Go | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | package main
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
func twoSum(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
... | Approach: Hash Map for Two Sum
Language: Go | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | package main
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := seen[complement]; ok {
return []int{idx, i}
... | Approach: Two Pointer for Two Sum
Language: Go | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | package main
import "sort"
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
func twoSum(nums []int, target int) []int {
type pair struct { val, idx int }
indexed := make([]pair, len(nums))
for i, num := range nums {
indexed[i] = pair{num, i}
}
sort.... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_rust_21_7532112b | algorithms | arrays | medium | rust | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Add memoization to avoid recomputation. | Approach: Brute Force for Two Sum
Language: Rust | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.len();
for i in 0..n {
for j in (i + 1)..n {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
... | Approach: Hash Map for Two Sum
Language: Rust | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
let mut seen = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&... | Approach: Two Pointer for Two Sum
Language: Rust | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
let mut indexed: Vec<(i32, usize)> = nums.into_iter().enumerate()
.map(|(i, v)| (v, i)).collect();
indexed.sort_by_key(|&(v, _)| v);
let (mut l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_typescript_21_a9303796 | algorithms | arrays | medium | typescript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Add memoization to avoid recomputation. | Approach: Brute Force for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
... | function twoSum(nums: number[], target: number): number[] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i,... | Approach: Hash Map for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduc... | function twoSum(nums: number[], target: number): number[] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
... | Approach: Two Pointer for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted wit... | function twoSum(nums: number[], target: number): number[] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < righ... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_csharp_21_9a2a1730 | algorithms | arrays | medium | csharp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Add memoization to avoid recomputation. | Approach: Brute Force for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step... | using System;
using System.Collections.Generic;
public class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces t... | using System;
using System.Collections.Generic;
public class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)... | Approach: Two Pointer for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without... | using System;
using System.Linq;
public class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
var indexed = nums.Select((val, idx) => new[] { val, idx }).ToArray();
Array.Sort(indexed, (a, b)... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_ruby_21_aac8aee1 | algorithms | arrays | medium | ruby | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Add memoization to avoid recomputation. | Approach: Brute Force for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | def two_sum(nums, target)
# Brute Force: Check every pair of elements.
# Time: O(N^2), Space: O(1)
n = nums.length
(0...n).each do |i|
((i + 1)...n).each do |j|
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end | Approach: Hash Map for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | def two_sum(nums, target)
# Optimized: Single-pass hash map for O(1) lookups.
# Time: O(N), Space: O(N)
seen = {}
nums.each_with_index do |num, i|
complement = target - num
return [seen[complement], i] if seen.key?(complement)
seen[num] = i
end
[]
end | Approach: Two Pointer for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | def two_sum(nums, target)
# Two Pointer: Sort and use two pointers from both ends.
# Time: O(N log N), Space: O(N)
indexed = nums.map.with_index { |num, i| [num, i] }
indexed.sort_by!(&:first)
left, right = 0, indexed.length - 1
while left < right
sum_val = indexed[left][0] + indexed[right][0]
retur... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_swift_21_77f5b1c9 | algorithms | arrays | medium | swift | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Add memoization to avoid recomputation. | Approach: Brute Force for Two Sum
Language: Swift | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step ... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.count
for i in 0..<n {
for j in (i + 1)..<n {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
... | Approach: Hash Map for Two Sum
Language: Swift | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces ti... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
var seen = [Int: Int]()
for (i, num) in nums.enumerated() {
let complement = target - num
if let idx = seen[complement] {
return [idx, i]
... | Approach: Two Pointer for Two Sum
Language: Swift | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without ... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
var indexed = nums.enumerated().map { ($1, $0) }
indexed.sort { $0.0 < $1.0 }
var left = 0, right = indexed.count - 1
while left < right {
let s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_python_22_8acdadd6 | algorithms | arrays | easy | python | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use sentinel values for boundary conditions. | Approach: Brute Force for Two Sum
Language: Python | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | def two_sum(nums, target):
"""
Brute Force: Check every pair of elements.
Time: O(N^2), Space: O(1)
"""
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] | Approach: Hash Map for Two Sum
Language: Python | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | def two_sum(nums, target):
"""
Optimized: Single-pass hash map for O(1) lookups.
Time: O(N), Space: O(N)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] | Approach: Two Pointer for Two Sum
Language: Python | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | def two_sum(nums, target):
"""
Two Pointer: Sort and use two pointers from both ends.
Time: O(N log N), Space: O(N) for indexed copy
"""
indexed = [(num, i) for i, num in enumerate(nums)]
indexed.sort()
left, right = 0, len(indexed) - 1
while left < right:
current_sum = indexed[l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_javascript_22_30bc4954 | algorithms | arrays | easy | javascript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use sentinel values for boundary conditions. | Approach: Brute Force for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
St... | function twoSum(nums, target) {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}... | Approach: Hash Map for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces... | function twoSum(nums, target) {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
... | Approach: Two Pointer for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted witho... | function twoSum(nums, target) {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < right) {
const sum = ind... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_java_22_e442a302 | algorithms | arrays | easy | java | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use sentinel values for boundary conditions. | Approach: Brute Force for Two Sum
Language: Java | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | import java.util.*;
class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j... | Approach: Hash Map for Two Sum
Language: Java | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | import java.util.*;
class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums... | Approach: Two Pointer for Two Sum
Language: Java | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | import java.util.*;
class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0]... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_cpp_22_611bf4ab | algorithms | arrays | easy | cpp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use sentinel values for boundary conditions. | Approach: Brute Force for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: T... | #include <vector>
using namespace std;
class Solution {
public:
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time c... | #include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i+... | Approach: Two Pointer for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without losi... | #include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> indexed;
for (int i = 0; i < (int)nums.s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_go_22_e82649d3 | algorithms | arrays | easy | go | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use sentinel values for boundary conditions. | Approach: Brute Force for Two Sum
Language: Go | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: Th... | package main
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
func twoSum(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
... | Approach: Hash Map for Two Sum
Language: Go | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time co... | package main
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := seen[complement]; ok {
return []int{idx, i}
... | Approach: Two Pointer for Two Sum
Language: Go | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without losin... | package main
import "sort"
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
func twoSum(nums []int, target int) []int {
type pair struct { val, idx int }
indexed := make([]pair, len(nums))
for i, num := range nums {
indexed[i] = pair{num, i}
}
sort.... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_rust_22_de707be3 | algorithms | arrays | easy | rust | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use sentinel values for boundary conditions. | Approach: Brute Force for Two Sum
Language: Rust | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.len();
for i in 0..n {
for j in (i + 1)..n {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
... | Approach: Hash Map for Two Sum
Language: Rust | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
let mut seen = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&... | Approach: Two Pointer for Two Sum
Language: Rust | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
let mut indexed: Vec<(i32, usize)> = nums.into_iter().enumerate()
.map(|(i, v)| (v, i)).collect();
indexed.sort_by_key(|&(v, _)| v);
let (mut l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_typescript_22_bbb3ed9b | algorithms | arrays | easy | typescript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use sentinel values for boundary conditions. | Approach: Brute Force for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
St... | function twoSum(nums: number[], target: number): number[] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i,... | Approach: Hash Map for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces... | function twoSum(nums: number[], target: number): number[] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
... | Approach: Two Pointer for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted witho... | function twoSum(nums: number[], target: number): number[] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < righ... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_csharp_22_b148d0d3 | algorithms | arrays | easy | csharp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use sentinel values for boundary conditions. | Approach: Brute Force for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | using System;
using System.Collections.Generic;
public class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | using System;
using System.Collections.Generic;
public class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)... | Approach: Two Pointer for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | using System;
using System.Linq;
public class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
var indexed = nums.Select((val, idx) => new[] { val, idx }).ToArray();
Array.Sort(indexed, (a, b)... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_ruby_22_d6863c47 | algorithms | arrays | easy | ruby | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use sentinel values for boundary conditions. | Approach: Brute Force for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | def two_sum(nums, target)
# Brute Force: Check every pair of elements.
# Time: O(N^2), Space: O(1)
n = nums.length
(0...n).each do |i|
((i + 1)...n).each do |j|
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end | Approach: Hash Map for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | def two_sum(nums, target)
# Optimized: Single-pass hash map for O(1) lookups.
# Time: O(N), Space: O(N)
seen = {}
nums.each_with_index do |num, i|
complement = target - num
return [seen[complement], i] if seen.key?(complement)
seen[num] = i
end
[]
end | Approach: Two Pointer for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | def two_sum(nums, target)
# Two Pointer: Sort and use two pointers from both ends.
# Time: O(N log N), Space: O(N)
indexed = nums.map.with_index { |num, i| [num, i] }
indexed.sort_by!(&:first)
left, right = 0, indexed.length - 1
while left < right
sum_val = indexed[left][0] + indexed[right][0]
retur... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_swift_22_3f2e93b3 | algorithms | arrays | easy | swift | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use sentinel values for boundary conditions. | Approach: Brute Force for Two Sum
Language: Swift | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4:... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.count
for i in 0..<n {
for j in (i + 1)..<n {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
... | Approach: Hash Map for Two Sum
Language: Swift | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
var seen = [Int: Int]()
for (i, num) in nums.enumerated() {
let complement = target - num
if let idx = seen[complement] {
return [idx, i]
... | Approach: Two Pointer for Two Sum
Language: Swift | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without lo... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
var indexed = nums.enumerated().map { ($1, $0) }
indexed.sort { $0.0 < $1.0 }
var left = 0, right = indexed.count - 1
while left < right {
let s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_python_23_191edc06 | algorithms | arrays | medium | python | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Apply divide and conquer for parallelization. | Approach: Brute Force for Two Sum
Language: Python | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step... | def two_sum(nums, target):
"""
Brute Force: Check every pair of elements.
Time: O(N^2), Space: O(1)
"""
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] | Approach: Hash Map for Two Sum
Language: Python | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces t... | def two_sum(nums, target):
"""
Optimized: Single-pass hash map for O(1) lookups.
Time: O(N), Space: O(N)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] | Approach: Two Pointer for Two Sum
Language: Python | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without... | def two_sum(nums, target):
"""
Two Pointer: Sort and use two pointers from both ends.
Time: O(N log N), Space: O(N) for indexed copy
"""
indexed = [(num, i) for i, num in enumerate(nums)]
indexed.sort()
left, right = 0, len(indexed) - 1
while left < right:
current_sum = indexed[l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_javascript_23_30d4e7c5 | algorithms | arrays | medium | javascript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Apply divide and conquer for parallelization. | Approach: Brute Force for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
... | function twoSum(nums, target) {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}... | Approach: Hash Map for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduc... | function twoSum(nums, target) {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
... | Approach: Two Pointer for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted wit... | function twoSum(nums, target) {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < right) {
const sum = ind... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_java_23_569a5087 | algorithms | arrays | medium | java | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Apply divide and conquer for parallelization. | Approach: Brute Force for Two Sum
Language: Java | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | import java.util.*;
class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j... | Approach: Hash Map for Two Sum
Language: Java | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | import java.util.*;
class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums... | Approach: Two Pointer for Two Sum
Language: Java | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | import java.util.*;
class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0]... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_cpp_23_0706ac8f | algorithms | arrays | medium | cpp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Apply divide and conquer for parallelization. | Approach: Brute Force for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4:... | #include <vector>
using namespace std;
class Solution {
public:
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time... | #include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i+... | Approach: Two Pointer for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without lo... | #include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> indexed;
for (int i = 0; i < (int)nums.s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_go_23_321a4b26 | algorithms | arrays | medium | go | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Apply divide and conquer for parallelization. | Approach: Brute Force for Two Sum
Language: Go | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | package main
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
func twoSum(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
... | Approach: Hash Map for Two Sum
Language: Go | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | package main
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := seen[complement]; ok {
return []int{idx, i}
... | Approach: Two Pointer for Two Sum
Language: Go | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | package main
import "sort"
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
func twoSum(nums []int, target int) []int {
type pair struct { val, idx int }
indexed := make([]pair, len(nums))
for i, num := range nums {
indexed[i] = pair{num, i}
}
sort.... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_rust_23_07045b16 | algorithms | arrays | medium | rust | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Apply divide and conquer for parallelization. | Approach: Brute Force for Two Sum
Language: Rust | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.len();
for i in 0..n {
for j in (i + 1)..n {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
... | Approach: Hash Map for Two Sum
Language: Rust | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
let mut seen = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&... | Approach: Two Pointer for Two Sum
Language: Rust | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
let mut indexed: Vec<(i32, usize)> = nums.into_iter().enumerate()
.map(|(i, v)| (v, i)).collect();
indexed.sort_by_key(|&(v, _)| v);
let (mut l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_typescript_23_dcf23390 | algorithms | arrays | medium | typescript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Apply divide and conquer for parallelization. | Approach: Brute Force for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
... | function twoSum(nums: number[], target: number): number[] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i,... | Approach: Hash Map for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduc... | function twoSum(nums: number[], target: number): number[] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
... | Approach: Two Pointer for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted wit... | function twoSum(nums: number[], target: number): number[] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < righ... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_csharp_23_7e04b925 | algorithms | arrays | medium | csharp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Apply divide and conquer for parallelization. | Approach: Brute Force for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step... | using System;
using System.Collections.Generic;
public class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces t... | using System;
using System.Collections.Generic;
public class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)... | Approach: Two Pointer for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without... | using System;
using System.Linq;
public class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
var indexed = nums.Select((val, idx) => new[] { val, idx }).ToArray();
Array.Sort(indexed, (a, b)... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_ruby_23_3e07a2fa | algorithms | arrays | medium | ruby | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Apply divide and conquer for parallelization. | Approach: Brute Force for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | def two_sum(nums, target)
# Brute Force: Check every pair of elements.
# Time: O(N^2), Space: O(1)
n = nums.length
(0...n).each do |i|
((i + 1)...n).each do |j|
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end | Approach: Hash Map for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | def two_sum(nums, target)
# Optimized: Single-pass hash map for O(1) lookups.
# Time: O(N), Space: O(N)
seen = {}
nums.each_with_index do |num, i|
complement = target - num
return [seen[complement], i] if seen.key?(complement)
seen[num] = i
end
[]
end | Approach: Two Pointer for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | def two_sum(nums, target)
# Two Pointer: Sort and use two pointers from both ends.
# Time: O(N log N), Space: O(N)
indexed = nums.map.with_index { |num, i| [num, i] }
indexed.sort_by!(&:first)
left, right = 0, indexed.length - 1
while left < right
sum_val = indexed[left][0] + indexed[right][0]
retur... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_swift_23_beec39ba | algorithms | arrays | medium | swift | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Apply divide and conquer for parallelization. | Approach: Brute Force for Two Sum
Language: Swift | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step ... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.count
for i in 0..<n {
for j in (i + 1)..<n {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
... | Approach: Hash Map for Two Sum
Language: Swift | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces ti... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
var seen = [Int: Int]()
for (i, num) in nums.enumerated() {
let complement = target - num
if let idx = seen[complement] {
return [idx, i]
... | Approach: Two Pointer for Two Sum
Language: Swift | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without ... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
var indexed = nums.enumerated().map { ($1, $0) }
indexed.sort { $0.0 < $1.0 }
var left = 0, right = indexed.count - 1
while left < right {
let s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_python_24_d3a76537 | algorithms | arrays | easy | python | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle circular references correctly. | Approach: Brute Force for Two Sum
Language: Python | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | def two_sum(nums, target):
"""
Brute Force: Check every pair of elements.
Time: O(N^2), Space: O(1)
"""
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] | Approach: Hash Map for Two Sum
Language: Python | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | def two_sum(nums, target):
"""
Optimized: Single-pass hash map for O(1) lookups.
Time: O(N), Space: O(N)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] | Approach: Two Pointer for Two Sum
Language: Python | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | def two_sum(nums, target):
"""
Two Pointer: Sort and use two pointers from both ends.
Time: O(N log N), Space: O(N) for indexed copy
"""
indexed = [(num, i) for i, num in enumerate(nums)]
indexed.sort()
left, right = 0, len(indexed) - 1
while left < right:
current_sum = indexed[l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_javascript_24_1d416d3c | algorithms | arrays | easy | javascript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle circular references correctly. | Approach: Brute Force for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
St... | function twoSum(nums, target) {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}... | Approach: Hash Map for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces... | function twoSum(nums, target) {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
... | Approach: Two Pointer for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted witho... | function twoSum(nums, target) {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < right) {
const sum = ind... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_java_24_88899af7 | algorithms | arrays | easy | java | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle circular references correctly. | Approach: Brute Force for Two Sum
Language: Java | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | import java.util.*;
class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j... | Approach: Hash Map for Two Sum
Language: Java | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | import java.util.*;
class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums... | Approach: Two Pointer for Two Sum
Language: Java | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | import java.util.*;
class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0]... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_cpp_24_63ffc517 | algorithms | arrays | easy | cpp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle circular references correctly. | Approach: Brute Force for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: T... | #include <vector>
using namespace std;
class Solution {
public:
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time c... | #include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i+... | Approach: Two Pointer for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without losi... | #include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> indexed;
for (int i = 0; i < (int)nums.s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_go_24_cc727ced | algorithms | arrays | easy | go | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle circular references correctly. | Approach: Brute Force for Two Sum
Language: Go | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: Th... | package main
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
func twoSum(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
... | Approach: Hash Map for Two Sum
Language: Go | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time co... | package main
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := seen[complement]; ok {
return []int{idx, i}
... | Approach: Two Pointer for Two Sum
Language: Go | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without losin... | package main
import "sort"
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
func twoSum(nums []int, target int) []int {
type pair struct { val, idx int }
indexed := make([]pair, len(nums))
for i, num := range nums {
indexed[i] = pair{num, i}
}
sort.... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_rust_24_28dd3267 | algorithms | arrays | easy | rust | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle circular references correctly. | Approach: Brute Force for Two Sum
Language: Rust | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.len();
for i in 0..n {
for j in (i + 1)..n {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
... | Approach: Hash Map for Two Sum
Language: Rust | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
let mut seen = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&... | Approach: Two Pointer for Two Sum
Language: Rust | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
let mut indexed: Vec<(i32, usize)> = nums.into_iter().enumerate()
.map(|(i, v)| (v, i)).collect();
indexed.sort_by_key(|&(v, _)| v);
let (mut l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_typescript_24_2b603f75 | algorithms | arrays | easy | typescript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle circular references correctly. | Approach: Brute Force for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
St... | function twoSum(nums: number[], target: number): number[] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i,... | Approach: Hash Map for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces... | function twoSum(nums: number[], target: number): number[] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
... | Approach: Two Pointer for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted witho... | function twoSum(nums: number[], target: number): number[] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < righ... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_csharp_24_4a0c4727 | algorithms | arrays | easy | csharp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle circular references correctly. | Approach: Brute Force for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | using System;
using System.Collections.Generic;
public class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | using System;
using System.Collections.Generic;
public class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)... | Approach: Two Pointer for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | using System;
using System.Linq;
public class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
var indexed = nums.Select((val, idx) => new[] { val, idx }).ToArray();
Array.Sort(indexed, (a, b)... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_ruby_24_891073de | algorithms | arrays | easy | ruby | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle circular references correctly. | Approach: Brute Force for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | def two_sum(nums, target)
# Brute Force: Check every pair of elements.
# Time: O(N^2), Space: O(1)
n = nums.length
(0...n).each do |i|
((i + 1)...n).each do |j|
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end | Approach: Hash Map for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | def two_sum(nums, target)
# Optimized: Single-pass hash map for O(1) lookups.
# Time: O(N), Space: O(N)
seen = {}
nums.each_with_index do |num, i|
complement = target - num
return [seen[complement], i] if seen.key?(complement)
seen[num] = i
end
[]
end | Approach: Two Pointer for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | def two_sum(nums, target)
# Two Pointer: Sort and use two pointers from both ends.
# Time: O(N log N), Space: O(N)
indexed = nums.map.with_index { |num, i| [num, i] }
indexed.sort_by!(&:first)
left, right = 0, indexed.length - 1
while left < right
sum_val = indexed[left][0] + indexed[right][0]
retur... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_swift_24_aebc63d8 | algorithms | arrays | easy | swift | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle circular references correctly. | Approach: Brute Force for Two Sum
Language: Swift | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4:... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.count
for i in 0..<n {
for j in (i + 1)..<n {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
... | Approach: Hash Map for Two Sum
Language: Swift | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
var seen = [Int: Int]()
for (i, num) in nums.enumerated() {
let complement = target - num
if let idx = seen[complement] {
return [idx, i]
... | Approach: Two Pointer for Two Sum
Language: Swift | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without lo... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
var indexed = nums.enumerated().map { ($1, $0) }
indexed.sort { $0.0 < $1.0 }
var left = 0, right = indexed.count - 1
while left < right {
let s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_python_25_d9eb06d1 | algorithms | arrays | medium | python | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle all edge cases including empty input. | Approach: Brute Force for Two Sum
Language: Python | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step... | def two_sum(nums, target):
"""
Brute Force: Check every pair of elements.
Time: O(N^2), Space: O(1)
"""
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] | Approach: Hash Map for Two Sum
Language: Python | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces t... | def two_sum(nums, target):
"""
Optimized: Single-pass hash map for O(1) lookups.
Time: O(N), Space: O(N)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] | Approach: Two Pointer for Two Sum
Language: Python | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without... | def two_sum(nums, target):
"""
Two Pointer: Sort and use two pointers from both ends.
Time: O(N log N), Space: O(N) for indexed copy
"""
indexed = [(num, i) for i, num in enumerate(nums)]
indexed.sort()
left, right = 0, len(indexed) - 1
while left < right:
current_sum = indexed[l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_javascript_25_c24c6845 | algorithms | arrays | medium | javascript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle all edge cases including empty input. | Approach: Brute Force for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
... | function twoSum(nums, target) {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}... | Approach: Hash Map for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduc... | function twoSum(nums, target) {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
... | Approach: Two Pointer for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted wit... | function twoSum(nums, target) {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < right) {
const sum = ind... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_java_25_995f586a | algorithms | arrays | medium | java | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle all edge cases including empty input. | Approach: Brute Force for Two Sum
Language: Java | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | import java.util.*;
class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j... | Approach: Hash Map for Two Sum
Language: Java | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | import java.util.*;
class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums... | Approach: Two Pointer for Two Sum
Language: Java | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | import java.util.*;
class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0]... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_cpp_25_66bc1dc7 | algorithms | arrays | medium | cpp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle all edge cases including empty input. | Approach: Brute Force for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4:... | #include <vector>
using namespace std;
class Solution {
public:
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time... | #include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i+... | Approach: Two Pointer for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without lo... | #include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> indexed;
for (int i = 0; i < (int)nums.s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_go_25_5321727a | algorithms | arrays | medium | go | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle all edge cases including empty input. | Approach: Brute Force for Two Sum
Language: Go | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | package main
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
func twoSum(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
... | Approach: Hash Map for Two Sum
Language: Go | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | package main
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := seen[complement]; ok {
return []int{idx, i}
... | Approach: Two Pointer for Two Sum
Language: Go | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | package main
import "sort"
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
func twoSum(nums []int, target int) []int {
type pair struct { val, idx int }
indexed := make([]pair, len(nums))
for i, num := range nums {
indexed[i] = pair{num, i}
}
sort.... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_rust_25_39dac8a4 | algorithms | arrays | medium | rust | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle all edge cases including empty input. | Approach: Brute Force for Two Sum
Language: Rust | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.len();
for i in 0..n {
for j in (i + 1)..n {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
... | Approach: Hash Map for Two Sum
Language: Rust | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
let mut seen = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&... | Approach: Two Pointer for Two Sum
Language: Rust | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
let mut indexed: Vec<(i32, usize)> = nums.into_iter().enumerate()
.map(|(i, v)| (v, i)).collect();
indexed.sort_by_key(|&(v, _)| v);
let (mut l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_typescript_25_f71451e6 | algorithms | arrays | medium | typescript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle all edge cases including empty input. | Approach: Brute Force for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
... | function twoSum(nums: number[], target: number): number[] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i,... | Approach: Hash Map for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduc... | function twoSum(nums: number[], target: number): number[] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
... | Approach: Two Pointer for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted wit... | function twoSum(nums: number[], target: number): number[] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < righ... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_csharp_25_1f458f3f | algorithms | arrays | medium | csharp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle all edge cases including empty input. | Approach: Brute Force for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step... | using System;
using System.Collections.Generic;
public class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces t... | using System;
using System.Collections.Generic;
public class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)... | Approach: Two Pointer for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without... | using System;
using System.Linq;
public class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
var indexed = nums.Select((val, idx) => new[] { val, idx }).ToArray();
Array.Sort(indexed, (a, b)... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_ruby_25_9258dc8a | algorithms | arrays | medium | ruby | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle all edge cases including empty input. | Approach: Brute Force for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | def two_sum(nums, target)
# Brute Force: Check every pair of elements.
# Time: O(N^2), Space: O(1)
n = nums.length
(0...n).each do |i|
((i + 1)...n).each do |j|
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end | Approach: Hash Map for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | def two_sum(nums, target)
# Optimized: Single-pass hash map for O(1) lookups.
# Time: O(N), Space: O(N)
seen = {}
nums.each_with_index do |num, i|
complement = target - num
return [seen[complement], i] if seen.key?(complement)
seen[num] = i
end
[]
end | Approach: Two Pointer for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | def two_sum(nums, target)
# Two Pointer: Sort and use two pointers from both ends.
# Time: O(N log N), Space: O(N)
indexed = nums.map.with_index { |num, i| [num, i] }
indexed.sort_by!(&:first)
left, right = 0, indexed.length - 1
while left < right
sum_val = indexed[left][0] + indexed[right][0]
retur... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_swift_25_6f58ed5b | algorithms | arrays | medium | swift | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Handle all edge cases including empty input. | Approach: Brute Force for Two Sum
Language: Swift | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step ... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.count
for i in 0..<n {
for j in (i + 1)..<n {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
... | Approach: Hash Map for Two Sum
Language: Swift | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces ti... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
var seen = [Int: Int]()
for (i, num) in nums.enumerated() {
let complement = target - num
if let idx = seen[complement] {
return [idx, i]
... | Approach: Two Pointer for Two Sum
Language: Swift | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without ... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
var indexed = nums.enumerated().map { ($1, $0) }
indexed.sort { $0.0 < $1.0 }
var left = 0, right = indexed.count - 1
while left < right {
let s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_python_26_d84906be | algorithms | arrays | easy | python | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Optimize for large input sizes up to 10^6. | Approach: Brute Force for Two Sum
Language: Python | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | def two_sum(nums, target):
"""
Brute Force: Check every pair of elements.
Time: O(N^2), Space: O(1)
"""
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] | Approach: Hash Map for Two Sum
Language: Python | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | def two_sum(nums, target):
"""
Optimized: Single-pass hash map for O(1) lookups.
Time: O(N), Space: O(N)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] | Approach: Two Pointer for Two Sum
Language: Python | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | def two_sum(nums, target):
"""
Two Pointer: Sort and use two pointers from both ends.
Time: O(N log N), Space: O(N) for indexed copy
"""
indexed = [(num, i) for i, num in enumerate(nums)]
indexed.sort()
left, right = 0, len(indexed) - 1
while left < right:
current_sum = indexed[l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_javascript_26_36f65bb7 | algorithms | arrays | easy | javascript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Optimize for large input sizes up to 10^6. | Approach: Brute Force for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
St... | function twoSum(nums, target) {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}... | Approach: Hash Map for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces... | function twoSum(nums, target) {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
... | Approach: Two Pointer for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted witho... | function twoSum(nums, target) {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < right) {
const sum = ind... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_java_26_5505f06d | algorithms | arrays | easy | java | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Optimize for large input sizes up to 10^6. | Approach: Brute Force for Two Sum
Language: Java | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | import java.util.*;
class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j... | Approach: Hash Map for Two Sum
Language: Java | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | import java.util.*;
class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums... | Approach: Two Pointer for Two Sum
Language: Java | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | import java.util.*;
class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0]... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_cpp_26_e6aa9b47 | algorithms | arrays | easy | cpp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Optimize for large input sizes up to 10^6. | Approach: Brute Force for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: T... | #include <vector>
using namespace std;
class Solution {
public:
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time c... | #include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i+... | Approach: Two Pointer for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without losi... | #include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> indexed;
for (int i = 0; i < (int)nums.s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_go_26_a2e55537 | algorithms | arrays | easy | go | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Optimize for large input sizes up to 10^6. | Approach: Brute Force for Two Sum
Language: Go | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: Th... | package main
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
func twoSum(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
... | Approach: Hash Map for Two Sum
Language: Go | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time co... | package main
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := seen[complement]; ok {
return []int{idx, i}
... | Approach: Two Pointer for Two Sum
Language: Go | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without losin... | package main
import "sort"
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
func twoSum(nums []int, target int) []int {
type pair struct { val, idx int }
indexed := make([]pair, len(nums))
for i, num := range nums {
indexed[i] = pair{num, i}
}
sort.... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_rust_26_f120539d | algorithms | arrays | easy | rust | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Optimize for large input sizes up to 10^6. | Approach: Brute Force for Two Sum
Language: Rust | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.len();
for i in 0..n {
for j in (i + 1)..n {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
... | Approach: Hash Map for Two Sum
Language: Rust | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
let mut seen = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&... | Approach: Two Pointer for Two Sum
Language: Rust | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
let mut indexed: Vec<(i32, usize)> = nums.into_iter().enumerate()
.map(|(i, v)| (v, i)).collect();
indexed.sort_by_key(|&(v, _)| v);
let (mut l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_typescript_26_492f0b0d | algorithms | arrays | easy | typescript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Optimize for large input sizes up to 10^6. | Approach: Brute Force for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
St... | function twoSum(nums: number[], target: number): number[] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i,... | Approach: Hash Map for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces... | function twoSum(nums: number[], target: number): number[] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
... | Approach: Two Pointer for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted witho... | function twoSum(nums: number[], target: number): number[] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < righ... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_csharp_26_0800fe93 | algorithms | arrays | easy | csharp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Optimize for large input sizes up to 10^6. | Approach: Brute Force for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | using System;
using System.Collections.Generic;
public class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | using System;
using System.Collections.Generic;
public class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)... | Approach: Two Pointer for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | using System;
using System.Linq;
public class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
var indexed = nums.Select((val, idx) => new[] { val, idx }).ToArray();
Array.Sort(indexed, (a, b)... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_ruby_26_bae1f9a9 | algorithms | arrays | easy | ruby | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Optimize for large input sizes up to 10^6. | Approach: Brute Force for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | def two_sum(nums, target)
# Brute Force: Check every pair of elements.
# Time: O(N^2), Space: O(1)
n = nums.length
(0...n).each do |i|
((i + 1)...n).each do |j|
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end | Approach: Hash Map for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | def two_sum(nums, target)
# Optimized: Single-pass hash map for O(1) lookups.
# Time: O(N), Space: O(N)
seen = {}
nums.each_with_index do |num, i|
complement = target - num
return [seen[complement], i] if seen.key?(complement)
seen[num] = i
end
[]
end | Approach: Two Pointer for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | def two_sum(nums, target)
# Two Pointer: Sort and use two pointers from both ends.
# Time: O(N log N), Space: O(N)
indexed = nums.map.with_index { |num, i| [num, i] }
indexed.sort_by!(&:first)
left, right = 0, indexed.length - 1
while left < right
sum_val = indexed[left][0] + indexed[right][0]
retur... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_swift_26_544ccead | algorithms | arrays | easy | swift | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Optimize for large input sizes up to 10^6. | Approach: Brute Force for Two Sum
Language: Swift | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4:... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.count
for i in 0..<n {
for j in (i + 1)..<n {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
... | Approach: Hash Map for Two Sum
Language: Swift | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
var seen = [Int: Int]()
for (i, num) in nums.enumerated() {
let complement = target - num
if let idx = seen[complement] {
return [idx, i]
... | Approach: Two Pointer for Two Sum
Language: Swift | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without lo... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
var indexed = nums.enumerated().map { ($1, $0) }
indexed.sort { $0.0 < $1.0 }
var left = 0, right = indexed.count - 1
while left < right {
let s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_python_27_04623572 | algorithms | arrays | medium | python | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use iterative approach to avoid recursion limits. | Approach: Brute Force for Two Sum
Language: Python | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step... | def two_sum(nums, target):
"""
Brute Force: Check every pair of elements.
Time: O(N^2), Space: O(1)
"""
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] | Approach: Hash Map for Two Sum
Language: Python | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces t... | def two_sum(nums, target):
"""
Optimized: Single-pass hash map for O(1) lookups.
Time: O(N), Space: O(N)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] | Approach: Two Pointer for Two Sum
Language: Python | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without... | def two_sum(nums, target):
"""
Two Pointer: Sort and use two pointers from both ends.
Time: O(N log N), Space: O(N) for indexed copy
"""
indexed = [(num, i) for i, num in enumerate(nums)]
indexed.sort()
left, right = 0, len(indexed) - 1
while left < right:
current_sum = indexed[l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_javascript_27_b2366fe0 | algorithms | arrays | medium | javascript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use iterative approach to avoid recursion limits. | Approach: Brute Force for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
... | function twoSum(nums, target) {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}... | Approach: Hash Map for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduc... | function twoSum(nums, target) {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
... | Approach: Two Pointer for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted wit... | function twoSum(nums, target) {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < right) {
const sum = ind... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_java_27_1d843f2a | algorithms | arrays | medium | java | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use iterative approach to avoid recursion limits. | Approach: Brute Force for Two Sum
Language: Java | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | import java.util.*;
class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j... | Approach: Hash Map for Two Sum
Language: Java | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | import java.util.*;
class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums... | Approach: Two Pointer for Two Sum
Language: Java | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | import java.util.*;
class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0]... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_cpp_27_55843b8b | algorithms | arrays | medium | cpp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use iterative approach to avoid recursion limits. | Approach: Brute Force for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4:... | #include <vector>
using namespace std;
class Solution {
public:
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time... | #include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i+... | Approach: Two Pointer for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without lo... | #include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> indexed;
for (int i = 0; i < (int)nums.s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_go_27_8e1d52dc | algorithms | arrays | medium | go | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use iterative approach to avoid recursion limits. | Approach: Brute Force for Two Sum
Language: Go | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | package main
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
func twoSum(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
... | Approach: Hash Map for Two Sum
Language: Go | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | package main
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := seen[complement]; ok {
return []int{idx, i}
... | Approach: Two Pointer for Two Sum
Language: Go | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | package main
import "sort"
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
func twoSum(nums []int, target int) []int {
type pair struct { val, idx int }
indexed := make([]pair, len(nums))
for i, num := range nums {
indexed[i] = pair{num, i}
}
sort.... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_rust_27_055b69a8 | algorithms | arrays | medium | rust | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use iterative approach to avoid recursion limits. | Approach: Brute Force for Two Sum
Language: Rust | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.len();
for i in 0..n {
for j in (i + 1)..n {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
... | Approach: Hash Map for Two Sum
Language: Rust | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
let mut seen = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&... | Approach: Two Pointer for Two Sum
Language: Rust | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
let mut indexed: Vec<(i32, usize)> = nums.into_iter().enumerate()
.map(|(i, v)| (v, i)).collect();
indexed.sort_by_key(|&(v, _)| v);
let (mut l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_typescript_27_fe2c3ffe | algorithms | arrays | medium | typescript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use iterative approach to avoid recursion limits. | Approach: Brute Force for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
... | function twoSum(nums: number[], target: number): number[] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i,... | Approach: Hash Map for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduc... | function twoSum(nums: number[], target: number): number[] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
... | Approach: Two Pointer for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted wit... | function twoSum(nums: number[], target: number): number[] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < righ... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_csharp_27_ef7b50ce | algorithms | arrays | medium | csharp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use iterative approach to avoid recursion limits. | Approach: Brute Force for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step... | using System;
using System.Collections.Generic;
public class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces t... | using System;
using System.Collections.Generic;
public class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)... | Approach: Two Pointer for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without... | using System;
using System.Linq;
public class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
var indexed = nums.Select((val, idx) => new[] { val, idx }).ToArray();
Array.Sort(indexed, (a, b)... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_ruby_27_f0e52184 | algorithms | arrays | medium | ruby | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use iterative approach to avoid recursion limits. | Approach: Brute Force for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | def two_sum(nums, target)
# Brute Force: Check every pair of elements.
# Time: O(N^2), Space: O(1)
n = nums.length
(0...n).each do |i|
((i + 1)...n).each do |j|
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end | Approach: Hash Map for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | def two_sum(nums, target)
# Optimized: Single-pass hash map for O(1) lookups.
# Time: O(N), Space: O(N)
seen = {}
nums.each_with_index do |num, i|
complement = target - num
return [seen[complement], i] if seen.key?(complement)
seen[num] = i
end
[]
end | Approach: Two Pointer for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | def two_sum(nums, target)
# Two Pointer: Sort and use two pointers from both ends.
# Time: O(N log N), Space: O(N)
indexed = nums.map.with_index { |num, i| [num, i] }
indexed.sort_by!(&:first)
left, right = 0, indexed.length - 1
while left < right
sum_val = indexed[left][0] + indexed[right][0]
retur... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_swift_27_5947708f | algorithms | arrays | medium | swift | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Use iterative approach to avoid recursion limits. | Approach: Brute Force for Two Sum
Language: Swift | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step ... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.count
for i in 0..<n {
for j in (i + 1)..<n {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
... | Approach: Hash Map for Two Sum
Language: Swift | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces ti... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
var seen = [Int: Int]()
for (i, num) in nums.enumerated() {
let complement = target - num
if let idx = seen[complement] {
return [idx, i]
... | Approach: Two Pointer for Two Sum
Language: Swift | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without ... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
var indexed = nums.enumerated().map { ($1, $0) }
indexed.sort { $0.0 < $1.0 }
var left = 0, right = indexed.count - 1
while left < right {
let s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_python_28_6c42ba5d | algorithms | arrays | easy | python | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Consider memory constraints for streaming input. | Approach: Brute Force for Two Sum
Language: Python | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | def two_sum(nums, target):
"""
Brute Force: Check every pair of elements.
Time: O(N^2), Space: O(1)
"""
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] | Approach: Hash Map for Two Sum
Language: Python | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | def two_sum(nums, target):
"""
Optimized: Single-pass hash map for O(1) lookups.
Time: O(N), Space: O(N)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] | Approach: Two Pointer for Two Sum
Language: Python | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | def two_sum(nums, target):
"""
Two Pointer: Sort and use two pointers from both ends.
Time: O(N log N), Space: O(N) for indexed copy
"""
indexed = [(num, i) for i, num in enumerate(nums)]
indexed.sort()
left, right = 0, len(indexed) - 1
while left < right:
current_sum = indexed[l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_javascript_28_39355120 | algorithms | arrays | easy | javascript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Consider memory constraints for streaming input. | Approach: Brute Force for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
St... | function twoSum(nums, target) {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}... | Approach: Hash Map for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces... | function twoSum(nums, target) {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
... | Approach: Two Pointer for Two Sum
Language: Javascript | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted witho... | function twoSum(nums, target) {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < right) {
const sum = ind... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_java_28_44b755a1 | algorithms | arrays | easy | java | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Consider memory constraints for streaming input. | Approach: Brute Force for Two Sum
Language: Java | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | import java.util.*;
class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j... | Approach: Hash Map for Two Sum
Language: Java | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | import java.util.*;
class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums... | Approach: Two Pointer for Two Sum
Language: Java | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | import java.util.*;
class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0]... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_cpp_28_9abfe4c9 | algorithms | arrays | easy | cpp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Consider memory constraints for streaming input. | Approach: Brute Force for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: T... | #include <vector>
using namespace std;
class Solution {
public:
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time c... | #include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i+... | Approach: Two Pointer for Two Sum
Language: Cpp | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without losi... | #include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> indexed;
for (int i = 0; i < (int)nums.s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_go_28_bdded1ab | algorithms | arrays | easy | go | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Consider memory constraints for streaming input. | Approach: Brute Force for Two Sum
Language: Go | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: Th... | package main
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
func twoSum(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
... | Approach: Hash Map for Two Sum
Language: Go | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time co... | package main
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := seen[complement]; ok {
return []int{idx, i}
... | Approach: Two Pointer for Two Sum
Language: Go | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without losin... | package main
import "sort"
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
func twoSum(nums []int, target int) []int {
type pair struct { val, idx int }
indexed := make([]pair, len(nums))
for i, num := range nums {
indexed[i] = pair{num, i}
}
sort.... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_rust_28_b64df8e3 | algorithms | arrays | easy | rust | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Consider memory constraints for streaming input. | Approach: Brute Force for Two Sum
Language: Rust | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.len();
for i in 0..n {
for j in (i + 1)..n {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
... | Approach: Hash Map for Two Sum
Language: Rust | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
let mut seen = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&... | Approach: Two Pointer for Two Sum
Language: Rust | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
let mut indexed: Vec<(i32, usize)> = nums.into_iter().enumerate()
.map(|(i, v)| (v, i)).collect();
indexed.sort_by_key(|&(v, _)| v);
let (mut l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_typescript_28_1f86e31c | algorithms | arrays | easy | typescript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Consider memory constraints for streaming input. | Approach: Brute Force for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
St... | function twoSum(nums: number[], target: number): number[] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i,... | Approach: Hash Map for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces... | function twoSum(nums: number[], target: number): number[] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
... | Approach: Two Pointer for Two Sum
Language: Typescript | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted witho... | function twoSum(nums: number[], target: number): number[] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < righ... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_csharp_28_8087d564 | algorithms | arrays | easy | csharp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Consider memory constraints for streaming input. | Approach: Brute Force for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | using System;
using System.Collections.Generic;
public class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | using System;
using System.Collections.Generic;
public class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)... | Approach: Two Pointer for Two Sum
Language: Csharp | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | using System;
using System.Linq;
public class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
var indexed = nums.Select((val, idx) => new[] { val, idx }).ToArray();
Array.Sort(indexed, (a, b)... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_ruby_28_86f915cd | algorithms | arrays | easy | ruby | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Consider memory constraints for streaming input. | Approach: Brute Force for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | def two_sum(nums, target)
# Brute Force: Check every pair of elements.
# Time: O(N^2), Space: O(1)
n = nums.length
(0...n).each do |i|
((i + 1)...n).each do |j|
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end | Approach: Hash Map for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | def two_sum(nums, target)
# Optimized: Single-pass hash map for O(1) lookups.
# Time: O(N), Space: O(N)
seen = {}
nums.each_with_index do |num, i|
complement = target - num
return [seen[complement], i] if seen.key?(complement)
seen[num] = i
end
[]
end | Approach: Two Pointer for Two Sum
Language: Ruby | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | def two_sum(nums, target)
# Two Pointer: Sort and use two pointers from both ends.
# Time: O(N log N), Space: O(N)
indexed = nums.map.with_index { |num, i| [num, i] }
indexed.sort_by!(&:first)
left, right = 0, indexed.length - 1
while left < right
sum_val = indexed[left][0] + indexed[right][0]
retur... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_swift_28_462202e3 | algorithms | arrays | easy | swift | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Consider memory constraints for streaming input. | Approach: Brute Force for Two Sum
Language: Swift | Difficulty: easy
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4:... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.count
for i in 0..<n {
for j in (i + 1)..<n {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
... | Approach: Hash Map for Two Sum
Language: Swift | Difficulty: easy
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
var seen = [Int: Int]()
for (i, num) in nums.enumerated() {
let complement = target - num
if let idx = seen[complement] {
return [idx, i]
... | Approach: Two Pointer for Two Sum
Language: Swift | Difficulty: easy
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without lo... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
var indexed = nums.enumerated().map { ($1, $0) }
indexed.sort { $0.0 < $1.0 }
var left = 0, right = indexed.count - 1
while left < right {
let s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-easy",
"two_sum"
],
"estimated_... |
two_sum_python_29_20b50b16 | algorithms | arrays | medium | python | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Support both ascending and descending order. | Approach: Brute Force for Two Sum
Language: Python | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step... | def two_sum(nums, target):
"""
Brute Force: Check every pair of elements.
Time: O(N^2), Space: O(1)
"""
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [] | Approach: Hash Map for Two Sum
Language: Python | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces t... | def two_sum(nums, target):
"""
Optimized: Single-pass hash map for O(1) lookups.
Time: O(N), Space: O(N)
"""
seen = {}
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
return [] | Approach: Two Pointer for Two Sum
Language: Python | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without... | def two_sum(nums, target):
"""
Two Pointer: Sort and use two pointers from both ends.
Time: O(N log N), Space: O(N) for indexed copy
"""
indexed = [(num, i) for i, num in enumerate(nums)]
indexed.sort()
left, right = 0, len(indexed) - 1
while left < right:
current_sum = indexed[l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_javascript_29_ccd36a21 | algorithms | arrays | medium | javascript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Support both ascending and descending order. | Approach: Brute Force for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
... | function twoSum(nums, target) {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}... | Approach: Hash Map for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduc... | function twoSum(nums, target) {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
return [seen.get(complement), i];
... | Approach: Two Pointer for Two Sum
Language: Javascript | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted wit... | function twoSum(nums, target) {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < right) {
const sum = ind... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_java_29_00f9cd01 | algorithms | arrays | medium | java | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Support both ascending and descending order. | Approach: Brute Force for Two Sum
Language: Java | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | import java.util.*;
class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j... | Approach: Hash Map for Two Sum
Language: Java | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | import java.util.*;
class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums... | Approach: Two Pointer for Two Sum
Language: Java | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | import java.util.*;
class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
int[][] indexed = new int[n][2];
for (int i = 0; i < n; i++) {
indexed[i][0]... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_cpp_29_1bccfde7 | algorithms | arrays | medium | cpp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Support both ascending and descending order. | Approach: Brute Force for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4:... | #include <vector>
using namespace std;
class Solution {
public:
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time... | #include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> seen;
for (int i = 0; i < (int)nums.size(); i+... | Approach: Two Pointer for Two Sum
Language: Cpp | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without lo... | #include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
vector<int> twoSum(vector<int>& nums, int target) {
vector<pair<int, int>> indexed;
for (int i = 0; i < (int)nums.s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_go_29_14b3650c | algorithms | arrays | medium | go | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Support both ascending and descending order. | Approach: Brute Force for Two Sum
Language: Go | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4: ... | package main
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
func twoSum(nums []int, target int) []int {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
... | Approach: Hash Map for Two Sum
Language: Go | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces time ... | package main
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
func twoSum(nums []int, target int) []int {
seen := make(map[int]int)
for i, num := range nums {
complement := target - num
if idx, ok := seen[complement]; ok {
return []int{idx, i}
... | Approach: Two Pointer for Two Sum
Language: Go | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without los... | package main
import "sort"
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
func twoSum(nums []int, target int) []int {
type pair struct { val, idx int }
indexed := make([]pair, len(nums))
for i, num := range nums {
indexed[i] = pair{num, i}
}
sort.... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_rust_29_5273d282 | algorithms | arrays | medium | rust | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Support both ascending and descending order. | Approach: Brute Force for Two Sum
Language: Rust | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.len();
for i in 0..n {
for j in (i + 1)..n {
if nums[i] + nums[j] == target {
return vec![i as i32, j as i32];
... | Approach: Hash Map for Two Sum
Language: Rust | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
let mut seen = HashMap::new();
for (i, &num) in nums.iter().enumerate() {
let complement = target - num;
if let Some(&... | Approach: Two Pointer for Two Sum
Language: Rust | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
let mut indexed: Vec<(i32, usize)> = nums.into_iter().enumerate()
.map(|(i, v)| (v, i)).collect();
indexed.sort_by_key(|&(v, _)| v);
let (mut l... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_typescript_29_0f05bb4c | algorithms | arrays | medium | typescript | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Support both ascending and descending order. | Approach: Brute Force for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
... | function twoSum(nums: number[], target: number): number[] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
const n = nums.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i,... | Approach: Hash Map for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduc... | function twoSum(nums: number[], target: number): number[] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
const seen = new Map<number, number>();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (seen.has(complement)) {
... | Approach: Two Pointer for Two Sum
Language: Typescript | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted wit... | function twoSum(nums: number[], target: number): number[] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
const indexed = nums.map((num, i) => [num, i]);
indexed.sort((a, b) => a[0] - b[0]);
let left = 0, right = indexed.length - 1;
while (left < righ... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_csharp_29_49fe6ef8 | algorithms | arrays | medium | csharp | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Support both ascending and descending order. | Approach: Brute Force for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step... | using System;
using System.Collections.Generic;
public class Solution {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
... | Approach: Hash Map for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces t... | using System;
using System.Collections.Generic;
public class Solution {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> seen = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)... | Approach: Two Pointer for Two Sum
Language: Csharp | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without... | using System;
using System.Linq;
public class Solution {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
public int[] TwoSum(int[] nums, int target) {
var indexed = nums.Select((val, idx) => new[] { val, idx }).ToArray();
Array.Sort(indexed, (a, b)... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_ruby_29_e6798694 | algorithms | arrays | medium | ruby | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Support both ascending and descending order. | Approach: Brute Force for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step 4... | def two_sum(nums, target)
# Brute Force: Check every pair of elements.
# Time: O(N^2), Space: O(1)
n = nums.length
(0...n).each do |i|
((i + 1)...n).each do |j|
return [i, j] if nums[i] + nums[j] == target
end
end
[]
end | Approach: Hash Map for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces tim... | def two_sum(nums, target)
# Optimized: Single-pass hash map for O(1) lookups.
# Time: O(N), Space: O(N)
seen = {}
nums.each_with_index do |num, i|
complement = target - num
return [seen[complement], i] if seen.key?(complement)
seen[num] = i
end
[]
end | Approach: Two Pointer for Two Sum
Language: Ruby | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without l... | def two_sum(nums, target)
# Two Pointer: Sort and use two pointers from both ends.
# Time: O(N log N), Space: O(N)
indexed = nums.map.with_index { |num, i| [num, i] }
indexed.sort_by!(&:first)
left, right = 0, indexed.length - 1
while left < right
sum_val = indexed[left][0] + indexed[right][0]
retur... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
two_sum_swift_29_817639c7 | algorithms | arrays | medium | swift | Given an array of integers and a target sum, find indices of two numbers that add up to the target. Support both ascending and descending order. | Approach: Brute Force for Two Sum
Language: Swift | Difficulty: medium
Step 1: Start with the most straightforward approach: iterate through all possible candidates.
Step 2: Check every valid combination against the problem requirements.
Step 3: This gives higher time complexity but requires minimal extra space.
Step ... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Brute Force: Check every pair of elements.
// Time: O(N^2), Space: O(1)
let n = nums.count
for i in 0..<n {
for j in (i + 1)..<n {
if nums[i] + nums[j] == target {
return [i, j]
}
}
}
... | Approach: Hash Map for Two Sum
Language: Swift | Difficulty: medium
Step 1: Trade space for time using a hash map (dictionary) for O(1) lookups.
Step 2: For each element, compute what complement would be needed and check if seen.
Step 3: Single pass: check for complement, then store current element.
Step 4: Reduces ti... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Optimized: Single-pass hash map for O(1) lookups.
// Time: O(N), Space: O(N)
var seen = [Int: Int]()
for (i, num) in nums.enumerated() {
let complement = target - num
if let idx = seen[complement] {
return [idx, i]
... | Approach: Two Pointer for Two Sum
Language: Swift | Difficulty: medium
Step 1: Sort the input first, then use two pointers from both ends.
Step 2: Initialize left at start and right at end of sorted range.
Step 3: Move pointers inward based on comparison with target.
Step 4: Effective when input can be sorted without ... | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// Two Pointer: Sort and use two pointers from both ends.
// Time: O(N log N), Space: O(N)
var indexed = nums.enumerated().map { ($1, $0) }
indexed.sort { $0.0 < $1.0 }
var left = 0, right = indexed.count - 1
while left < right {
let s... | {
"approach_1_type": "brute_force",
"approach_2_type": "optimized",
"approach_3_type": "two_pointer",
"time_complexity_1": "O(N^2)",
"time_complexity_2": "O(N)",
"time_complexity_3": "O(N log N)",
"tags": [
"array",
"iteration",
"search",
"difficulty-medium",
"two_sum"
],
"estimate... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.