Given two integers N and K where K < N, the task is to generate a permutation of integers from 1 to N such that the absolute difference of all the consecutive integers give exactly K distinct integers.
Input: N = 3, K = 2
Output: 1 3 2
|1 – 3| = 2 and |3 – 2| = 1 which gives 2 distinct integers (2 and 1)
Input: N = 5, K = 4
Output: 1 5 2 4 3
|1 – 5| = 4, |5 – 2| = 3, |2 – 4| = 2 and |4 – 3| = 1 gives 4 distinct integers i.e. 4, 3, 2 and 1
Approach: The problem can be easily solved by simple observation. At the odd indices place increasing sequence 1, 2, 3, … and at the even indices place the decreasing sequence N, N-1, N-2, … and so on.
For N = 10, a permutation with distinct integers for consecutive absolute difference can be 1 10 2 9 3 8 4 7 5 6. The consecutive absolute difference gives integers 9, 8, 7 and so on.
So, first print K integers of such a sequence then make the rest of the differences equal to 1. The code is quite self explanatory.
Below is the implementation of the above approach:
1 10 2 9 8 7 6 5 4 3
- Print combinations of distinct numbers which add up to give sum N
- Permutation of first N positive integers such that prime numbers are at prime indices
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference
- Check if X can give change to every person in the Queue
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Remove Minimum coins such that absolute difference between any two piles is less than K
- Count pairs in an array such that the absolute difference between them is ≥ K
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Generate a String of having N*N distinct non-palindromic Substrings
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Find two distinct numbers such that their LCM lies in given range
- Pair of prime numbers with a given sum and minimum absolute difference
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.