Dada una array arr[] que tiene N enteros en orden no decreciente y un entero K, la tarea es encontrar el valor mínimo de (j – i) para un par (i, j) tal que el rango [arr[i] , arr[j]] contiene al menos K enteros impares.
Ejemplos :
Entrada : arr[] = {1, 3, 6, 8, 15, 21}, K = 3
Salida : 1
Explicación : Para (i, j) = (3, 4), representa el rango [8, 15] que tiene 4 enteros impares {9, 11, 13, 15} (es decir, más que K). De ahí el valor de j – i = 1, que es el mínimo posible.Entrada : arr[] = {5, 6, 7, 8, 9}, K = 5
Salida : -1
Enfoque : el problema dado se puede resolver utilizando el enfoque de dos puntos y algunas matemáticas básicas. La idea es usar una ventana deslizante para verificar el conteo de números impares en el rango y encontrar el tamaño de la ventana más pequeña que contiene al menos K números impares. Se puede hacer manteniendo dos punteros i y j y calculando el recuento de números impares en el rango [arr[i], arr[j]] . Mantenga el valor mínimo de (j – i) en una variable para ventanas con más de K enteros impares, que es la respuesta requerida.
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 find the minimum value of j - i // such that the count of odd integers in the // range [arr[i], arr[j]] is more than K int findEven(int arr[], int N, int K) { // Base Case int L = arr[0]; int R = arr[N - 1]; // Maximum count of odd integers int Count = (L & 1) ? ceil((float)(R - L + 1) / 2) : (R - L + 1) / 2; // If no valid (i, j) exists if (K > Count) { return -1; } // Initialize the variables int i = 0, j = 0, ans = INT_MAX; // Loop for the two pointer approach while (j < N) { L = arr[i]; R = arr[j]; // Calculate count of odd numbers in the // range [L, R] Count = (L & 1) ? ceil((float)(R - L + 1) / 2) : (R - L + 1) / 2; if (K > Count) { j++; } else { // If the current value of j - i // is smaller, update answer if (j - i < ans) { ans = j - i; } i++; } } // Return Answer return ans; } // Driver Code int main() { int arr[] = { 1, 3, 6, 8, 15, 21 }; int N = sizeof(arr) / sizeof(arr[0]); int K = 3; cout << findEven(arr, N, K); return 0; }
Java
// Java program for the above approach import java.util.*; public class GFG { // Function to find the minimum value of j - i // such that the count of odd integers in the // range [arr[i], arr[j]] is more than K static int findEven(int []arr, int N, int K) { // Base Case int L = arr[0]; int R = arr[N - 1]; int Count = 0; // Maximum count of odd integers if ((L & 1) > 0) { Count = (int)Math.ceil((float)(R - L + 1) / 2.0); } else { Count = (R - L + 1) / 2; } // If no valid (i, j) exists if (K > Count) { return -1; } // Initialize the variables int i = 0, j = 0, ans = Integer.MAX_VALUE; // Loop for the two pointer approach while (j < N) { L = arr[i]; R = arr[j]; // Calculate count of odd numbers in the // range [L, R] if ((L & 1) > 0) { Count = (int)Math.ceil((float)(R - L + 1) / 2); } else { Count = (R - L + 1) / 2; } if (K > Count) { j++; } else { // If the current value of j - i // is smaller, update answer if (j - i < ans) { ans = j - i; } i++; } } // Return Answer return ans; } // Driver Code public static void main(String args[]) { int []arr = { 1, 3, 6, 8, 15, 21 }; int N = arr.length; int K = 3; System.out.println(findEven(arr, N, K)); } } // This code is contributed by Samim Hossain Mondal.
Python3
# Python Program to implement # the above approach import math as Math # Function to find the minimum value of j - i # such that the count of odd integers in the # range [arr[i], arr[j]] is more than K def findEven(arr, N, K): # Base Case L = arr[0] R = arr[N - 1] # Maximum count of odd integers Count = Math.ceil((R - L + 1) / 2) if (L & 1) else (R - L + 1) / 2 # If no valid (i, j) exists if (K > Count): return -1 # Initialize the variables i = 0 j = 0 ans = 10**9 # Loop for the two pointer approach while (j < N): L = arr[i] R = arr[j] # Calculate count of odd numbers in the # range [L, R] Count = Math.ceil((R - L + 1) / 2) if (L & 1) else (R - L + 1) / 2 if (K > Count): j += 1 else: # If the current value of j - i # is smaller, update answer if (j - i < ans): ans = j - i i += 1 # Return Answer return ans # Driver Code arr = [1, 3, 6, 8, 15, 21] N = len(arr) K = 3 print(findEven(arr, N, K)) # This code is contributed by gfgking
C#
// C# program for the above approach using System; public class GFG { // Function to find the minimum value of j - i // such that the count of odd integers in the // range [arr[i], arr[j]] is more than K static int findEven(int []arr, int N, int K) { // Base Case int L = arr[0]; int R = arr[N - 1]; int Count = 0; // Maximum count of odd integers if ((L & 1) > 0) { Count = (int)Math.Ceiling((float)(R - L + 1) / 2.0); } else { Count = (R - L + 1) / 2; } // If no valid (i, j) exists if (K > Count) { return -1; } // Initialize the variables int i = 0, j = 0, ans = Int32.MaxValue; // Loop for the two pointer approach while (j < N) { L = arr[i]; R = arr[j]; // Calculate count of odd numbers in the // range [L, R] if ((L & 1) > 0) { Count = (int)Math.Ceiling((float)(R - L + 1) / 2); } else { Count = (R - L + 1) / 2; } if (K > Count) { j++; } else { // If the current value of j - i // is smaller, update answer if (j - i < ans) { ans = j - i; } i++; } } // Return Answer return ans; } // Driver Code public static void Main() { int []arr = { 1, 3, 6, 8, 15, 21 }; int N = arr.Length; int K = 3; Console.Write(findEven(arr, N, K)); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript Program to implement // the above approach // Function to find the minimum value of j - i // such that the count of odd integers in the // range [arr[i], arr[j]] is more than K function findEven(arr, N, K) { // Base Case let L = arr[0]; let R = arr[N - 1]; // Maximum count of odd integers let Count = (L & 1) ? Math.ceil((R - L + 1) / 2) : (R - L + 1) / 2; // If no valid (i, j) exists if (K > Count) { return -1; } // Initialize the variables let i = 0, j = 0, ans = Number.MAX_VALUE; // Loop for the two pointer approach while (j < N) { L = arr[i]; R = arr[j]; // Calculate count of odd numbers in the // range [L, R] Count = (L & 1) ? Math.ceil((R - L + 1) / 2) : (R - L + 1) / 2; if (K > Count) { j++; } else { // If the current value of j - i // is smaller, update answer if (j - i < ans) { ans = j - i; } i++; } } // Return Answer return ans; } // Driver Code let arr = [1, 3, 6, 8, 15, 21]; let N = arr.length; let K = 3; document.write(findEven(arr, N, K)); // This code is contributed by Potta Lokesh </script>
1
Complejidad de Tiempo : O(N) Espacio
Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por akashjha2671 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA