- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the good permutation of first N natural numbers C++
In this problem, we are an integer value N. Our task is to find the good permutation of first N natural numbers.
permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement.
Good Permutation is a permutation in which $1\leqslant{i}\leqslant{N}$ and follows,
$P_{pi}\:=\:i$
$P_{p!}\:=\:i$
Let's take an example to understand the problem,
Input : N = 1 Output : -1
Solution Approach
A simple solution to the problem is by finding permutations p such that pi = i.
Then we will reconsider the equation to satisfy pi != i. So, for a value x such that $2x \leqslant x$, we have p2x - 1 and p2k. Now, we have an equation that satisfies the permutation equation for n. Here, the solution for the equation.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; void printGoodPermutation(int n) { if (n % 2 != 0) cout<<-1; else for (int i = 1; i <= n / 2; i++) cout<<(2*i)<<"\t"<<((2*i) - 1)<<"\t"; } int main() { int n = 4; cout<<"Good Permutation of first N natural Numbers : \n"; printGoodPermutation(n); return 0; }
Output
Good Permutation of first N natural Numbers : 2 1 4 3
- Related Articles
- Find permutation of first N natural numbers that satisfies the given condition in C++
- Find the average of first N natural numbers in C++
- Program to find number of magic sets from a permutation of first n natural numbers in Python
- Find the sum of first $n$ odd natural numbers.
- Program to find sum of first n natural numbers in C++
- Find m-th summation of first n natural numbers in C++
- Sum of first n natural numbers in C Program
- 8085 program to find the sum of first n natural numbers
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python
- Sum of sum of first n natural numbers in C++
- Average of first n even natural numbers?
- C Program for the cube sum of first n natural numbers?
- Find if given number is sum of first n natural numbers in C++
- C Program for cube sum of first n natural numbers?
- C++ Program for cube sum of first n natural numbers?

Advertisements