📌  相关文章
📜  求 [1, N] 的排列,使得 (arr[i] != i+1) 和 arr[i] 和 (i+1) 之间的绝对差之和最小

📅  最后修改于: 2022-05-13 01:56:07.560000             🧑  作者: Mango

求 [1, N] 的排列,使得 (arr[i] != i+1) 和 arr[i] 和 (i+1) 之间的绝对差之和最小

给定一个正整数N ,任务是找到前N个自然数的排列,比如arr[]使得(arr[i] != i + 1)以及arr[i](i + 1)最小值

例子:

朴素方法:解决给定问题的最简单方法是生成前N个自然数的所有可能排列,并打印满足给定标准的排列。

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

有效方法:上述方法也可以通过观察以下事实来优化:结果数组可以通过交换交替的相邻对来形成,以允许新位置具有arr[i](i + 1) 。如果N大于1N为奇数,则最后一个元素可以与排列的倒数第二个或倒数第三个元素中的任何一个交换。请按照以下步骤解决给定的问题:

  • 初始化一个数组,比如arr[] ,其中前 N 个自然数按升序排列。
  • 遍历数组并将所有相邻元素交换为swap ( arr[i], arr[i – 1])
  • 现在,如果N的值大于1并且N为奇数,则swap(arr[N – 1], arr[N – 2])
  • 完成上述步骤后,打印数组arr[]作为结果排列。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
// Function to generate the permutation
// of the first N natural numbers having
// sum of absolute difference between
// element and indices as minimum
#include 
using namespace std;
 
void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
 
void findPermutation(int N)
{
   
    // Initialize array arr[] from 1 to N
    int arr[N];
    for (int i = 0; i < N; i++) {
        arr[i] = i + 1;
    }
    for (int i = 1; i < N; i += 2) {
       
      // Swap alternate positions
        swap(arr[i], arr[i - 1]);
    }
   
  // Check N is greater than 1 and
    // N is odd
    if (N % 2 == 1 && N > 1) {
       
      // Swapping last two positions
        swap(arr[N - 1], arr[N - 2]);
    }
 
   // Print the permutation
    for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
    }
}
 
// Driver code
int main()
{
    int N = 7;
    findPermutation(N);
    return 0;
}
 
// This code is contributed by Parth Manchanda


Java
// Java program for the above approach
 
// Function to generate the permutation
// of the first N natural numbers having
// sum of absolute difference between
// element and indices as minimum
import java.util.*;
 
class GFG{
 
static void findPermutation(int N)
{
     
    // Initialize array arr[] from 1 to N
    int[] arr = new int[N];
    int temp;
     
    for(int i = 0; i < N; i++)
    {
        arr[i] = i + 1;
    }
    for(int i = 1; i < N; i += 2)
    {
         
        // Swap alternate positions
        temp = arr[i];
        arr[i] = arr[i - 1];
        arr[i - 1] = temp;
    }
 
    // Check N is greater than 1 and
    // N is odd
    if (N % 2 == 1 && N > 1)
    {
         
        // Swapping last two positions
        temp = arr[N - 1];
        arr[N - 1] = arr[N - 2];
        arr[N - 2] = temp;
    }
 
    // Print the permutation
    for(int i = 0; i < N; i++)
    {
        System.out.print(arr[i] + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 7;
     
    findPermutation(N);
}
}
 
// This code is contributed by subhammahato348


Python3
# Python3 program for the above approach
 
# Function to generate the permutation
# of the first N natural numbers having
# sum of absolute difference between
# element and indices as minimum
def findPermutation(N):
 
    # Initialize array arr[] from 1 to N
    arr = [i + 1 for i in range(N)]
 
    # Swap alternate positions
    for i in range(1, N, 2):
        arr[i], arr[i-1] = arr[i-1], arr[i]
 
    # Check N is greater than 1 and
    # N is odd
    if N % 2 and N > 1:
 
        # Swapping last two positions
        arr[-1], arr[-2] = arr[-2], arr[-1]
 
    # Print the permutation
    for i in arr:
        print(i, end = " ")
 
 
# Driver Code
if __name__ == '__main__':
 
    N = 7
    findPermutation(N)


C#
// C# program for the above approach
 
// Function to generate the permutation
// of the first N natural numbers having
// sum of absolute difference between
// element and indices as minimum
using System;
class GFG {
 
    static void findPermutation(int N)
    {
 
        // Initialize array arr[] from 1 to N
        int[] arr = new int[N];
        int temp;
        for (int i = 0; i < N; i++) {
            arr[i] = i + 1;
        }
        for (int i = 1; i < N; i += 2) {
 
            // Swap alternate positions
            temp = arr[i];
            arr[i] = arr[i - 1];
            arr[i - 1] = temp;
        }
 
        // Check N is greater than 1 and
        // N is odd
        if (N % 2 == 1 && N > 1) {
 
            // Swapping last two positions
            temp = arr[N - 1];
            arr[N - 1] = arr[N - 2];
            arr[N - 2] = temp;
        }
 
        // Print the permutation
        for (int i = 0; i < N; i++) {
            Console.Write(arr[i] + " ");
        }
    }
 
    // Driver code
    public static void Main()
    {
        int N = 7;
        findPermutation(N);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
2 1 4 3 6 7 5

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