# 3Sum Problem Solution in Haskell

This repository contains an efficient Haskell implementation of the classic 3Sum problem.

## Problem Description

Given an integer array `nums`, return all the triplets `[nums[i], nums[j], nums[k]]` such that:
- `i != j`, `i != k`, and `j != k`
- `nums[i] + nums[j] + nums[k] == 0`

The solution set must not contain duplicate triplets.

## Algorithm Approaches

### 1. Efficient Solution - O(n²) Time Complexity

The main `threeSum` function uses a two-pointer technique:

1. **Sort the array** - This enables the two-pointer approach
2. **Fix the first element** - Iterate through each element as the first element of the triplet
3. **Two-pointer search** - For the remaining elements, use two pointers (left and right) to find pairs that sum to the negative of the first element
4. **Skip duplicates** - Avoid duplicate triplets by skipping repeated values

**Time Complexity:** O(n²)  
**Space Complexity:** O(1) excluding the output array

### 2. Naive Solution - O(n³) Time Complexity

The `threeSumNaive` function checks all possible triplets:
- Uses three nested loops to examine every combination
- Useful for comparison and understanding the problem

**Time Complexity:** O(n³)  
**Space Complexity:** O(1) excluding the output array

## Key Features

- **Pure Functional Implementation**: Uses immutable data structures and pure functions
- **Type Safety**: Leverages Haskell's strong type system with custom `Triplet` type alias
- **Duplicate Handling**: Automatically removes duplicate triplets using Set operations
- **Comprehensive Testing**: Includes test cases covering edge cases and typical scenarios

## Usage

### Compile and Run

```bash
# Compile the main module
ghc -o threesum ThreeSum.hs

# Run the example
./threesum

# Compile and run tests
ghc -o test_threesum test_threesum.hs
./test_threesum
```

### Interactive Usage (GHCi)

```haskell
ghci ThreeSum.hs

-- Example usage
*ThreeSum> threeSum [-1, 0, 1, 2, -1, -4]
[(-1,-1,2),(-1,0,1)]

*ThreeSum> threeSum [0, 0, 0]
[(0,0,0)]

*ThreeSum> threeSum [0, 1, 1]
[]
```

## Example Test Cases

| Input | Output | Explanation |
|-------|--------|-------------|
| `[-1,0,1,2,-1,-4]` | `[(-1,-1,2),(-1,0,1)]` | Two valid triplets found |
| `[0,1,1]` | `[]` | No triplets sum to zero |
| `[0,0,0]` | `[(0,0,0)]` | Only one unique triplet |
| `[-2,0,1,1,2]` | `[(-2,0,2),(-2,1,1)]` | Multiple valid combinations |

## Code Structure

- **`ThreeSum.hs`** - Main implementation with both efficient and naive solutions
- **`test_threesum.hs`** - Comprehensive test suite
- **`README_ThreeSum.md`** - This documentation file

## Algorithm Walkthrough

For input `[-1, 0, 1, 2, -1, -4]`:

1. **Sort**: `[-4, -1, -1, 0, 1, 2]`
2. **Fix first element** `-4`:
   - Look for pairs in `[-1, -1, 0, 1, 2]` that sum to `4`
   - No valid pairs found
3. **Fix first element** `-1`:
   - Look for pairs that sum to `1`
   - Find: `(-1, 0, 1)` and `(-1, -1, 2)`
4. **Continue** until all elements processed
5. **Remove duplicates** and return result

## Performance Characteristics

- **Best Case**: O(n²) when array is already sorted
- **Average Case**: O(n²) 
- **Worst Case**: O(n²)
- **Space**: O(1) additional space (excluding output)

The algorithm is optimal for the 3Sum problem, as no solution exists with better than O(n²) time complexity in the general case.