In coding interviews, number combinations usually means listing selections from a set with nested loops. If order matters, you are listing permutations. If order is ignored, you are listing combinations (each subset once).
Remember
Permutation: order matters
[1, 2, 3] → 6 lines (3! orderings)
Combination: order ignored
choose 2 from [10, 20, 30, 40] → 6 pairs
choose 3 from [10, 20, 30, 40] → 4 triples
Pattern
Index rule
Count
3 permutations
i != j != k (distinct slots)
3! = 6
Unordered pairs
j = i + 1
C(n, 2)
Unordered triples
i < j < k
C(n, 3)
Approach
How to Generate Combinations in C
Four clear steps — then pick permutations or increasing-index combinations.
Store the numbers in an array.
Decide whether order matters (permutations) or not (combinations).
Use nested loops: distinct indices for orderings, or j = i + 1 / k = j + 1 for subsets.
Print each valid selection once.
Method
Idea
Time
Best for
3 nested loops
All orderings with distinct indices
O(1) for n = 3
Small fixed-size permutations
Pairs j = i + 1
Choose 2, order ignored
O(n²)
Unordered pair listing
Triples i < j < k
Choose 3, order ignored
O(n³)
Unordered triple listing
Pseudocode
Pseudocode
function PrintThreePermutations(arr): // length 3
for i from 0 to 2:
for j from 0 to 2:
for k from 0 to 2:
if i, j, k are all different:
output arr[i], arr[j], arr[k]
function PrintUnorderedPairs(arr):
n = length(arr)
for i from 0 to n - 1:
for j from i + 1 to n - 1:
output arr[i], arr[j]
function PrintUnorderedTriples(arr):
n = length(arr)
for i from 0 to n - 1:
for j from i + 1 to n - 1:
for k from j + 1 to n - 1:
output arr[i], arr[j], arr[k]
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Permutation
Order matters — distinct indices
Combination
Order ignored — increasing indices
Pairs trick
for (j = i + 1; ...)
Triples trick
i < j < k via nested starts
Count (3 distinct)
3! = 6 orderings
Count (pairs)
C(n, 2) = n(n-1)/2
Classic check
[1,2,3] → 6 lines; 4 values → 6 pairs
Try it
Live Preview
Enter three integers to list all distinct-index orderings (permutations).
Use commas or spaces. Needs exactly three whole numbers.
Interview tip: say whether order matters before coding the loops.
One line: distinct indices for orderings; increasing indices for unordered subsets.
Frequently Asked Questions
This page shows two nested-loop patterns: all orderings of three numbers (permutations), and unordered pairs or triples (combinations where order does not matter).
In a permutation, order matters (1 2 3 and 2 1 3 are different). In a combination, order is ignored, so each subset appears once.
It forces increasing indices, so you never print both (10, 20) and (20, 10). That is choose 2 from n.
3! = 6. The triple nested loops with distinct indices print all of them.
Listing pairs is O(n²). Listing unordered triples is O(n³). Fixed-size three-element permutations are O(1).