Dadas dos arrays arr1[] y arr2[] , ambas de tamaño N y un número entero X , la tarea es verificar si la suma de los elementos del mismo índice de ambas arrays en los índices correspondientes se puede hacer como máximo K después de reorganizar la segunda formación. Si es posible, escriba «Sí» , de lo contrario, escriba «No» .
Ejemplos:
Entrada: arr1[] = {1, 2, 3}, arr2[] = {1, 1, 2}, X = 4
Salida: Sí
Explicación:
Reorganizar la array B[] como {1, 2, 1}. Ahora la suma de los índices correspondientes son:
A[0] + B[0] = 1 + 1 = 2 ≤ 4
A[1] + B[1] = 2 + 2 = 4 ≤ 4
A[2] + B[2 ] = 3 + 1 = 4 ≤ 4Entrada: arr1[] = {1, 2, 3, 4}, arr2[] = {1, 2, 3, 4}, X = 4
Salida: No
Explicación: No hay manera de que la array B[] pueda ser reorganizado de tal manera que se satisfaga la condición A[i] + B[i] <= X.
Enfoque ingenuo: el enfoque más simple es generar todas las permutaciones posibles de la array B[] y, si alguna permutación satisface la condición dada, imprimir Yes . De lo contrario , imprima No.
Complejidad temporal: O(N!)
Espacio auxiliar: O(1)
Enfoque eficiente: para optimizar el enfoque anterior, la idea es usar Sorting . Siga los pasos a continuación para resolver el problema:
- Ordene la array arr1[] en orden ascendente y arr2[] en orden descendente.
- Ahora, recorra ambas arrays simultáneamente y si existe algún par tal que la suma de arr1[i] y arr2[] exceda X , entonces imprima «No» . De lo contrario, escriba «Sí» .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if elements // of B[] can be rearranged // such that A[i] + B[i] <= X void rearrange(int A[], int B[], int N, int X) { // Checks the given condition bool flag = true; // Sort A[] in ascending order sort(A, A + N); // Sort B[] in descending order sort(B, B + N, greater<int>()); // Traverse the arrays A[] and B[] for (int i = 0; i < N; i++) { // If A[i] + B[i] exceeds X if (A[i] + B[i] > X) { // Rearrangement not possible, // set flag to false flag = false; break; } } // If flag is true if (flag) cout << "Yes"; // Otherwise else cout << "No"; } // Driver Code int main() { int A[] = { 1, 2, 3 }; int B[] = { 1, 1, 2 }; int X = 4; int N = sizeof(A) / sizeof(A[0]); // Function Call rearrange(A, B, N, X); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG { // Function to check if elements // of B[] can be rearranged // such that A[i] + B[i] <= X static void rearrange(int A[], int B[], int N, int X) { // Checks the given condition boolean flag = true; // Sort A[] in ascending order Arrays.sort(A); // Sort B[] in descending order Arrays.sort(B); // Traverse the arrays A[] and B[] for (int i = 0; i < N; i++) { // If A[i] + B[i] exceeds X if (A[i] + B[N - 1 - i] > X) { // Rearrangement not possible, // set flag to false flag = false; break; } } // If flag is true if (flag == true) System.out.print("Yes"); // Otherwise else System.out.print("No"); } // Driver Code public static void main (String[] args) { int A[] = { 1, 2, 3 }; int B[] = { 1, 1, 2 }; int X = 4; int N = A.length; // Function Call rearrange(A, B, N, X); } } // This code is contributed by AnkThon
Python3
# Python3 program for the above approach # Function to check if elements # of B can be rearranged # such that A[i] + B[i] <= X def rearrange(A, B, N, X): # Checks the given condition flag = True # Sort A in ascending order A = sorted(A) # Sort B in descending order B = sorted(B)[::-1] # Traverse the arrays A and B for i in range(N): # If A[i] + B[i] exceeds X if (A[i] + B[i] > X): # Rearrangement not possible, # set flag to false flag = False break # If flag is true if (flag): print("Yes") # Otherwise else: print("No") # Driver Code if __name__ == '__main__': A = [ 1, 2, 3 ] B = [ 1, 1, 2 ] X = 4 N = len(A) # Function Call rearrange(A, B, N, X) # This code is contributed by mohit kumar 29
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function to check if elements // of B[] can be rearranged // such that A[i] + B[i] <= X static void rearrange(int[] A, int[] B, int N, int X) { // Checks the given condition bool flag = true; // Sort A[] in ascending order Array.Sort(A); // Sort B[] in descending order Array.Sort(B); // Traverse the arrays A[] and B[] for (int i = 0; i < N; i++) { // If A[i] + B[i] exceeds X if (A[i] + B[N - 1 - i] > X) { // Rearrangement not possible, // set flag to false flag = false; break; } } // If flag is true if (flag == true) Console.WriteLine("Yes"); // Otherwise else Console.WriteLine("No"); } // Driver Code public static void Main() { int []A = { 1, 2, 3 }; int []B = { 1, 1, 2 }; int X = 4; int N = A.Length; // Function Call rearrange(A, B, N, X); } } // This code is contributed by AnkThon
Javascript
<script> // Javascript program of the above approach // Function to check if elements // of B[] can be rearranged // such that A[i] + B[i] <= X function rearrange(A, B, N, X) { // Checks the given condition let flag = true; // Sort A[] in ascending order A.sort(); // Sort B[] in descending order B.sort(); // Traverse the arrays A[] and B[] for(let i = 0; i < N; i++) { // If A[i] + B[i] exceeds X if (A[i] + B[N - 1 - i] > X) { // Rearrangement not possible, // set flag to false flag = false; break; } } // If flag is true if (flag == true) document.write("Yes"); // Otherwise else document.write("No"); } // Driver Code let A = [ 1, 2, 3 ]; let B = [ 1, 1, 2 ]; let X = 4; let N = A.length; // Function Call rearrange(A, B, N, X); // This code is contributed by avijitmondal1998 </script>
Yes
Complejidad de tiempo: O(N*log N)
Espacio auxiliar: O(1)