Dadas dos arrays A y B , cada una de tamaño N , la tarea es verificar si la secuencia de ambas arrays es la misma o no. Si la secuencia de ambas arrays es la misma, imprime Sí ; de lo contrario, imprime No.
Ejemplos:
Entrada: A[] = { 10, 12, 9, 11 }, B[] = { 2, 7, -3, 5 };
Salida: Sí
Explicación: En ambas arrays, el segundo elemento es mayor que el primero.
El tercer elemento es menor que el segundo y el último elemento es mayor que el tercero.Entrada: A[] = { 1, 2, 3, 4 }, B[] = { 1, 3, 2, 4 };
Salida: Sí
Enfoque: siga los pasos a continuación para resolver este problema:
- Cree un vector de pares, diga arr[] e inserte los elementos de A y B en él.
- Cada elemento del vector arr, es decir, arr[i] es de tipo {A[i], B[i]}.
- Ahora, ordene este vector arr, según el primer elemento.
- Después de ordenar, verifique si el segundo elemento de cada par en arr debe ser parte de la secuencia ordenada.
arr[i-1].segundo < arr[i].segundo, para cada i.
- Si es así, imprima Sí , de lo contrario imprima No.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if the sequencing // of both the arrays is the same or not bool sameOrder(vector<int>& A, vector<int>& B) { int N = A.size(); vector<pair<int, int> > arr(N); for (int i = 0; i < N; ++i) { arr[i] = { A[i], B[i] }; } sort(arr.begin(), arr.end()); // Check if the second element // of each pair in arr // is a part of the sorted sequence for (int i = 1; i < N; ++i) { if (arr[i - 1].second > arr[i].second) { return false; } } return true; } // Driver Code int main() { vector<int> A = { 10, 12, 9, 11 }; vector<int> B = { 2, 7, -3, 5 }; if (sameOrder(A, B)) { cout << "Yes"; } else { cout << "No"; } return 0; }
Java
// Java code for the above approach import java.util.*; class GFG{ static class pair implements Comparable<pair> { int first,second; pair(int s, int e) { first = s; second = e; } public int compareTo(pair p) { return this.first - p.first; } } // Function to check if the sequencing // of both the arrays is the same or not static boolean sameOrder(int []A, int []B) { int N = A.length; pair[] arr = new pair[N]; for (int i = 0; i < N; ++i) { arr[i] = new pair( A[i], B[i] ); } Arrays.sort(arr); // Check if the second element // of each pair in arr // is a part of the sorted sequence for (int i = 1; i < N; ++i) { if (arr[i - 1].second > arr[i].second) { return false; } } return true; } // Driver Code public static void main(String[] args) { int []A = { 10, 12, 9, 11 }; int []B = { 2, 7, -3, 5 }; if (sameOrder(A, B)) { System.out.print("Yes"); } else { System.out.print("No"); } } } // This code is contributed by shikhasingrajput
Python3
# Python 3 code for the above approach # Function to check if the sequencing # of both the arrays is the same or not def sameOrder(A, B): N = len(A) arr = [] for i in range(N): arr.append([A[i], B[i]]) arr.sort() # Check if the second element # of each pair in arr # is a part of the sorted sequence for i in range(1, N): if (arr[i - 1][1] > arr[i][1]): return False return True # Driver Code if __name__ == "__main__": A = [10, 12, 9, 11] B = [2, 7, -3, 5] if (sameOrder(A, B)): print("Yes") else: print("No") # This code is contributed by ukasp.
C#
// C# code for the above approach using System; public class GFG{ class pair : IComparable<pair> { public int first, second; public pair(int first, int second) { this.first = first; this.second = second; } public int CompareTo(pair p) { return this.first-p.first; } } // Function to check if the sequencing // of both the arrays is the same or not static bool sameOrder(int []A, int []B) { int N = A.Length; pair[] arr = new pair[N]; for (int i = 0; i < N; ++i) { arr[i] = new pair( A[i], B[i] ); } Array.Sort(arr); // Check if the second element // of each pair in arr // is a part of the sorted sequence for (int i = 1; i < N; ++i) { if (arr[i - 1].second > arr[i].second) { return false; } } return true; } // Driver Code public static void Main(String[] args) { int []A = { 10, 12, 9, 11 }; int []B = { 2, 7, -3, 5 }; if (sameOrder(A, B)) { Console.Write("Yes"); } else { Console.Write("No"); } } } // This code contributed by shikhasingrajput
Javascript
<script> // JavaScript code for the above approach // Function to check if the sequencing // of both the arrays is the same or not const sameOrder = (A, B) => { let N = A.length; let arr = []; for (let i = 0; i < N; ++i) { arr.push([A[i], B[i]]); } arr.sort((a, b) => a[0] - b[0]); // Check if the second element // of each pair in arr // is a part of the sorted sequence for (let i = 1; i < N; ++i) { if (arr[i - 1][1] > arr[i][1]) { return false; } } return true; } // Driver Code let A = [10, 12, 9, 11]; let B = [2, 7, -3, 5]; if (sameOrder(A, B)) { document.write("Yes"); } else { document.write("No"); } // This code is contributed by rakeshsahni </script>
Yes
Complejidad temporal: O(N * logN)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por sauarbhyadav y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA