Dada una array arr[] que consta de números enteros en el rango [1, N] , la tarea es determinar si la permutación inversa de la array dada es la misma que la array dada.
Una permutación inversa es una permutación obtenida al insertar la posición de todos los elementos en la posición igual a los valores respectivos del elemento en la array.
Ilustración:
arr[] = {2, 4, 1, 3, 5}
La permutación inversa de la array será igual a {3, 1, 4, 2, 5}
Ejemplos:
Entrada: N = 4, arr[] = {1, 4, 3, 2}
Salida: Sí
Explicación:
La permutación inversa de la array dada es {1, 4, 3, 2} que es igual a la array dada.
Entrada: N = 5, arr[] = {2, 3, 4, 5, 1}
Salida: No
Explicación:
La permutación inversa de la array dada es {5, 1, 2, 3, 4} que no es lo mismo como la array dada.
Método 1 :
En este método, generaremos la permutación inversa de la array, luego verificaremos si es igual a la array original.
Siga los pasos a continuación para resolver el problema:
- Encuentre la permutación inversa de la array dada.
- Verifique si la array generada es la misma que la array original.
- Si ambos son iguales, imprima Sí . De lo contrario , imprima No.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to implement // the above approach #include <iostream> using namespace std; // Function to check if the inverse // permutation of the given array is // same as the original array void inverseEqual(int arr[], int n) { // Stores the inverse // permutation int brr[n]; // Generate the inverse permutation for (int i = 0; i < n; i++) { int present_index = arr[i] - 1; brr[present_index] = i + 1; } // Check if the inverse permutation // is same as the given array for (int i = 0; i < n; i++) { if (arr[i] != brr[i]) { cout << "No" << endl; return; } } cout << "Yes" << endl; } // Driver Code int main() { int n = 4; int arr[n] = { 1, 4, 3, 2 }; inverseEqual(arr, n); return 0; }
Java
// Java program to implement // the above approach import java.util.*; class GFG{ // Function to check if the inverse // permutation of the given array is // same as the original array static void inverseEqual(int arr[], int n) { // Stores the inverse // permutation int[] brr = new int[n]; // Generate the inverse permutation for(int i = 0; i < n; i++) { int present_index = arr[i] - 1; brr[present_index] = i + 1; } // Check if the inverse permutation // is same as the given array for(int i = 0; i < n; i++) { if (arr[i] != brr[i]) { System.out.println("No"); return; } } System.out.println("Yes"); } // Driver code public static void main(String[] args) { int n = 4; int[] arr = { 1, 4, 3, 2 }; inverseEqual(arr, n); } } // This code is contributed by offbeat
Python3
# Python3 program to implement # the above approach # Function to check if the inverse # permutation of the given array is # same as the original array def inverseEqual(arr, n): # Stores the inverse # permutation brr = [0] * n # Generate the inverse permutation for i in range(n): present_index = arr[i] - 1 brr[present_index] = i + 1 # Check if the inverse permutation # is same as the given array for i in range(n): if arr[i] != brr[i]: print("NO") return print("YES") # Driver code n = 4 arr = [ 1, 4, 3, 2 ] inverseEqual(arr, n) # This code is contributed by Stuti Pathak
C#
// C# program to implement // the above approach using System; class GFG{ // Function to check if the inverse // permutation of the given array is // same as the original array static void inverseEqual(int []arr, int n) { // Stores the inverse // permutation int[] brr = new int[n]; // Generate the inverse permutation for(int i = 0; i < n; i++) { int present_index = arr[i] - 1; brr[present_index] = i + 1; } // Check if the inverse permutation // is same as the given array for(int i = 0; i < n; i++) { if (arr[i] != brr[i]) { Console.WriteLine("No"); return; } } Console.WriteLine("Yes"); } // Driver code public static void Main(String[] args) { int n = 4; int[] arr = { 1, 4, 3, 2 }; inverseEqual(arr, n); } } // This code is contributed by sapnasingh4991
Javascript
<script> // Javascript Program to implement // the above approach // Function to check if the inverse // permutation of the given array is // same as the original array function inverseEqual(arr, n) { // Stores the inverse // permutation var brr = Array(n).fill(0); // Generate the inverse permutation for (var i = 0; i < n; i++) { var present_index = arr[i] - 1; brr[present_index] = i + 1; } // Check if the inverse permutation // is same as the given array for (var i = 0; i < n; i++) { if (arr[i] != brr[i]) { document.write( "No" ); return; } } document.write( "Yes" ); } // Driver Code var n = 4; var arr = [ 1, 4, 3, 2 ]; inverseEqual(arr, n); // This code is contributed by noob2000. </script>
Yes
Complejidad temporal: O(N)
Espacio auxiliar: O(N)
Método 2:
En este método, tomamos los elementos uno por uno y verificamos si en (arr[i] -1) el índice i+1 está presente o no . De lo contrario, la permutación inversa de la array dada no es la misma que la array.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to check if the inverse // permutation of the given array is // same as the original array bool inverseEqual(int arr[], int n) { // Check the if inverse permutation is not same for (int i = 0; i < n; i++) if (arr[arr[i] - 1] != i + 1) return false; return true; } // Driver Code int main() { int n = 4; int arr[n] = { 1, 4, 3, 2 }; // Function Call cout << (inverseEqual(arr, n) ? "Yes" : "No"); return 0; }
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { // Java program to implement // the above approach // Function to check if the inverse // permutation of the given array is // same as the original array static boolean inverseEqual(int[] arr,int n){ // Check the if inverse permutation // is not same for(int i=0;i<n;i++){ if (arr[arr[i] - 1] != i + 1) return false; } return true; } // Driver code public static void main(String args[]) { int n = 4; int[] arr = { 1, 4, 3, 2 }; // Function Call System.out.println((inverseEqual(arr, n)==true)?"Yes" : "No"); } } // This Code is contributed by shinjanpatra
Python3
# Python program to implement # the above approach # Function to check if the inverse # permutation of the given array is # same as the original array def inverseEqual(arr, n): # Check the if inverse permutation # is not same for i in range(n): if (arr[arr[i] - 1] != i + 1): return False return True # Driver Code n = 4 arr = [ 1, 4, 3, 2 ] # Function Call print("Yes" if (inverseEqual(arr, n)==True) else "No") # This code is contributed by shinjanpatra
Javascript
<script> // Javascript program to implement // the above approach // Function to check if the inverse // permutation of the given array is // same as the original array function inverseEqual(arr, n) { // Check the if inverse permutation // is not same for (let i = 0; i < n; i++) if (arr[arr[i] - 1] != i + 1) return false; return true; } // Driver Code let n = 4; let arr = [ 1, 4, 3, 2 ]; // Function Call document.write(inverseEqual(arr, n) ? "Yes" : "No"); </script>
Yes
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por thakurabhaysingh445 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA