The Wayback Machine - https://web.archive.org/web/20210526122725/https://www.geeksforgeeks.org/generate-a-permutation-of-first-n-natural-numbers-having-count-of-unique-adjacent-differences-equal-to-k/
Skip to content
Related Articles

Related Articles

Generate a permutation of first N natural numbers having count of unique adjacent differences equal to K
  • Last Updated : 26 May, 2021

Given two positive integers N and K, the task is to construct a permutation of the first N natural numbers such that all possible absolute differences between adjacent elements is K.

Examples:

Input: N = 3, K = 1
Output: 1 2 3
Explanation: Considering the permutation {1, 2, 3}, all possible unique absolute difference of adjacent elements is {1}. Since the count is 1(= K), print the sequence {1, 2, 3} as the resultant permutation.

Input: N = 3, K = 2
Output: 1 3 2

Naive Approach: The simplest approach to solve the given problem is to create an array with elements from 1 to N arranged in ascending order and then traverse the first K elements of the array and reverse the subarray starting at the current index and ending at the last index. After completing the above steps, print the resultant array obtained.



Below is the implementation of the above approach:

Java




// Java program for the above approach
  
class GFG {
  
    // Function to construct a list with
    // exactly K unique adjacent element
    // differences
    public static void makeList(int N, int K)
    {
        // Stores the resultant array
        int[] list = new int[N];
  
        // Add initial value to array
        for (int i = 1; i <= N; i++) {
            list[i - 1] = i;
        }
  
        // Reverse the list k-1 times
        // from index i to n-1
        for (int i = 1; i < K; i++) {
            reverse(list, i, N - 1);
        }
  
        // Print the resultant array
        for (int i = 0;
             i < list.length; i++) {
            System.out.print(list[i] + " ");
        }
    }
  
    // Function to reverse the given list
    public static void reverse(
        int[] list, int start, int end)
    {
        // Iterate until start < end
        while (start < end) {
  
            // Swap operation
            int temp = list[start];
            list[start] = list[end];
            list[end] = temp;
  
            start++;
            end--;
        }
    }
  
    // Driver Code
    public static void main(
        String[] args)
    {
        int N = 6, K = 3;
        makeList(N, K);
    }
}
Output:
1 6 2 3 4 5

Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: The above approach can also be optimized by using the two-pointer approach. Follow the steps below to solve the problem:

  • Initialize an array ans[] of size N, that stores the resultant permutation.
  • Create two variables, say left and right as 1 and N respectively.
  • Traverse the given array and perform the following steps:
    • If the value of K is even, then push the value of the left to the array ans[] and increment the value of left by 1.
    • If the value of K is odd, then push the value of the right to the array ans[] and decrement the value of right by 1.
    • If the value of K is greater than 1, then decrement the value of K by 1.
  • After completing the above steps, print the array ans[].

Below is the implementation of the above approach:

Java




// Java program for the above approach
  
class GFG {
  
    // Function to construct the list
    // with exactly K unique adjacent
    // element differences
    public static void makeList(int N,
                                int K)
    {
        // Stores the resultant array
        int[] list = new int[N];
  
        // Stores the left and the right
        // most element of the range
        int left = 1;
        int right = N;
  
        // Traverse the array
        for (int i = 0; i < N; i++) {
  
            // If k is even, the add
            // left to array and
            // increment the left
            if (K % 2 == 0) {
                list[i] = left;
                left = left + 1;
            }
  
            // If k is odd, the add
            // right to array and
            // decrement the right
            else {
                list[i] = right;
                right = right - 1;
            }
  
            // Repeat the steps for
            // k-1 times
            if (K > 1)
                K--;
        }
  
        // Print the resultant list
        for (int i = 0;
             i < list.length; i++) {
            System.out.print(
                list[i] + " ");
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int N = 6;
        int K = 3;
        makeList(N, K);
    }
}
Output:
6 1 5 4 3 2

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :