Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers

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.
Examples:Â Â
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:Â Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to generate a permutation of integers// from 1 to N such that the absolute difference of// all the two consecutive integers give K distinct integersvoid printPermutation(int N, int K){    // To store the permutation    vector<int> res;Â
    int l = 1, r = N, flag = 0;Â
    for (int i = 0; i < K; i++) {Â
        if (!flag) {Â
            // For sequence 1 2 3...            res.push_back(l);            l++;        }        else {Â
            // For sequence N, N-1, N-2...            res.push_back(r);            r--;        }Â
        // Flag is used to alternate between        // the above if else statements        flag ^= 1;    }Â
    // Taking integers with difference 1Â
    // If last element added was r + 1    if (!flag) {        for (int i = r; i >= l; i--)            res.push_back(i);    }Â
    // If last element added was l - 1    else {        for (int i = l; i <= r; i++)            res.push_back(i);    }Â
    // Print the permutation    for (auto i : res)        cout << i << " ";}Â
// Driver codeint main(){Â Â Â Â int N = 10, K = 4;Â
    printPermutation(N, K);Â
    return 0;} |
Java
// Java implementation of the above approachimport java.util.Vector;Â
class GFG {    // Function to generate a permutation     // of integers from 1 to N such that the     // absolute difference of all the two     // consecutive integers give K distinct integers    static void printPermutation(int N, int K)     {        // To store the permutation        Vector<Integer> res = new Vector<>();Â
        int l = 1, r = N, flag = 0;Â
        for (int i = 0; i < K; i++)        {            if (flag == 0)             {                // For sequence 1 2 3...                res.add(l);                l++;            }             else            {                // For sequence N, N-1, N-2...                res.add(r);                r--;            }Â
            // Flag is used to alternate between            // the above if else statements            flag ^= 1;        }Â
        // Taking integers with difference 1        // If last element added was r + 1        if (flag != 1)         {            for (int i = r; i >= l; i--)             {                res.add(i);            }        }         // If last element added was l - 1        else        {            for (int i = l; i <= r; i++)             {                res.add(i);            }        }Â
        // Print the permutation        for (Integer i : res)         {            System.out.print(i + " ");        }    }Â
    // Driver code    public static void main(String[] args)    {        int N = 10, K = 4;        printPermutation(N, K);             }}Â
// This code is contributed by// 29AjayKumar |
Python3
# Python 3 implementation of the approachÂ
# Function to generate a permutation # of integers from 1 to N such that the # absolute difference of all the two # consecutive integers give K distinct# integersdef printPermutation(N, K):Â
    # To store the permutation    res = list();Â
    l, r, flag = 1, N, 0Â
    for i in range(K):Â
        if flag == False:                         # For sequence 1 2 3...            res.append(l)            l += 1             else:                         # For sequence N, N-1, N-2...            res.append(r);            r -= 1;Â
    # Flag is used to alternate between    # the above if else statements        flag = flag ^ 1;Â
    # Taking integers with difference 1Â
    # If last element added was r + 1    if flag == False:        for i in range(r, 2, -1):            res.append(i)Â
    # If last element added was l - 1    else:        for i in range(l, r):            res.append(i)Â
    # Print the permutation    for i in res:        print(i, end = " ")Â
# Driver codeN, K = 10, 4printPermutation(N, K)Â
# This code is contributed by# Mohit Kumar 29 |
C#
// C# implementation of the above approachusing System;using System.Collections;Â
class GFG {    // Function to generate a permutation     // of integers from 1 to N such that the     // absolute difference of all the two     // consecutive integers give K distinct integers    static void printPermutation(int N, int K)     {                 // To store the permutation        ArrayList res = new ArrayList();Â
        int l = 1, r = N, flag = 0;        for (int i = 0; i < K; i++)        {            if (flag == 0)             {                // For sequence 1 2 3...                res.Add(l);                l++;            }             else            {                // For sequence N, N-1, N-2...                res.Add(r);                r--;            }Â
            // Flag is used to alternate between            // the above if else statements            flag ^= 1;        }Â
        // Taking integers with difference 1        // If last element added was r + 1        if (flag != 1)         {            for (int i = r; i >= l; i--)             {                res.Add(i);            }        }                  // If last element added was l - 1        else        {            for (int i = l; i <= r; i++)             {                res.Add(i);            }        }Â
        // Print the permutation        foreach (int i in res)         {            Console.Write(i + " ");        }    }Â
    // Driver code    public static void Main()    {        int N = 10, K = 4;        printPermutation(N, K);           }}Â
// This code is contributed by PrinciRaj1992 |
PHP
<?php// PHP implementation of the approach Â
// Function to generate a permutation // of integers from 1 to N such that the // absolute difference of all the two // consecutive integers give K distinct // integers function printPermutation($N, $K) {          // To store the permutation     $res = array();Â
    $l = 1;    $r = $N;    $flag = 0; Â
    for ($i = 0; $i < $K; $i++)     {         if (!$flag)         { Â
            // For sequence 1 2 3...             array_push($res, $l);             $l++;         }         else        { Â
            // For sequence N, N-1, N-2...             array_push($res, $r);             $r--;         } Â
        // Flag is used to alternate between         // the above if else statements         $flag ^= 1;     } Â
    // Taking integers with difference 1 Â
    // If last element added was r + 1     if (!$flag)     {         for ($i = $r; $i >= $l; $i--)             array_push($res, $i);     } Â
    // If last element added was l - 1     else    {         for ($i = l; $i <= $r; $i++)             array_push($res, $i);     } Â
    // Print the permutation     for($i = 0; $i < sizeof($res); $i++)        echo $res[$i], " ";} Â
// Driver code $N = 10;$K = 4; Â
printPermutation($N, $K); Â
// This code is contributed by Ryuga?> |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to generate a permutation of // integers from 1 to N such that the // absolute difference of all the two // consecutive integers give K distinct integersfunction printPermutation(N, K){         // To store the permutation    var res = [];    var l = 1, r = N, flag = 0;Â
    for(var i = 0; i < K; i++)     {        if (!flag)         {                         // For sequence 1 2 3...            res.push(l);            l++;        }        else        {                         // For sequence N, N-1, N-2...            res.push(r);            r--;        }Â
        // Flag is used to alternate between        // the above if else statements        flag ^= 1;    }Â
    // Taking integers with difference 1Â
    // If last element added was r + 1    if (!flag)    {        for(var i = r; i >= l; i--)            res.push(i);    }Â
    // If last element added was l - 1    else    {        for(var i = l; i <= r; i++)            res.push(i);    }Â
    // Print the permutation    for(var i = 0; i< res.length; i++)    {        document.write(res[i] + " ");    }}Â
// Driver codevar N = 10, K = 4;Â
printPermutation(N, K);Â
// This code is contributed by noob2000Â
</script> |
1 10 2 9 8 7 6 5 4 3
Â
Time Complexity : O(K+N)
Space Complexity : O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



