最小化索引差 (j – i) 使得范围 [arr[i], arr[j]] 包含至少 K 个奇数
给定一个数组arr[] ,其中包含N个非递减顺序的整数和一个整数 K,任务是为一对(i, j)找到(j – i)的最小值,使得范围[arr[i] , arr[j]]至少包含K个奇数。
例子:
Input: arr[] = {1, 3, 6, 8, 15, 21}, K = 3
Output: 1
Explanation: For (i, j) = (3, 4), it represents the range [8, 15] having 4 odd integers {9, 11, 13, 15} (i.e, more than K). Hence the value of j – i = 1, which is the minimum possible.
Input: arr[] = {5, 6, 7, 8, 9}, K = 5
Output: -1
方法:给定的问题可以使用两点方法和一些基础数学来解决。这个想法是使用滑动窗口来检查范围内奇数的计数,并找到包含至少K个奇数的最小窗口的大小。这可以通过维护两个指针i和j并计算范围[arr[i], arr[j]]中奇数的计数来完成。对于具有超过K个奇数整数的窗口,在变量中保持(j – i)的最小值,这是所需的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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 numbrs 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 numbrs 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 numbrs 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 numbrs 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
输出
1
时间复杂度:O(N)
辅助空间:O(1)