Challenge - 5 Problems
Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Permutations for Small Array
What is the output of the following C++ code that generates all permutations of the array [1, 2]?
DSA C++
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> arr = {1, 2}; do { for (int x : arr) std::cout << x << ' '; std::cout << '\n'; } while (std::next_permutation(arr.begin(), arr.end())); return 0; }
Attempts:
2 left
💡 Hint
Remember that std::next_permutation generates permutations in lexicographical order starting from the current arrangement.
✗ Incorrect
The array starts as [1, 2], which is the first permutation. next_permutation generates the next lexicographical permutation, which is [2, 1]. After that, it returns false and stops.
❓ Predict Output
intermediateNumber of Permutations for Array of Size 3
How many permutations will be printed by the following code for the array [1, 2, 3]?
DSA C++
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> arr = {1, 2, 3}; int count = 0; do { count++; } while (std::next_permutation(arr.begin(), arr.end())); std::cout << count << '\n'; return 0; }
Attempts:
2 left
💡 Hint
The number of permutations of n distinct elements is n factorial (n!).
✗ Incorrect
For 3 elements, the number of permutations is 3! = 3 × 2 × 1 = 6.
🔧 Debug
advancedWhy Does This Permutation Code Fail to Print All Permutations?
Consider this code snippet intended to print all permutations of [1, 2, 3]. Why does it not print all permutations?
DSA C++
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> arr = {1, 2, 3}; while (std::next_permutation(arr.begin(), arr.end())) { for (int x : arr) std::cout << x << ' '; std::cout << '\n'; } return 0; }
Attempts:
2 left
💡 Hint
Think about what the initial arrangement is and when printing happens.
✗ Incorrect
The code only prints after calling next_permutation, so the initial arrangement [1, 2, 3] is never printed. To print all permutations, print before calling next_permutation or use a do-while loop.
🧠 Conceptual
advancedUnderstanding Permutation Generation Algorithm
Which of the following best describes how std::next_permutation generates the next permutation?
Attempts:
2 left
💡 Hint
Think about how lexicographical order is maintained when generating permutations.
✗ Incorrect
std::next_permutation works by finding the longest non-increasing suffix, swapping the pivot with the next larger element, then reversing the suffix to get the next lexicographically greater permutation.
🚀 Application
expertPermutations with Duplicate Elements Count
Given the array [1, 2, 2], how many unique permutations will be generated by std::next_permutation if the array is sorted initially?
DSA C++
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> arr = {1, 2, 2}; int count = 0; do { count++; } while (std::next_permutation(arr.begin(), arr.end())); std::cout << count << '\n'; return 0; }
Attempts:
2 left
💡 Hint
Consider the formula for permutations of multiset: n! divided by factorials of duplicates.
✗ Incorrect
The array has 3 elements with one duplicate '2'. Number of unique permutations = 3! / 2! = 6 / 2 = 3.
