📜  最多有 K 个奇数的最大和子数组

📅  最后修改于: 2022-05-13 01:56:10.313000             🧑  作者: Mango

最多有 K 个奇数的最大和子数组

给定一个包含N个整数的数组arr[]和一个整数K ,任务是找到最大子数组和,使得子数组最多有K个奇数整数。

例子:

方法:给定的问题可以使用类似于本文中讨论的方法的滑动窗口技术来解决。创建一个变量odd_cnt ,它存储当前窗口中奇数的个数。如果odd_cnt的值在任何时候超过K ,则从开始减小窗口大小,否则将元素包含在当前窗口中。类似地,迭代整个数组并在变量max_sum中保持所有窗口总和的最大值。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// To find subarray with maximum sum
// having at most K odd integers
int findMaxSubarraySum(int arr[], int n, int K)
{
    // To store current sum and
    // max sum of subarrays
    int curr_sum = arr[0],
        max_sum = 0, start = 0;
 
    // Stores the count of odd integers in
    // the current window
    int odd_cnt = arr[0] % 2;
 
    // Loop to iterate through the given array
    for (int i = 1; i < n; i++) {
 
        // If odd_cnt becomes greater than
        // K start removing starting elements
        while (start < i
               && odd_cnt + arr[i] % 2 > K) {
            curr_sum -= arr[start];
            odd_cnt -= arr[start] % 2;
            start++;
        }
 
        // Update max_sum
        max_sum = max(max_sum, curr_sum);
 
        // If cur_sum becomes negative then
        // start new subarray
        if (curr_sum < 0) {
            curr_sum = 0;
        }
 
        // Add elements to curr_sum
        curr_sum += arr[i];
        odd_cnt += arr[i] % 2;
    }
 
    // Adding an extra check for last subarray
    if (odd_cnt <= K)
        max_sum = max(max_sum, curr_sum);
 
    // Return Answer
    return max_sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 1;
 
    cout << findMaxSubarraySum(arr, N, K);
 
    return 0;
}


Java
// Java program of the above approach
class GFG
{
   
    // To find subarray with maximum sum
    // having at most K odd integers
    public static int findMaxSubarraySum(int arr[], int n, int K)
    {
       
        // To store current sum and
        // max sum of subarrays
        int curr_sum = arr[0], max_sum = 0, start = 0;
 
        // Stores the count of odd integers in
        // the current window
        int odd_cnt = arr[0] % 2;
 
        // Loop to iterate through the given array
        for (int i = 1; i < n; i++) {
 
            // If odd_cnt becomes greater than
            // K start removing starting elements
            while (start < i && odd_cnt + arr[i] % 2 > K) {
                curr_sum -= arr[start];
                odd_cnt -= arr[start] % 2;
                start++;
            }
 
            // Update max_sum
            max_sum = Math.max(max_sum, curr_sum);
 
            // If cur_sum becomes negative then
            // start new subarray
            if (curr_sum < 0) {
                curr_sum = 0;
            }
 
            // Add elements to curr_sum
            curr_sum += arr[i];
            odd_cnt += arr[i] % 2;
        }
 
        // Adding an extra check for last subarray
        if (odd_cnt <= K)
            max_sum = Math.max(max_sum, curr_sum);
 
        // Return Answer
        return max_sum;
    }
 
    // Driver Code
    public static void main(String args[]) {
        int arr[] = { 1, 2, 3, 4, 5, 6 };
        int N = arr.length;
        int K = 1;
 
        System.out.println(findMaxSubarraySum(arr, N, K));
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# python program of the above approach
 
# To find subarray with maximum sum
# having at most K odd integers
 
 
def findMaxSubarraySum(arr, n, K):
 
        # To store current sum and
        # max sum of subarrays
    curr_sum = arr[0]
    max_sum = 0
    start = 0
 
    # Stores the count of odd integers in
    # the current window
    odd_cnt = arr[0] % 2
 
    # Loop to iterate through the given array
    for i in range(1, n):
 
                # If odd_cnt becomes greater than
                # K start removing starting elements
        while (start < i and odd_cnt + arr[i] % 2 > K):
            curr_sum -= arr[start]
            odd_cnt -= arr[start] % 2
            start += 1
 
            # Update max_sum
        max_sum = max(max_sum, curr_sum)
 
        # If cur_sum becomes negative then
        # start new subarray
        if (curr_sum < 0):
            curr_sum = 0
 
            # Add elements to curr_sum
        curr_sum += arr[i]
        odd_cnt += arr[i] % 2
 
        # Adding an extra check for last subarray
    if (odd_cnt <= K):
        max_sum = max(max_sum, curr_sum)
 
        #  Return Answer
    return max_sum
 
 
# Driver Code
if __name__ == "__main__":
 
    arr = [1, 2, 3, 4, 5, 6]
    N = len(arr)
    K = 1
 
    print(findMaxSubarraySum(arr, N, K))
 
    # This code is contributed by rakeshsahni


C#
// C# program of the above approach
using System;
class GFG
{
   
    // To find subarray with maximum sum
    // having at most K odd integers
    public static int findMaxSubarraySum(int[] arr, int n, int K)
    {
       
        // To store current sum and
        // max sum of subarrays
        int curr_sum = arr[0], max_sum = 0, start = 0;
 
        // Stores the count of odd integers in
        // the current window
        int odd_cnt = arr[0] % 2;
 
        // Loop to iterate through the given array
        for (int i = 1; i < n; i++)
        {
 
            // If odd_cnt becomes greater than
            // K start removing starting elements
            while (start < i && odd_cnt + arr[i] % 2 > K)
            {
                curr_sum -= arr[start];
                odd_cnt -= arr[start] % 2;
                start++;
            }
 
            // Update max_sum
            max_sum = Math.Max(max_sum, curr_sum);
 
            // If cur_sum becomes negative then
            // start new subarray
            if (curr_sum < 0)
            {
                curr_sum = 0;
            }
 
            // Add elements to curr_sum
            curr_sum += arr[i];
            odd_cnt += arr[i] % 2;
        }
 
        // Adding an extra check for last subarray
        if (odd_cnt <= K)
            max_sum = Math.Max(max_sum, curr_sum);
 
        // Return Answer
        return max_sum;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 4, 5, 6 };
        int N = arr.Length;
        int K = 1;
 
        Console.Write(findMaxSubarraySum(arr, N, K));
    }
}
 
// This code is contributed by saurabh_jaiswal.


Javascript


输出
15

时间复杂度: O(N)
辅助空间: O(1)