CSES Solutions – Permutations
Last Updated :
15 Mar, 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
import java.util.*;
public class Main {
// 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) {
System.out.println("NO SOLUTION");
return;
}
// Output all even numbers first
for (int i = 2; i <= N; i += 2) {
// Print even numbers with a step of 2
System.out.print(i + " ");
}
// Output all odd numbers next
for (int i = 1; i <= N; i += 2) {
// Print odd numbers with a step of 2
System.out.print(i + " ");
}
}
// Driver Code
public static void main(String[] args) {
int N = 5;
// Call the permutation function with input N
permutation(N);
}
}
Python
def GFG(N):
if N == 2 or N == 3:
print "NO SOLUTION"
return
# Output all even numbers first
for i in range(2, N + 1, 2):
# Print even numbers with the step of 2
print i,
# Output all odd numbers next
for i in range(1, N + 1, 2):
# Print odd numbers with the step of 2
print i,
# Driver Code
if __name__ == "__main__":
N = 5
# Call the permutation function with the input N
GFG(N)
C#
using System;
public class Program
{
// 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)
{
// Print even numbers with a step of 2
Console.Write(i + " ");
}
// Output all odd numbers next
for (int i = 1; i <= N; i += 2)
{
// Print odd numbers with a step of 2
Console.Write(i + " ");
}
}
// Main method
public static void Main(string[] args)
{
int N = 5;
// Call the permutation function with input N
Permutation(N);
}
}
Javascript
// Function to construct a beautiful permutation
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;
}
// used variable result here to get the output in a single line
let result = '';
// Output all even numbers first
for (let i = 2; i <= N; i += 2) {
// Concatenate even numbers with a space
result += i + " ";
}
// Output all odd numbers next
for (let i = 1; i <= N; i += 2) {
// Concatenate odd numbers with a space
result += i + " ";
}
// Print the result
console.log(result);
}
// Driver Code
let 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)
Share your thoughts in the comments
Please Login to comment...