Dado un arreglo de N enteros y un número K, la tarea es encontrar la longitud del subarreglo más pequeño en el que todos los elementos son mayores que K. Si no es posible tal subarreglo, imprima -1.
Ejemplos:
Entrada : a[] = {3, 4, 5, 6, 7, 2, 10, 11}, K = 5
Salida : 1
El subarreglo es {10}
Entrada : a[] = {1, 2, 3}, K = 13
Salida : -1
Enfoque: la tarea es encontrar el subarreglo más pequeño con todos los elementos mayores que K. Dado que el subarreglo más pequeño puede tener un tamaño de 1. Entonces, solo verifique si existe algún elemento en el arreglo mayor que K. Si es así, imprima «1» de lo contrario imprime “-1”.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to print the length of the shortest // subarray with all elements greater than X #include <bits/stdc++.h> using namespace std; // Function to return shortest array int smallestSubarray(int a[], int n, int x) { int count = 0, length = 0; // Iterate in the array for (int i = 0; i < n; i++) { // check if array element // greater than X or not if (a[i] > x) { return 1; } } return -1; } // Driver Code int main() { int a[] = { 1, 22, 3 }; int n = sizeof(a) / sizeof(a[0]); int k = 13; cout << smallestSubarray(a, n, k); return 0; }
Java
// Java program to print the length of the shortest // subarray with all elements greater than X import java.io.*; class GFG { // Function to return shortest array static int smallestSubarray(int a[], int n, int x) { int count = 0, length = 0; // Iterate in the array for (int i = 0; i < n; i++) { // check if array element // greater than X or not if (a[i] > x) { return 1; } } return -1; } // Driver Code public static void main (String[] args) { int a[] = { 1, 22, 3 }; int n = a.length; int k = 13; System.out.println(smallestSubarray(a, n, k)); } } // This code has been contributed by anuj_67..
Python3
# Python 3 program to print the # length of the shortest subarray # with all elements greater than X # Function to return shortest array def smallestSubarray(a, n, k): # Iterate in the array for i in range(n): # check if array element # greater than X or not if a[i] > k: return 1 return -1 # Driver Code a = [1, 22, 3] n = len(a) k = 13 print(smallestSubarray(a, n, k)) # This code is contributed # by Shrikant13
C#
using System; class GFG { // Function to return shortest array static int smallestSubarray(int []a, int n, int x) { // Iterate in the array for (int i = 0; i < n; i++) { // check if array element // greater than X or not if (a[i] > x) { return 1; } } return -1; } // Driver Code static public void Main () { int []a = { 1, 22, 3 }; int n = a.Length; int k = 13; Console.WriteLine(smallestSubarray(a, n, k)); } } // This code is contributed by ajit
PHP
<?php // PHP program to print the length // of the shortest subarray with // all elements greater than X // Function to return shortest array function smallestSubarray($a, $n, $x) { $count = 0; $length = 0; // Iterate in the array for ($i = 0; $i < $n; $i++) { // check if array element // greater than X or not if ($a[$i] > $x) { return 1; } } return -1; } // Driver Code $a = array( 1, 22, 3 ); $n = sizeof($a); $k = 13; echo smallestSubarray($a, $n, $k); // This code is contributed by ajit ?>
Javascript
<script> // JavaScriptto print the length of the shortest // subarray with all elements greater than X // Function to return shortest array function smallestSubarray(a, n, x) { let count = 0, length = 0; // Iterate in the array for (let i = 0; i < n; i++) { // check if array element // greater than X or not if (a[i] > x) { return 1; } } return -1; } // Driver Code let a = [1, 22, 3 ] let n = a.length let k = 13; document.write(smallestSubarray(a, n, k)); // This code is contributed by Surbhi Tyagi. </script>
1
Complejidad temporal : O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por swetankmodi y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA