Dado un número entero n y una array de tamaño n, compruebe si cumple las siguientes condiciones:
- Todos los elementos de la array deben estar entre 1 y n.
- La array NO debe ordenarse en orden ascendente.
- La array consta de elementos únicos.
Si se cumplen todas las condiciones, escriba Sí, de lo contrario, No.
Ejemplos:
Input: 4 1 2 3 4 Output: No Array is sorted in ascending order Input: 4 4 3 2 1 Output: Yes Satisfies all given condition Input: 4 1 1 2 3 Output: No Array has repeated entries
Una solución simple es contar la frecuencia de todos los elementos. Mientras cuenta la frecuencia, verifique si todos los elementos están del 1 al n. Finalmente, verifique si la frecuencia de cada elemento es 1 o no y si la array está ordenada en orden ascendente o no.
Una solución eficiente es evitar el espacio extra.
CPP
// CPP program to check array is // beautiful or not #include <iostream> using namespace std; // Function to implement the given task bool isBeautiful(int a[], int n) { int sum = a[0]; bool isAscSorted = true; for (int i = 1; i < n; i++) { // Checking for any repeated entry if (a[i] == a[i - 1]) return 0; // Checking for ascending sorting if (a[i] < a[i - 1]) isAscSorted = false; sum += a[i]; } // Does not satisfy second condition if (isAscSorted == true) return false; // Sum of 1 to n elements is // (n*(n + 1)/2)) return (sum == (n * (n + 1) / 2)); } // Driver Code int main() { int a[] = {1, 2, 4, 3}; int n = sizeof(a) / sizeof(a[0]); if (isBeautiful(a, n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java program to check array is // beautiful or not import java.io.*; class GFG { // Function to implement the given task static boolean isBeautiful(int a[], int n) { int sum = a[0]; boolean isAscSorted = true; for (int i = 1; i < n; i++) { // Checking for any repeated entry if (a[i] == a[i - 1]) return false; // Checking for ascending sorting if (a[i] < a[i - 1]) isAscSorted = false; sum += a[i]; } // Does not satisfy second condition if (isAscSorted == true) return false; // Sum of 1 to n elements is // (n*(n + 1)/2)) return (sum == (n * (n + 1) / 2)); } // Driver Code public static void main (String[] args) { int a[] = {1, 2, 4, 3}; int n = a.length; if (isBeautiful(a, n)) System.out.println ( "Yes"); else System.out.println("No"); } } // This code is contributed by vt_m
Python3
# Python program to check array is # beautiful or not # Function to implement the given task def isBeautiful(a,n): sum = a[0] isAscSorted = True for i in range(1,n): # Checking for any repeated entry if (a[i] == a[i - 1]): return False # Checking for ascending sorting if (a[i] < a[i - 1]): isAscSorted = False sum=sum+ a[i] # Does not satisfy second condition if (isAscSorted == True): return False #Sum of 1 to n elements is # (n*(n + 1)/2)) return (sum == (n * (n + 1) // 2)) # Driver code a= [1, 2, 4, 3] n = len(a) if (isBeautiful(a, n)): print("Yes") else: print("No") # This code is contributed # by Anant Agarwal.
C#
// C# program to check array is // beautiful or not using System; class GFG { // Function to implement the given task static bool isBeautiful(int []a, int n) { int sum = a[0]; bool isAscSorted = true; for (int i = 1; i < n; i++) { // Checking for any repeated entry if (a[i] == a[i - 1]) return false; // Checking for ascending sorting if (a[i] < a[i - 1]) isAscSorted = false; sum += a[i]; } // Does not satisfy second condition if (isAscSorted == true) return false; // Sum of 1 to n elements is // (n*(n + 1)/2)) return (sum == (n * (n + 1) / 2)); } // Driver Code public static void Main () { int []a = {1, 2, 4, 3}; int n = a.Length; if (isBeautiful(a, n)) Console.WriteLine( "Yes"); else Console.WriteLine("No"); } } // This code is contributed by vt_m
PHP
<?php // PHP program to check array is // beautiful or not // Function to implement // the given task function isBeautiful($a, $n) { $sum = $a[0]; $sAscSorted = true; for ($i = 1; $i < $n; $i++) { // Checking for any repeated entry if ($a[$i] == $a[$i - 1]) return 0; // Checking for ascending sorting if ($a[$i] < $a[$i - 1]) $isAscSorted = false; $sum += $a[$i]; } // Does not satisfy second condition if ($isAscSorted == true) return false; // Sum of 1 to n elements is // (n*(n + 1)/2)) return ($sum == ($n * ($n + 1) / 2)); } // Driver Code $a = array(1, 2, 4, 3); $n = count($a); if (isBeautiful($a, $n)) echo "Yes"; else echo "No"; // This code is contributed by Sam007 ?>
Javascript
<script> // Javascript program to check array is // beautiful or not // Function to implement the given task function isBeautiful(a, n) { var sum = a[0]; var isAscSorted = true; for (var i = 1; i < n; i++) { // Checking for any repeated entry if (a[i] == a[i - 1]) return 0; // Checking for ascending sorting if (a[i] < a[i - 1]) isAscSorted = false; sum += a[i]; } // Does not satisfy second condition if (isAscSorted == true) return false; // Sum of 1 to n elements is // (n*(n + 1)/2)) return (sum == (n * (n + 1) / 2)); } // Driver Code var a = [1, 2, 4, 3]; var n = a.length; if (isBeautiful(a, n)) document.write( "Yes"); else document.write( "No"); </script>
Producción:
Yes
Publicación traducida automáticamente
Artículo escrito por Sakshi_Tiwari y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA