📌  相关文章
📜  生成前 N 个自然数的排列,其唯一相邻差异的计数等于 K

📅  最后修改于: 2021-09-04 11:40:25             🧑  作者: Mango

给定两个正整数NK ,任务是构造前N 个自然数的排列,使得相邻元素之间所有可能的绝对差为K

例子:

朴素的方法:解决给定问题的最简单的方法是创建一个数组,其中元素从1N按升序排列,然后遍历数组的前 K 个元素并反转子数组,从当前索引开始到最后一个结束指数。完成上述步骤后,打印得到的结果数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to reverse the given list
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--;
    }
}
 
// Function to construct a list with
// exactly K unique adjacent element
// differences
void makeList(int N, int K)
{
     
    // Stores the resultant array
    int list[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 < N; i++)
    {
        cout << list[i] << " ";
    }
}
 
// Driver code
int main()
{
    int N = 6, K = 3;
    makeList(N, K);
     
    return 0;
}
 
// This code is contributed by mohit kumar 29


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);
    }
}


Python3
# Python 3 program for the above approach
 
# Function to reverse the given lst
def reverse(lst, start,  end):
   
    # Iterate until start < end
    while (start < end):
       
        # Swap operation
        temp = lst[start]
        lst[start] = lst[end]
        lst[end] = temp
 
        start += 1
        end -= 1
 
# Function to construct a lst with
# exactly K unique adjacent element
# differences
def makelst(N, K):
   
    # Stores the resultant array
    lst = [0 for i in range(N)]
     
    # Add initial value to array
    for i in range(1, N + 1, 1):
        lst[i - 1] = i
 
    # Reverse the lst k-1 times
    # from index i to n-1
    for i in range(1, K, 1):
        reverse(lst, i, N - 1)
 
    # Print the resultant array
    for i in range(N):
        print(lst[i], end = " ")
 
# Driver code
if __name__ == '__main__':
    N = 6
    K = 3
    makelst(N, K)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
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++)
    {
        Console.Write(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
static public void Main()
{
    int N = 6, K = 3;
     
    makeList(N, K);
}
}
 
// This code is contributed by Dharanendra L V.


Javascript


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);
    }
}


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
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++)
    {
        Console.Write(list[i] + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 6;
    int K = 3;
     
    makeList(N, K);
}
}
 
// This code is contributed by 29AjayKumar


输出:
1 6 2 3 4 5

时间复杂度: O(N 2 )
辅助空间: O(N)

高效的方法:上述方法也可以通过使用两点方法进行优化。请按照以下步骤解决问题:

  • 初始化一个大小为N的数组ans[] ,用于存储结果排列。
  • 创建两个变量,分别说leftright1N
  • 遍历给定数组并执行以下步骤:
    • 如果K 的值为偶数,则将left的值推入数组ans[]并将left的值增加1
    • 如果K 的值为奇数,则将右边的值推入 到数组ans[]并将right的值减1
    • 如果 K的值大于 1,则将K的值减1
  • 完成上述步骤后,打印数组ans[]

下面是上述方法的实现:

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);
    }
}

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
 
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++)
    {
        Console.Write(list[i] + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 6;
    int K = 3;
     
    makeList(N, K);
}
}
 
// This code is contributed by 29AjayKumar
输出:
6 1 5 4 3 2

时间复杂度: O(N)
辅助空间: O(N)