Dada una array arr[] , la tarea es verificar si la array dada se puede convertir en una secuencia estrictamente decreciente con la ayuda de la siguiente operación:
- Disminuye cualquier elemento (si es mayor que 1) en 1 en una sola operación
Ejemplos:
Entrada: arr[] = {11, 11, 11, 11}
Salida: Sí
Explicación:
La secuencia dada se puede convertir en
arr[] = {11, 10, 9, 8}
Entrada: arr[] = {1, 1 }
Salida: No
Explicación:
La secuencia dada no se puede convertir en una secuencia estrictamente decreciente
Enfoque: La idea es verificar si cada elemento de la array es mayor o igual que (N – índice del elemento), porque si es mayor o igual que (N – índice del elemento) significa que la array se puede convertir en [N, N-1, N-2, ….1]
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check that // array can be converted into a // strictly decreasing sequence #include <bits/stdc++.h> using namespace std; // Function to check that array // can be converted into a // strictly decreasing sequence bool check(int* arr, int n) { bool flag = true; // Loop to check that each element is // greater than the (N - index) for (int i = 0; i < n; i++) { // If element is less than (N - index) if (arr[i] < n - i) { flag = false; } } // If array can be converted if (flag) { return true; } else { return false; } } // Driver Code int main() { int arr1[] = { 11, 11, 11, 11 }; int n1 = sizeof(arr1) / sizeof(arr1[0]); // Function calling if (check(arr1, n1)) { cout << "Yes" << endl; } else { cout << "No" << endl; } }
Java
// Java implementation to check that // array can be converted into a // strictly decreasing sequence class GFG{ // Function to check that array // can be converted into a // strictly decreasing sequence static boolean check(int []arr, int n) { boolean flag = true; // Loop to check that each element is // greater than the (N - index) for (int i = 0; i < n; i++) { // If element is less than (N - index) if (arr[i] < n - i) { flag = false; } } // If array can be converted if (flag) { return true; } else { return false; } } // Driver Code public static void main(String[] args) { int arr1[] = { 11, 11, 11, 11 }; int n1 = arr1.length; // Function calling if (check(arr1, n1)) { System.out.print("Yes" + "\n"); } else { System.out.print("No" + "\n"); } } } // This code is contributed by amal kumar choubey
Python3
# Python3 implementation to check that # array can be converted into a # strictly decreasing sequence # Function to check that array # can be converted into a # strictly decreasing sequence def check(arr, n): flag = True; # Loop to check that each element # is greater than the (N - index) for i in range(n): # If element is less than (N - index) if (arr[i] < n - i): flag = False; # If array can be converted if (flag): return True; else: return False; # Driver Code if __name__ == '__main__': arr1 = [ 11, 11, 11, 11 ]; n1 = len(arr1); # Function calling if (check(arr1, n1)): print("Yes"); else: print("No"); # This code is contributed by sapnasingh4991
C#
// C# implementation to check that // array can be converted into a // strictly decreasing sequence using System; class GFG{ // Function to check that array // can be converted into a // strictly decreasing sequence static bool check(int []arr, int n) { bool flag = true; // Loop to check that each element is // greater than the (N - index) for(int i = 0; i < n; i++) { // If element is less than (N - index) if (arr[i] < n - i) { flag = false; } } // If array can be converted if (flag) { return true; } else { return false; } } // Driver Code static public void Main(String[] args) { int []arr1 = { 11, 11, 11, 11 }; int n1 = arr1.Length; // Function calling if (check(arr1, n1)) { Console.Write("Yes" + "\n"); } else { Console.Write("No" + "\n"); } } } // This code is contributed by 29AjayKumar
Javascript
<script> // javascript implementation to check that // array can be converted into a // strictly decreasing sequence // Function to check that array // can be converted into a // strictly decreasing sequence function check(arr,n) { var flag = true; // Loop to check that each element is // greater than the (N - index) for (let i = 0; i < n; i++) { // If element is less than (N - index) if (arr[i] < n - i) { flag = false; } } // If array can be converted if (flag) { return true; } else { return false; } } let arr1= [ 11, 11, 11, 11 ]; let n1 = arr1.length; // Function calling if (check(arr1, n1)) { document.write("Yes"); } else { document.write("No"); } // This code is contributed by vaibhavrabadiya117. </script>
Yes
Publicación traducida automáticamente
Artículo escrito por AmanGupta65 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA