CSES Solutions - Permutations
Last Updated :
06 Aug, 2024
A permutation of integers 1,2 ... N is called beautiful if there are no adjacent elements whose difference is 1. Given N, construct a beautiful permutation if such a permutation exists. If there are no solutions, print "NO SOLUTION".
Examples:
Input: N = 5
Output: 4 2 5 3 1
Explanation: No two adjacent elements have a difference of 1.
Input: N = 3
Output: NO SOLUTION
Explanation: No permutation of size 3 is possible.
Approach: To solve the problem, follow the below idea:
The idea is to construct a beautiful permutation by first outputting all the even numbers up to n
and then all the odd numbers up to n
. This approach ensures that no two adjacent elements have a difference of 1, as even numbers have a difference of at least 2 from each other, and the same holds for odd numbers. For n=2 and n=3, there is no possible solution.
Proof of Correctness:
The correctness of the code can be proven by examining the characteristics of even and odd numbers. The solution can be proved using 3 cases:-
1. Even-Even Adjacent Pair: For even numbers, the difference between consecutive elements is always 2 (e.g., 2, 4, 6, ...). Therefore, if we print all even numbers first, no two even numbers will be adjacent with a difference of 1.
2. Odd-Odd Adjacent Pair: For odd numbers, the difference between consecutive elements is also always 2 (e.g., 1, 3, 5, ...). If we print all odd numbers after the even numbers, no two odd numbers will be adjacent with a difference of 1.
3. Even-Odd Adjacent Pair: We output all even numbers first and then all odd numbers, so there is one even-odd adjacent pair which consists of the greatest even number less than or equal to n
, and the first odd number will be 1. If the difference between these two numbers is greater than 1, then the permutation is valid, satisfying the conditions of a beautiful permutation. For n is greater than 3, the difference between the greatest even number less than or equal to n and the first odd number is always greater than 1, so our solution is correct. For n=2 or n=3, no permutation is possible.
Step-by-step algorithm:
- Check if
n
is 2 or 3. If so, it is not possible to construct a beautiful permutation, and "NO SOLUTION" is printed. Otherwise, - Iterate through all even numbers from 2 to
n
with a step size of 2 and print them. - Iterate through all odd numbers from 1 to
n
with a step size of 2 and print them.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to construct a beautiful permutation
void permutation(int N)
{
// Check if N is 2 or 3, as a beautiful permutation is
// not possible for these cases
if (N == 2 || N == 3) {
cout << "NO SOLUTION\n";
return;
}
// Output all even numbers first
for (int i = 2; i <= N; i = i + 2) {
// Print even numbers with a step of 2
cout << i << " ";
}
// Output all odd numbers next
for (int i = 1; i <= N; i = i + 2) {
// Print odd numbers with a step of 2
cout << i << " ";
}
}
// Driver Code
int main()
{
int N = 5;
// Call the permutation function with input N
permutation(N);
}
Java
public class BeautifulPermutation {
// Function to construct a beautiful permutation
public static void permutation(int N) {
// Check if N is 2 or 3, as a beautiful permutation is not possible for these cases
if (N == 2 || N == 3) {
System.out.println("NO SOLUTION");
return;
}
// Output all even numbers first
for (int i = 2; i <= N; i += 2) {
System.out.print(i + " ");
}
// Output all odd numbers next
for (int i = 1; i <= N; i += 2) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String[] args) {
int N = 5;
// Call the permutation function with input N
permutation(N);
}
}
Python
def permutation(N):
# Check if N is 2 or 3, as a beautiful permutation is not possible for these cases
if N == 2 or N == 3:
print("NO SOLUTION")
return
# Output all even numbers first
for i in range(2, N + 1, 2):
print(i)
# Output all odd numbers next
for i in range(1, N + 1, 2):
print(i)
# Driver Code
if __name__ == "__main__":
N = 5
# Call the permutation function with input N
permutation(N)
C#
using System;
class BeautifulPermutation
{
// Function to construct a beautiful permutation
static void Permutation(int N)
{
// Check if N is 2 or 3, as a beautiful permutation is not possible for these cases
if (N == 2 || N == 3)
{
Console.WriteLine("NO SOLUTION");
return;
}
// Output all even numbers first
for (int i = 2; i <= N; i += 2)
{
Console.Write(i + " ");
}
// Output all odd numbers next
for (int i = 1; i <= N; i += 2)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
public static void Main(string[] args)
{
int N = 5;
// Call the permutation function with input N
Permutation(N);
}
}
JavaScript
function permutation(N) {
// Check if N is 2 or 3, as a beautiful permutation is not possible for these cases
if (N === 2 || N === 3) {
console.log("NO SOLUTION");
return;
}
let result = '';
// Output all even numbers first
for (let i = 2; i <= N; i += 2) {
result += i + ' ';
}
// Output all odd numbers next
for (let i = 1; i <= N; i += 2) {
result += i + ' ';
}
console.log(result.trim());
}
// Driver Code
const N = 5;
// Call the permutation function with input N
permutation(N);
Time Complexity: O(N), where N is the size of permutation we have to construct.
Auxiliary Space: O(1)
Similar Reads
CSES Solutions - Coin Combinations II Consider a money system consisting of N coins. Each coin has a positive integer value. Your task is to calculate the number of distinct ordered ways you can produce a money sum X using the available coins.Examples:Input: N = 3, X = 9, coins[] = {2, 3, 5}Output: 3Explanation: There are three ways to
8 min read
What is Restricted Permutation? Ans: Restricted permutation is the arrangement of elements made under certain restrictions. In restricted permutations, certain elements are always either included or excluded. Restricted PermutationsThe permutation is a way of filtering and selecting a set of objects, where the arrangement of objec
4 min read
CSES Solutions - Creating Strings Given a string S, your task is to generate all different strings that can be created using its characters. Examples: Input: S = "aabac"Output: 20aaabcaaacbaabacaabcaaacabaacbaabaacabacaabcaaacaabacabaacbaabaaacbaacabacaabcaaacaaabcaabacabaacbaaa Explanation: 20 unique strings can be generated by rea
5 min read
Class 11 RD Sharma Solutions- Chapter 16 Permutations - Exercise 16.1 Question 1. Compute:(i) 30!/28!(ii) (11! â 10!)/9!(iii) L.C.M. (6!, 7!, 8!) Solution: (i) 30!/28! We know that, n! = n(n-1)! Therefore, 30! = 30 Ã 29! = 30 Ã29Ã28! 30!/28! = (30 Ã 29 Ã 28!)/28! = 30 Ã 29 = 870 (ii) (11! â 10!)/9! We know that, n! = n(n-1)! Therefore, 11! = 11 Ã 10! =11 Ã 10 Ã 9! 10!
7 min read
Permutation In Mathematics, Permutation is defined as a mathematical concept that determines the number of possible arrangements for a specific set of elements. therefore, it plays a big role in computer science, cryptography, and operations research. For example, take a set {1, 2, 3}:All Permutations taking al
15+ min read
All permutations of a string using iteration A permutation, also called an âarrangement numberâ or âorderâ, is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation ( Source: Mathword ) Below are the permutations of string ABC. ABC ACB BAC BCA CBA CAB We hav
4 min read
Class 11 RD Sharma Solutions- Chapter 16 Permutations - Exercise 16.2 | Set 1 Question 1. In a class, there are 27 boys and 14 girls. The teacher wants to select 1 boy and 1 girl to represent the class in a function. In how many ways can the teacher make this selection? Solution: Given: Total number of boys = 27 Total number of girls = 14 So, ways to select a boy = 27 P1 = 27
8 min read
Circular Permutation Circular Permutation is an arrangement notion in which the objects are arranged in a closed loop. The beginning and end points are ambiguous, in contrast to linear layouts. A circular permutation is a configuration of items or components where the starting and ending positions are flexible. It entai
7 min read
Permutations and Combinations Permutation and Combination are the most fundamental concepts in mathematics related to picking items from a group or set. Permutation is arranging items considering the order of selection from a certain group.Combination is selecting items without considering order.For example, in the below diagram
14 min read