- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two integers N and K. We have to find first permutation of 2N number of natural numbers, such that the following equation is satisfied.

$$\displaystyle\sum\limits_{i=1}^N\lvert A_{2i-1}-A_{2i}\rvert+\lvert \displaystyle\sum\limits_{i=1}^N A_{2i-1}-A_{2i} \rvert=2K$$

The value of K should be less than or equal to N. For example, if N = 4 and K = 1, then output will be 2 1 3 4. The result of the given expression will be (|2 – 1| + |3 – 4|) – (|2 – 1 + 3 – 4|) = 2.

The idea is simple, consider we have a sorted sequence like 1, 2, 3, 4, 5, 6, …. If we swap any two indices 2i – 1 and 2i, the result will increase by exactly 2. We need to make K such swaps.

#include<iostream> using namespace std; void showPermutations(int n, int k) { for (int i = 1; i <= n; i++) { int a = 2 * i - 1; int b = 2 * i; if (i <= k) cout << b << " " << a << " "; else cout << a << " " << b << " "; } } int main() { int n = 4, k = 2; showPermutations(n, k); }

2 1 4 3 5 6 7 8

- Related Questions & Answers
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K in C++
- Find permutation of first N natural numbers that satisfies the given condition in C++
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python
- Find the largest interval that contains exactly one of the given N integers In C++
- Find smallest permutation of given number in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Maximum sum of distinct numbers such that LCM of these numbers is N in C++
- Find a point such that sum of the Manhattan distances is minimize in C++
- Find a permutation that causes worst case of Merge Sort in C
- Find a permutation that causes worst case of Merge Sort in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in C++
- Find all possible outcomes of a given expression in C++
- Find a number x such that sum of x and its digits is equal to given n in C++
- Find numbers a and b that satisfy the given condition in C++
- Find a number x such that sum of x and its digits is equal to given n using C++.

Advertisements