📜  逆排列

📅  最后修改于: 2021-06-25 15:40:01             🧑  作者: Mango

给定一个大小为n的整数数组,其范围为1到n,我们需要找到该数组的逆排列。
反向排列是通过在数组中元素值指定的位置插入元素的位置而获得的排列。为了更好地理解,请考虑以下示例:
假设我们在数组中的位置3找到了元素4,然后在反向置换中,在位置4(元素值)中插入了3(元素4在数组中的位置)。
基本上,反向排列是一种排列,其中每个数字和它所占据的位置的数字都可以交换。
数组应包含从1到array_size的元素。
范例1:

Input  = {1, 4, 3, 2}
Output = {1, 4, 3, 2}

在此,对于元素1,我们从arr1插入位置1,即在arr2的位置1处插入1。对于arr1中的元素4,我们将arr1中的2插入arr2中的位置4。类似地,对于arr1中的元素2,我们在arr2中插入位置2,即4。

Example 2 :
Input  = {2, 3, 4, 5, 1}
Output = {5, 1, 2, 3, 4}

在此示例中,对于元素2,我们将arr1中2的位置插入位置2处的arr2中。类似地,我们发现其他元素的逆排列。
考虑具有元素1到n的数组arr。
方法1:
在此方法中,我们一个接一个地处理元素,并按升序检查元素,并在找到该元素的位置打印该元素的位置。

C++
// Naive CPP Program to find inverse permutation.
#include 
using namespace std;
 
// C++ function to find inverse permutations
void inversePermutation(int arr[], int size) {
 
  // Loop to select Elements one by one
  for (int i = 0; i < size; i++) {
 
    // Loop to print position of element
    // where we find an element
    for (int j = 0; j < size; j++) {
 
      // checking the element in increasing order
      if (arr[j] == i + 1) {
 
        // print position of element where
        // element is in inverse permutation
        cout << j + 1 << " ";
        break;
      }
    }
  }
}
 
// Driver program to test above function
int main() {
  int arr[] = {2, 3, 4, 5, 1};
  int size = sizeof(arr) / sizeof(arr[0]);
  inversePermutation(arr, size);
  return 0;
}


Java
// Naive java Program to find inverse permutation.
import java.io.*;
 
class GFG {
 
    // java function to find inverse permutations
    static void inversePermutation(int arr[], int size)
    {
        int i ,j;
        // Loop to select Elements one by one
        for ( i = 0; i < size; i++)
        {
         
            // Loop to print position of element
            // where we find an element
            for ( j = 0; j < size; j++)
            {
         
                // checking the element in
                // increasing order
                if (arr[j] == i + 1)
                {
                    // print position of element
                    // where element is in inverse
                    // permutation
                    System.out.print( j + 1 + " ");
                    break;
                }
            }
        }
    }
     
    // Driver program to test above function
     
     
    public static void main (String[] args)
    {
        int arr[] = {2, 3, 4, 5, 1};
        int size = arr.length;
        inversePermutation(arr, size);
         
    }
}
 
// This code is contributed by vt_m


Python3
# Naive Python3 Program to
# find inverse permutation.
 
# Function to find inverse permutations
def inversePermutation(arr, size):
 
    # Loop to select Elements one by one
    for i in range(0, size):
 
        # Loop to print position of element
        # where we find an element
        for j in range(0, size):
 
        # checking the element in increasing order
            if (arr[j] == i + 1):
 
                # print position of element where
                # element is in inverse permutation
                print(j + 1, end = " ")
                break
 
# Driver Code
arr = [2, 3, 4, 5, 1]
size = len(arr)
 
inversePermutation(arr, size)
 
#This code is cotributed by Smitha Dinesh Semwal


C#
// Naive C# Program to find inverse permutation.
using System;
 
class GFG {
 
    // java function to find inverse permutations
    static void inversePermutation(int []arr, int size)
    {
        int i ,j;
        // Loop to select Elements one by one
        for ( i = 0; i < size; i++)
        {
         
            // Loop to print position of element
            // where we find an element
            for ( j = 0; j < size; j++)
            {
         
                // checking the element in
                // increasing order
                if (arr[j] == i + 1)
                {
                    // print position of element
                    // where element is in inverse
                    // permutation
                    Console.Write( j + 1 + " ");
                    break;
                }
            }
        }
    }
     
    // Driver program to test above function
     
     
    public static void Main ()
    {
        int []arr = {2, 3, 4, 5, 1};
        int size = arr.Length;
        inversePermutation(arr, size);
         
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


C++
// Efficient CPP Program to find inverse permutation.
#include 
using namespace std;
 
// C++ function to find inverse permutations
void inversePermutation(int arr[], int size) {
 
  // to store element to index mappings
  int arr2[size];
 
  // Inserting position at their
  // respective element in second array
  for (int i = 0; i < size; i++)
    arr2[arr[i] - 1] = i + 1;
 
  for (int i = 0; i < size; i++)
    cout << arr2[i] << " "; 
}
 
// Driver program to test above function
int main() {
  int arr[] = {2, 3, 4, 5, 1};
  int size = sizeof(arr) / sizeof(arr[0]);
  inversePermutation(arr, size);
  return 0;
}


Java
// Efficient Java Program to find
// inverse permutation.
import java.io.*;
 
class GFG {
     
// function to find inverse permutations
static void inversePermutation(int arr[], int size) {
 
    // to store element to index mappings
    int arr2[] = new int[size];
 
    // Inserting position at their
    // respective element in second array
    for (int i = 0; i < size; i++)
    arr2[arr[i] - 1] = i + 1;
 
    for (int i = 0; i < size; i++)
    System.out.print(arr2[i] + " ");
}
 
// Driver program to test above function
public static void main(String args[]) {
    int arr[] = {2, 3, 4, 5, 1};
    int size = arr.length;
    inversePermutation(arr, size);
}
}
 
// This code is contributed by Nikita Tiwari.


Python3
# Efficient Python 3 Program to find
# inverse permutation.
 
# function to find inverse permutations
def inversePermutation(arr, size) :
 
    # To store element to index mappings
    arr2 = [0] *(size)
     
    # Inserting position at their
    # respective element in second array
    for i in range(0, size) :
        arr2[arr[i] - 1] = i + 1
     
    for i in range(0, size) :
        print( arr2[i], end = " ")
     
# Driver program
arr = [2, 3, 4, 5, 1]
size = len(arr)
 
inversePermutation(arr, size)
 
# This code is contributed by Nikita Tiwari.


C#
// Efficient C# Program to find
// inverse permutation.
using System;
 
class GFG {
     
// function to find inverse permutations
static void inversePermutation(int []arr, int size) {
 
    // to store element to index mappings
    int []arr2 = new int[size];
 
    // Inserting position at their
    // respective element in second array
    for (int i = 0; i < size; i++)
    arr2[arr[i] - 1] = i + 1;
 
    for (int i = 0; i < size; i++)
    Console.Write(arr2[i] + " ");
}
 
// Driver program to test above function
public static void Main() {
    int []arr = {2, 3, 4, 5, 1};
    int size = arr.Length;
    inversePermutation(arr, size);
}
}
 
// This code is contributed by vt_m.


输出 :

5 1 2 3 4

方法2:
这个想法是使用另一个数组来存储索引和元素映射

C++

// Efficient CPP Program to find inverse permutation.
#include 
using namespace std;
 
// C++ function to find inverse permutations
void inversePermutation(int arr[], int size) {
 
  // to store element to index mappings
  int arr2[size];
 
  // Inserting position at their
  // respective element in second array
  for (int i = 0; i < size; i++)
    arr2[arr[i] - 1] = i + 1;
 
  for (int i = 0; i < size; i++)
    cout << arr2[i] << " "; 
}
 
// Driver program to test above function
int main() {
  int arr[] = {2, 3, 4, 5, 1};
  int size = sizeof(arr) / sizeof(arr[0]);
  inversePermutation(arr, size);
  return 0;
}

Java

// Efficient Java Program to find
// inverse permutation.
import java.io.*;
 
class GFG {
     
// function to find inverse permutations
static void inversePermutation(int arr[], int size) {
 
    // to store element to index mappings
    int arr2[] = new int[size];
 
    // Inserting position at their
    // respective element in second array
    for (int i = 0; i < size; i++)
    arr2[arr[i] - 1] = i + 1;
 
    for (int i = 0; i < size; i++)
    System.out.print(arr2[i] + " ");
}
 
// Driver program to test above function
public static void main(String args[]) {
    int arr[] = {2, 3, 4, 5, 1};
    int size = arr.length;
    inversePermutation(arr, size);
}
}
 
// This code is contributed by Nikita Tiwari.

Python3

# Efficient Python 3 Program to find
# inverse permutation.
 
# function to find inverse permutations
def inversePermutation(arr, size) :
 
    # To store element to index mappings
    arr2 = [0] *(size)
     
    # Inserting position at their
    # respective element in second array
    for i in range(0, size) :
        arr2[arr[i] - 1] = i + 1
     
    for i in range(0, size) :
        print( arr2[i], end = " ")
     
# Driver program
arr = [2, 3, 4, 5, 1]
size = len(arr)
 
inversePermutation(arr, size)
 
# This code is contributed by Nikita Tiwari.

C#

// Efficient C# Program to find
// inverse permutation.
using System;
 
class GFG {
     
// function to find inverse permutations
static void inversePermutation(int []arr, int size) {
 
    // to store element to index mappings
    int []arr2 = new int[size];
 
    // Inserting position at their
    // respective element in second array
    for (int i = 0; i < size; i++)
    arr2[arr[i] - 1] = i + 1;
 
    for (int i = 0; i < size; i++)
    Console.Write(arr2[i] + " ");
}
 
// Driver program to test above function
public static void Main() {
    int []arr = {2, 3, 4, 5, 1};
    int size = arr.Length;
    inversePermutation(arr, size);
}
}
 
// This code is contributed by vt_m.
Output : 5 1 2 3 4