Permutations
Order matters
List every ordering of three fixed numbers (3! = 6 lines).
“Number combinations” in beginner exercises usually means nested-loop index patterns: every ordering of three values, then every unordered pair from a longer list. This tutorial covers permutations vs combinations, distinct-index rules, a live preview, worked PHP examples, edge cases, and complexity.
Order matters
List every ordering of three fixed numbers (3! = 6 lines).
Order ignored
List each unordered pair once with j > i.
i ≠ j ≠ k
Use each array slot once per printed triple.
Six pairs
Classic C(4, 2) listing from four numbers.
Three ints
Type three integers and see all six orderings.
Nested cost
Pair loops are quadratic; fixed size-3 perms are O(1).
Number combination exercises usually ask you to systematically try index patterns with nested loops. First: every way to fill three slots from three values (permutations). Second: every way to pick two slots from four without repeating the same pair (combinations).
Think of three chairs and three people for Example 1: each seating arrangement is a different ordering. For Example 2 you only pick two people out of four and do not care who was “first,” so you force the first index to be smaller than the second.
It trains nested loops, index discipline, and the vocabulary of permutation vs combination before heavier combinatorics or recursion.
Yes → permutation; no → combination.
i, j, k all unequal for three-of-three.
Lists each unordered pair once.
Trace small arrays before scaling up.
In short: use nested loops with the right index rules — distinct indices for orderings, j > i for unordered pairs — and print each valid selection.
Store numbers in an array, then visit valid index tuples with nested for loops and print each result.
// [1, 2, 3] orderings -> 6 lines (permutations)
// [10, 20, 30, 40] pairs -> 6 lines (C(4, 2)) | Item | Type | Description |
|---|---|---|
$arr | array | Small list of integers to rearrange or pair. |
| Printed output | text | One line per valid ordering or pair. |
// All orderings of three array entries (indices 0..2)
for i in 0..2:
for j in 0..2:
for k in 0..2:
if i, j, k all different:
print arr[i], arr[j], arr[k]
// All unordered pairs from indices 0..n-1
for i in 0..n-2:
for j in i+1 .. n-1:
print arr[i], arr[j] | Pattern | Index rule | Counts |
|---|---|---|
| Permutation (size 3) | i, j, k all distinct | 3! = 6 when values differ |
| Unordered pairs | j = i + 1 .. n-1 | C(n, 2) |
| Ordered pairs | i != j | n(n-1) |
| Goal | Pattern |
|---|---|
| Distinct triple indices | if ($i !== $j && $j !== $k && $i !== $k) |
| Unordered pair | for ($j = $i + 1; $j < $n; $j++) |
| Ordered pair | if ($i !== $j) inside double loop |
| Print a triple | echo $arr[$i] . " " . $arr[$j] . " " . $arr[$k] . "\n"; |
| Count of 3-perms | 3! = 6 for three distinct values |
| Count of pairs | C(n, 2) = n(n-1)/2 |
Same nested-loop toolbox — different index rules and meanings.
order mattersExample 1 — all seatings of three values
j > iExample 2 — each unordered pair once
i != jExample 3 — (a,b) and (b,a) both count
ask order?Clarify before choosing the index rule
Reach for these nested-loop patterns when you must list selections systematically.
Nested loops, index rules, and perm vs combo vocabulary.
See the pattern on paper before Heap’s algorithm.
All teams of two, all edges in a complete graph.
Visit each subset or ordering instead of only counting.
Deep nesting explodes — cap demos and discuss asymptotics.
Key benefit: one visual nested-loop drill that separates “order matters” from “order does not.”
Enter three integers separated by commas (same as Example 1: 1, 2, 3). Shows all six orderings when indices are distinct.
Three complete PHP programs — permutations of three numbers, unordered pairs from four, and ordered pairs. Click View Output to reveal sample console results.
All orderings of three fixed values.
Array [1, 2, 3]. These are permutations (order matters). Mathematicians reserve “combination” for selections where order does not matter.
<?php
$arr = [1, 2, 3];
echo "All orderings (permutations) of the three numbers:\n";
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j < 3; $j++) {
for ($k = 0; $k < 3; $k++) {
if ($i !== $j && $j !== $k && $i !== $k) {
echo $arr[$i] . " " . $arr[$j] . " " . $arr[$k] . "\n";
}
}
}
}
?> The condition $i !== $j && $j !== $k && $i !== $k ensures each printed triple uses three different positions, so each value appears once per line. For three distinct numbers you always get 3! = 6 lines.
Each unordered pair appears once — classic choose-2 listing.
Values 10, 20, 30, 40. Each unordered pair appears once — C(4, 2) = 6 lines.
<?php
$arr = [10, 20, 30, 40];
$n = count($arr);
echo "All unordered pairs (choose 2 from $n):\n";
for ($i = 0; $i < $n; $i++) {
for ($j = $i + 1; $j < $n; $j++) {
echo $arr[$i] . " " . $arr[$j] . "\n";
}
}
?> Starting $j at $i + 1 skips self-pairs and skips the reverse of each pair. That is the standard nested-loop shape for combinations of size 2.
If (a, b) and (b, a) should both appear, drop the j > i rule.
Same four numbers, but both directions count — 4 × 3 = 12 lines.
<?php
$arr = [10, 20, 30, 40];
$n = count($arr);
echo "All ordered pairs (i != j):\n";
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($i !== $j) {
echo $arr[$i] . " " . $arr[$j] . "\n";
}
}
}
?> Compared with Example 2, you keep both 10 20 and 20 10. Ask the interviewer whether order matters before choosing j = i + 1 vs i !== $j.
Put the numbers in an indexed array.
Distinct indices for perms; j > i for unordered pairs.
One echo per valid tuple of indices.
Every valid ordering or pair has been printed.
[10, 20, 30, 40]Trace Example 2: outer $i, then $j from $i + 1.
$i | $j values | Printed pairs |
|---|---|---|
0 (10) | 1, 2, 3 | 10 20, 10 30, 10 40 |
1 (20) | 2, 3 | 20 30, 20 40 |
2 (30) | 3 | 30 40 |
3 (40) | (none) | — |
Total: 3 + 2 + 1 = 6 = C(4, 2).
Where permutation and combination nested loops show up beyond the prompt.
Nested loops and clear index rules.
Example: print all pairs with j > i.
Visit each selection instead of only counting.
Example: C(4, 2) = 6 lines.
Compare every unordered duo in a list.
Example: find equal pairs.
Build intuition before recursive generators.
Example: Heap’s algorithm later.
State O(n²) for pairs, O(n³) for triples.
Example: interview asymptotics.
Separate “combination lock” talk from math terms.
Example: order matters or not?
Pro Tip: ask “does order matter?” before writing the first nested loop.
Why nested-loop listing works well for beginners and interviews.
Dry-run small arrays on paper and match the printed lines.
Distinct vs j > i is the whole skill in miniature.
Beginners can finish without call stacks or backtracking.
Fixed 3-perms are O(1); general pairs are O(n²).
Pro Tip: name the pattern aloud (“unordered pairs with j = i + 1”) before coding the loops.
Small habits that keep combination listings interview-ready.
Permutation vs combination changes the index rule.
Cleaner than a full double loop plus j > i checks.
Identical numbers can print identical-looking lines.
3! or C(n, 2) is a fast self-check.
Keep n small while learning; mention recursion for larger sets.
Pro Tip: dry-run C(4, 2) as 3+2+1 before coding — if your loop prints six lines, the bounds are almost certainly right.
Mistakes that commonly break combination / permutation listings.
Using j > i when the prompt wants both (a,b) and (b,a).
→ Ask whether order matters first.
Printing triples that reuse the same array slot.
→ Require i, j, k all different.
Starting $j at $i includes self-pairs.
→ Start at $i + 1 for unordered pairs.
Identical numbers produce duplicate-looking output.
→ Filter or sort if uniqueness is required.
Using three nested loops for huge n without discussing cost.
→ State O(n³) and mention better generators for large sets.
Handle these before calling the listing done.
Example 1 may print duplicate-looking triples; tighten logic if you need unique outputs only.
n < 2 for pairsThe inner loop never runs; no pairs to print — expected.
Unordered listing prints a single line.
Print nothing; guard if callers can pass empty input.
Index rules still work; values are just numbers.
Output and time explode — discuss better algorithms.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
i, j, k give permutations of three entries.j > i lists each 2-combination once.Quick Takeaway: distinct indices for orderings, j = i + 1 for unordered pairs — and say whether order matters.
| Program | Time | Extra space |
|---|---|---|
| Three nested loops, size 3 | O(1) (fixed 27 iterations) | O(1) |
| Unordered pairs from n | O(n²) | O(1) |
| Ordered pairs from n | O(n²) | O(1) |
For general triples from n elements, expect O(n³) if you scan all index triples.
Number-combination drills are nested loops with the right index rules: distinct slots for permutations, j > i for unordered pairs, and i != j when order matters. Master the vocabulary and the counts (3!, C(n, 2)) so you can explain either pattern in an interview.
Practice the three examples above, then continue to checking odd numbers.
Ask whether order matters, pick the matching index rule, and verify the line count against 3! or C(n, 2).
$i + 1List selections the interview-friendly way.
Order matters
Vocabj > i pairs
Vocabi, j, k distinct
Rule3! / C(n,2)
CheckO(n²) pairs
AnalysisIn everyday language people say “combination lock,” but mathematically a combination ignores order while a permutation counts different orders as different. This page shows both ideas with small nested loops so you can see the pattern before heavier math.
Learn how to check whether a number is odd using the modulo operator in PHP.
8 people found this page helpful