Dado un número entero N y una array arr[] de tamaño 4 * N , la tarea es verificar si se pueden formar N rectángulos de igual área a partir de esta array si cada elemento se puede usar solo una vez.
Ejemplos:
Entrada: arr[] = {1, 8, 2, 1, 2, 4, 4, 8}, N = 2
Salida: Sí
Dos rectángulos con lados (1, 8, 1, 8) y (2, 4, 2 , 4) se pueden formar.
Ambos rectángulos tienen la misma área.Entrada: arr[] = {1, 3, 3, 5, 5, 7, 1, 6}, N = 2
Salida: No
Acercarse:
- Se necesitan cuatro lados para formar un rectángulo.
- Dados 4 * N enteros, los N rectángulos máximos se pueden formar usando números solo una vez.
- La tarea es verificar si las áreas de todos los rectángulos son iguales. Para verificar esto, primero se ordena la array.
- Los lados se consideran como los dos primeros elementos y los dos últimos elementos.
- El área se calcula y se comprueba si tiene la misma área que el área calculada inicialmente.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to check whether we can make n // rectangles of equal area bool checkRectangles(int* arr, int n) { bool ans = true; // Sort the array sort(arr, arr + 4 * n); // Find the area of any one rectangle int area = arr[0] * arr[4 * n - 1]; // Check whether we have two equal sides // for each rectangle and that area of // each rectangle formed is the same for (int i = 0; i < 2 * n; i = i + 2) { if (arr[i] != arr[i + 1] || arr[4 * n - i - 1] != arr[4 * n - i - 2] || arr[i] * arr[4 * n - i - 1] != area) { // Update the answer to false // if any condition fails ans = false; break; } } // If possible if (ans) return true; return false; } // Driver code int main() { int arr[] = { 1, 8, 2, 1, 2, 4, 4, 8 }; int n = 2; if (checkRectangles(arr, n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to check whether we can make n // rectangles of equal area static boolean checkRectangles(int[] arr, int n) { boolean ans = true; // Sort the array Arrays.sort(arr); // Find the area of any one rectangle int area = arr[0] * arr[4 * n - 1]; // Check whether we have two equal sides // for each rectangle and that area of // each rectangle formed is the same for (int i = 0; i < 2 * n; i = i + 2) { if (arr[i] != arr[i + 1] || arr[4 * n - i - 1] != arr[4 * n - i - 2] || arr[i] * arr[4 * n - i - 1] != area) { // Update the answer to false // if any condition fails ans = false; break; } } // If possible if (ans) return true; return false; } // Driver code public static void main(String[] args) { int arr[] = { 1, 8, 2, 1, 2, 4, 4, 8 }; int n = 2; if (checkRectangles(arr, n)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by 29AjayKumar
Python3
# Python implementation of the approach # Function to check whether we can make n # rectangles of equal area def checkRectangles(arr, n): ans = True # Sort the array arr.sort() # Find the area of any one rectangle area = arr[0] * arr[4 * n - 1] # Check whether we have two equal sides # for each rectangle and that area of # each rectangle formed is the same for i in range(0, 2 * n, 2): if (arr[i] != arr[i + 1] or arr[4 * n - i - 1] != arr[4 * n - i - 2] or arr[i] * arr[4 * n - i - 1] != area): # Update the answer to false # if any condition fails ans = False break # If possible if (ans): return True return False # Driver code arr = [ 1, 8, 2, 1, 2, 4, 4, 8 ] n = 2 if (checkRectangles(arr, n)): print("Yes") else: print("No") # This code is contributed by Sanjit_Prasad
C#
// C# implementation of the approach using System; class GFG { // Function to check whether we can make n // rectangles of equal area static bool checkRectangles(int[] arr, int n) { bool ans = true; // Sort the array Array.Sort(arr); // Find the area of any one rectangle int area = arr[0] * arr[4 * n - 1]; // Check whether we have two equal sides // for each rectangle and that area of // each rectangle formed is the same for (int i = 0; i < 2 * n; i = i + 2) { if (arr[i] != arr[i + 1] || arr[4 * n - i - 1] != arr[4 * n - i - 2] || arr[i] * arr[4 * n - i - 1] != area) { // Update the answer to false // if any condition fails ans = false; break; } } // If possible if (ans) return true; return false; } // Driver code public static void Main(String[] args) { int []arr = { 1, 8, 2, 1, 2, 4, 4, 8 }; int n = 2; if (checkRectangles(arr, n)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by Rajput-Ji
Javascript
<script> // Javascript implementation of the approach // Function to check whether we can make n // rectangles of equal area function checkRectangles(arr, n) { let ans = true; // Sort the array arr.sort(); // Find the area of any one rectangle var area = arr[0] * arr[4 * n - 1]; // Check whether we have two equal sides // for each rectangle and that area of // each rectangle formed is the same for(let i = 0; i < 2 * n; i = i + 2) { if (arr[i] != arr[i + 1] || arr[4 * n - i - 1] != arr[4 * n - i - 2] || arr[i] * arr[4 * n - i - 1] != area) { // Update the answer to false // if any condition fails ans = false; break; } } // If possible if (ans) return true; return false; } // Driver code var arr = [ 1, 8, 2, 1, 2, 4, 4, 8 ]; var n = 2; if (checkRectangles(arr, n)) document.write("Yes"); else document.write("No"); // This code is contributed by gauravrajput1 </script>
Producción:
Yes
Complejidad de tiempo: O(n * log n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por AmanGupta65 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA