combinations
array pairs
algorithm
data structures
programming techniques

Generating all combinations of elements in a single array in pairs

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Generating combinations of elements from a single array is a common task in programming, often needed in mathematical computations, data analysis, and algorithm development. This article delves into generating all unique combinations of array elements in pairs, relevant use-cases, and technical explanations.

What are Combinations?

Combinations are selections made by choosing elements from a set without regard to the order in which they are selected. When discussing combinations in pairs, we are specifically referring to the task of choosing two elements from an array and forming unique pairs.

Technical Explanation

Given an array $\text\{arr\}$ of $n$ elements, a combination in pairs involves selecting all possible unique pairs of elements. If order matters, combinations are considered permutations; however, when the order does not matter (e.g., a,b{a, b} is identical to b,a{b, a}), they are true combinations.

For instance, consider an array with 4 elements: arr=A,B,C,D\text{arr} = {A, B, C, D}. The possible combinations in pairs are:

A,B{A, B}A,C{A, C}A,D{A, D}B,C{B, C}B,D{B, D}C,D{C, D}

Mathematical Background

The number of unique pairs (combinations) can be calculated using the binomial coefficient, often denoted as (nk)\binom{n}{k}, where nn is the total number of elements, and kk is the size of each combination (in this case, 22). The formula is:

(nk)=n!k!(nk)!\binom{n}{k} = \frac{n!}{k!(n-k)!}

For combinations of pairs:

(n2)=n(n1)2\binom{n}{2} = \frac{n(n-1)}{2}

This formula provides the count of unique pairs. For the array A,B,C,D{A, B, C, D}, this results in (42)=6\binom{4}{2} = 6 pairs.

Algorithm to Generate Combinations

The algorithm to generate combinations involves nested iterations through the array, avoiding duplication:

  1. Initialization: Start with an empty result list.
  2. Iteration: Loop through each element of the array with an index ii.
  3. Nested Loop: For each ii, start a second loop from index i+1i + 1 to the end of the array.
  4. Collect Pairs: For each pair of indices ii and jj, add the combination arr[i],arr[j]{\text{arr}[i], \text{arr}[j]} to the list of combinations.

Example Code (Python)

Time Complexity: The algorithm involves two nested loops over the array, leading to O(n2)O(n^2) time complexity. • Space Complexity: The space complexity depends on the number of generated pairs, approximately O(n2)O(n^2), as each pair needs to be stored.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.