Two Sum Solution for Accenture

Company: Accenture · Difficulty: Easy · Topics: Array, Hash Table

Problem Statement

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

Key Understanding:

Examples

Example 1:

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

Output: [0,1]

Explanation:

Example 2:

Input: nums = [3,2,4], target = 6

Output: [1,2]

Explanation:

Example 3:

Input: nums = [3,3], target = 6

Output: [0,1]

Explanation:

Example 4:

Input: nums = [1,5,8,10,13], target = 18

Output: [2,4]

Explanation:

Intuition

The key insight is that for each number, we need to find its complement (target - current_number) in the array. If we can find this complement efficiently, we've solved the problem.

Core Concept:

Why Hash Table Works:

Approach

Approach 1: Hash Table (Optimal)

Algorithm:

1. Create an empty hash table to store numbers and their indices

2. Iterate through the array from left to right

3. For each number nums[i]:

4. If no solution found, return empty array (shouldn't happen per constraints)

Why This Works:

Complexity Analysis:

Approach 2: Two Pointer (Requires Sorting)

Algorithm:

1. Create pairs of (value, original_index) for each element

2. Sort these pairs by value

3. Use two pointers: left at start, right at end

4. While left < right:

5. If no solution found, return empty array

Why This Works:

Complexity Analysis:

Approach 3: Brute Force

Algorithm:

1. Use nested loops to check all possible pairs

2. Outer loop: i from 0 to n-2

3. Inner loop: j from i+1 to n-1

4. For each pair (i, j):

5. If no solution found, return empty array

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: nums = [2,7,11,15], target = 9

Hash Table Approach:

Initial State: hash_table = {}, i = 0

Step 1: i = 0, num = 2

Step 2: i = 1, num = 7

Two Pointer Approach:

Step 1: Create pairs with original indices

Step 2: Sort pairs by value

Step 3: Two pointer search

Brute Force Approach:

Step 1: Check all pairs

Key Insights

1. Complement Strategy: For each number, look for its complement (target - number)

2. Hash Table Efficiency: O(1) average time for insert and lookup

3. Single Pass: Hash table approach requires only one pass through the array

4. Index Preservation: Need to store both values and their original indices

5. Unique Solution: Problem guarantees exactly one valid answer

Edge Cases

1. Two Same Numbers: nums = [3,3], target = 6 → [0,1] (same value, different indices)

2. Large Numbers: Handle integer overflow in calculations

3. Negative Numbers: Hash table approach works with negative numbers

4. Single Element: Not possible (constraints: 2 ≤ nums.length)

5. No Solution: Not possible (constraints: exactly one valid answer exists)

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Hash Table (Optimal)

Time Complexity: O(n) - We traverse the array once

Space Complexity: O(n) - We store at most n elements in hash table

Advantages:

Disadvantages:

Solution 2: Two Pointer (Requires Sorting)

Time Complexity: O(n log n) due to sorting

Space Complexity: O(n) to store original indices

Advantages:

Disadvantages:

Solution 3: Brute Force

Time Complexity: O(n²) - We check n(n-1)/2 pairs

Space Complexity: O(1) - No extra space needed

Advantages:

Disadvantages:

More Accenture coding questions