Code together.
Solve
faster.
Level up.

The best way to practice coding challenges with friends, bootcamps, and your dev squad.Crush problems faster together.

Get early access and product updates:

Wallpaper

Gamma Code Editor

00:00

Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9

Output: [0,1]

Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

JavaScript Demo

This is a demo to gauge interest. We're developing this collaborative coding platform and want to see if people are interested. If you want updates as we build this product:

Sign Up for Updates
Your EditorEDITABLE
1function twoSum(nums, target) {
2  const seen = {};
3  
4  for (let i = 0; i < nums.length; i++) {
5    const complement = target - nums[i];
6    
7    if (seen[complement] !== undefined) {
8      return [seen[complement], i];
9    }
10    
11    seen[nums[i]] = i;
12  }
13  
14  return null;
15}
Sarah ChenOnline
function twoSum(nums, target) {
  // Using a map to store values we've seen
  const map = new Map();
  
  for (let i = 0; i < nums.length; i++) {
    const diff = target - nums[i];
    
    // If we've seen the complement, return both indices
    if (map.has(diff)) {
      return [map.get(diff), i];
    }
    
    // Store current number and its index
    map.set(nums[i], i);
  }
}
James RodriguezAway
// Brute force approach
function twoSum(nums, target) {
  for (let i = 0; i < nums.length; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] + nums[j] === target) {
        return [i, j];
      }
    }
  }
  return null;
}
App Icon
Klack App Icon

Key Features

A better way to solve coding challenges together

Our real-time collaborative editor and integrated tools help coding teams learn faster, solve problems more effectively, and track progress together.

šŸ’» Real-time Collaborative Editor

Write, run, and debug code together in perfect sync. Multiple cursors, syntax highlighting, and instant updates make pair programming seamless.

JavaScript - twoSum.js
1
Ana is typing...
Jay

šŸ“‹ Problem Viewer

Seamlessly import coding problems from LeetCode, HackerRank, and other platforms. Everyone sees the same problem with test cases and constraints.

Two Sum
Easy
Problem:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Example:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0,1].
Constraints:
  • 2≤nums.length≤104
  • -109≤nums[i]≤109
  • -109≤target≤109
  • Only one valid answer exists.

šŸ“Š Group Progress

Track your team's practice history, achievements, and streaks. See which problems your group has solved and who contributed.

Weekly Problems
14/20 completed
Current Streak
7 days
1
2
3
4
5
6
7
8
9
10
Team Leaderboard
J
Jay
42 solved
A
Ana
38 solved
M
Mike
35 solved
S
Sara
31 solved

šŸ¤– AI Coding Assistant

Get intelligent code hints, explanations, and optimizations when you're stuck. Our AI helps you understand solutions without giving away the answer.

šŸ¤–

I notice you're trying to optimize your two-sum solution. Your current approach has an O(n²) time complexity. Have you considered using a hash map to reduce it to O(n)?

šŸ‘Øā€šŸ’»

I'm not sure how to implement that. Can you explain the hash map approach without giving me the full code?

šŸ¤–

Sure! With a hash map, you can store each number you've seen and its index.

  1. Iterate through the array once
  2. For each number, calculate what value you need to reach the target (target - current)
  3. Check if that value is already in your map
  4. If found, you have your pair! If not, add the current number to the map

This way, you only need to go through the array once, giving you O(n) time complexity.

Gamma