📖 Scenario: You are helping a friend organize a small photo album. They want to see all possible ways to arrange 3 photos in a row. This means finding every order the photos can be placed.
🎯 Goal: Build a program that generates and prints all possible orders (permutations) of a list of 3 numbers representing photos.
📋 What You'll Learn
Create an array called photos with the values 1, 2, and 3
Create a helper function called swap that swaps two elements in the array
Write a recursive function called permute that generates all permutations of the array
Print each permutation on its own line with elements separated by spaces
💡 Why This Matters
🌍 Real World
Generating all permutations is useful in tasks like arranging photos, scheduling, or solving puzzles where all possible orders matter.
💼 Career
Understanding permutations and recursion is important for software roles involving algorithms, problem solving, and optimization.
Progress0 / 4 steps
1
Create the initial array
Create an integer array called photos with exactly these values: 1, 2, and 3.
DSA C++
Hint
Use C++ array syntax with curly braces to list the values.
2
Create a swap helper function
Write a function called swap that takes two integer references a and b and swaps their values.
DSA C++
Hint
Use a temporary variable to hold one value while swapping.
3
Write the recursive permutation function
Write a recursive function called permute that takes the array photos, an integer start, and an integer end. It should generate all permutations by swapping elements and recursively calling itself.
DSA C++
Hint
Use a base case when start == end to print the array. Use a loop to swap and recurse.
4
Call the permutation function and print results
Call the permute function with photos, 0 as start, and 2 as end to print all permutations.
DSA C++
Hint
Call permute(photos, 0, 2); inside main() to start printing all permutations.
Practice
(1/5)
1. What does the process of generating all permutations of an array mean?
easy
A. Listing all possible orders of the array elements
B. Sorting the array in ascending order
C. Finding the maximum element in the array
D. Removing duplicate elements from the array
Solution
Step 1: Understand permutations
Permutations are all possible ways to arrange elements in order.
Step 2: Apply to array elements
Generating all permutations means listing every possible order of the array elements.
Final Answer:
Listing all possible orders of the array elements -> Option A
Quick Check:
Permutations = all orders [OK]
Hint: Permutations = all possible orders of elements [OK]
Common Mistakes:
Confusing permutations with sorting
Thinking permutations remove duplicates
Assuming permutations find max or min
2. Which of the following C++ function signatures is correct for a recursive function generating all permutations of an integer array arr with size n</> and current index start?
easy
A. void permute(int arr[], int start, int n)
B. int permute(int arr[], int n, int start)
C. void permute(int arr[], int n)
D. int permute(int arr[], int start)
Solution
Step 1: Check parameter order and types
The function needs the array, current index (start), and total size (n) to generate permutations recursively.
Step 2: Validate return type and parameters
It should return void (no value) and have parameters (int arr[], int start, int n) in that order.
Final Answer:
void permute(int arr[], int start, int n) -> Option A